The best way to control Claude Code, Codex and more from any computer or phone

main
ts 26 lines 1.2 KB
Raw
1 import { mFallback, msg } from "gt-i18n";
2 import type { MFunctionType } from "gt-i18n/types";
3
4 /** @public */
5 export function formatDuration(durationMs: number, resolve: MFunctionType = mFallback): string {
6 if (!Number.isFinite(durationMs) || durationMs < 0) return resolve(msg("{ms}ms", { ms: 0 }));
7 if (durationMs < 1_000) {
8 return resolve(msg("{ms}ms", { ms: Math.max(1, Math.round(durationMs)) }));
9 }
10 if (durationMs < 10_000) {
11 return resolve(msg("{seconds}s", { seconds: Math.round(durationMs / 100) / 10 }));
12 }
13 const totalSeconds = Math.round(durationMs / 1_000);
14 // Choose the unit after rounding so 59.5 seconds never renders as "60s".
15 if (totalSeconds < 60) return resolve(msg("{seconds}s", { seconds: totalSeconds }));
16 const hours = Math.floor(totalSeconds / 3_600);
17 const minutes = Math.floor((totalSeconds % 3_600) / 60);
18 const seconds = totalSeconds % 60;
19 if (hours === 0) return resolve(msg("{minutes}m {seconds}s", { minutes, seconds }));
20 if (minutes > 0) {
21 return resolve(msg("{hours}h {minutes}m {seconds}s", { hours, minutes, seconds }));
22 }
23 return seconds > 0
24 ? resolve(msg("{hours}h {seconds}s", { hours, seconds }))
25 : resolve(msg("{hours}h", { hours }));
26 }