Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(fmt): display millisecond durations
  • Loading branch information
chenjiahan committed Sep 1, 2026
commit 9bb4b21fb59e8bdf44ba608740c6e15e65234452
45 changes: 11 additions & 34 deletions packages/rstack/src/fmt/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ensureProjectCacheDir } from '../projectCache.ts';
import { fmtCacheFileName } from './cacheStore.ts';
import { resolveFmtConfig } from './config.ts';
import { discoverFmtFiles } from './discovery.ts';
import { formatElapsed } from './elapsed.ts';
import { createRelativePathResolver, toPosixPath } from './pathHelpers.ts';
import { runFmtFiles } from './runner.ts';
import type { FmtMode, FmtRunResult, ResolvedFmtConfig } from './types.ts';
Expand Down Expand Up @@ -157,34 +158,8 @@ const createDisplayPathResolver = (
return (filePath) => toPosixPath(resolveRelativePath(filePath));
};

const prettyTime = (seconds: number): string => {
const format = (time: string, unit: 'm' | 's') =>
color.bold(`${time}${unit}`);

if (seconds < 10) {
const digits = seconds >= 0.01 ? 2 : 3;
return format(seconds.toFixed(digits), 's');
}

if (seconds < 60) {
return format(seconds.toFixed(1), 's');
}

const minutes = Math.floor(seconds / 60);
const minutesLabel = format(minutes.toFixed(0), 'm');
const remainingSeconds = seconds % 60;

if (remainingSeconds === 0) {
return minutesLabel;
}

const secondsLabel = format(
remainingSeconds.toFixed(remainingSeconds % 1 === 0 ? 0 : 1),
's',
);

return `${minutesLabel} ${secondsLabel}`;
};
const prettyTime = (milliseconds: number): string =>
color.bold(formatElapsed(milliseconds));

const formatCount = (count: number): string => color.bold(count);
const formatFileCount = (count: number, isError = false): string => {
Expand All @@ -207,7 +182,7 @@ const logFmtResult = (
mode: FmtMode,
cwd: string,
processedFileCount: number,
durationSeconds: number,
durationMilliseconds: number,
fixCommand?: string,
): void => {
let writtenCount = 0;
Expand Down Expand Up @@ -235,7 +210,7 @@ const logFmtResult = (
}

const processedFiles = formatFileCount(processedFileCount);
const time = prettyTime(durationSeconds);
const time = prettyTime(durationMilliseconds);
const message =
writtenCount > 0
? `Formatted ${formatCount(writtenCount)} of ${processedFiles} in ${time}.`
Expand All @@ -255,10 +230,12 @@ const logFmtResult = (
? `Run ${color.cyan(fixCommand)} to fix.`
: `Rerun this command without ${color.cyan('--check')} to fix.`;
logger.error(`Formatting issues found in ${differentFiles}. ${fixHint}`);
logger.info(`Checked ${processedFiles} in ${prettyTime(durationSeconds)}.`);
logger.info(
`Checked ${processedFiles} in ${prettyTime(durationMilliseconds)}.`,
);
} else if (result.exitCode === 0) {
logger.success(
`Checked ${formatFileCount(processedFileCount)} in ${prettyTime(durationSeconds)}. No issues found.`,
`Checked ${formatFileCount(processedFileCount)} in ${prettyTime(durationMilliseconds)}. No issues found.`,
);
}
};
Expand Down Expand Up @@ -415,13 +392,13 @@ const runFmtCLI = async (
return;
}

const durationSeconds = (performance.now() - startTime) / 1000;
const durationMilliseconds = performance.now() - startTime;
logFmtResult(
result,
mode,
cwd,
result.processedFileCount,
durationSeconds,
durationMilliseconds,
fixCommand,
);
process.exitCode = result.exitCode;
Expand Down
35 changes: 35 additions & 0 deletions packages/rstack/src/fmt/elapsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const formatSeconds = (milliseconds: number): string => {
const seconds = Math.floor(milliseconds / 1000);
const remainder = milliseconds % 1000;

if (remainder === 0) {
return `${seconds}s`;
}

const fraction = remainder.toString().padStart(3, '0').replace(/0+$/, '');
return `${seconds}.${fraction}s`;
};

/** Formats elapsed milliseconds after rounding to millisecond precision. */
const formatElapsed = (milliseconds: number): string => {
if (milliseconds < 1) {
return '<1ms';
}

const roundedMilliseconds = Math.round(milliseconds);
if (roundedMilliseconds < 1000) {
return `${roundedMilliseconds}ms`;
}

const hours = Math.floor(roundedMilliseconds / 3_600_000);
const minutes = Math.floor(roundedMilliseconds / 60_000) % 60;
const seconds = formatSeconds(roundedMilliseconds % 60_000);

if (hours > 0) {
return `${hours}h${minutes}m${seconds}`;
}

return minutes > 0 ? `${minutes}m${seconds}` : seconds;
};

export { formatElapsed };
5 changes: 4 additions & 1 deletion packages/rstack/tests/cli/fmt/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export const createCliEnv = (): NodeJS.ProcessEnv => {
};

export const normalizeDuration = (output: string): string =>
output.replace(/\d+m(?: \d+(?:\.\d+)?s)?|\d+(?:\.\d+)?s/g, '<duration>');
output.replace(
/<1ms|\d+ms|\d+h\d+m\d+(?:\.\d+)?s|\d+m\d+(?:\.\d+)?s|\d+(?:\.\d+)?s/g,
'<duration>',
);

export const expectWriteSummary = (
output: string,
Expand Down
18 changes: 18 additions & 0 deletions packages/rstack/tests/fmt/elapsed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from 'rstack/test';
import { formatElapsed } from '../../src/fmt/elapsed.ts';

test.each([
[0, '<1ms'],
[0.999, '<1ms'],
[1, '1ms'],
[29.6, '30ms'],
[999.4, '999ms'],
[999.6, '1s'],
[1_390, '1.39s'],
[1_234, '1.234s'],
[60_000, '1m0s'],
[60_123, '1m0.123s'],
[3_661_234, '1h1m1.234s'],
] as const)('formats %sms as %s', (milliseconds, expected) => {
expect(formatElapsed(milliseconds)).toBe(expected);
});