summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/utils/formatters.ts
diff options
context:
space:
mode:
authorAbhi <[email protected]>2025-06-10 15:59:52 -0400
committerGitHub <[email protected]>2025-06-10 15:59:52 -0400
commit9c3f34890f220456235303498736938156d7fefe (patch)
tree463b910e7e4bac945e24748fe19bbb5875d7c8eb /packages/cli/src/ui/utils/formatters.ts
parent04e2fe0bff1dc59d90dd81374a652cccc39dc625 (diff)
feat: Add UI for /stats slash command (#883)
Diffstat (limited to 'packages/cli/src/ui/utils/formatters.ts')
-rw-r--r--packages/cli/src/ui/utils/formatters.ts47
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/cli/src/ui/utils/formatters.ts b/packages/cli/src/ui/utils/formatters.ts
index ab02160e..82a78109 100644
--- a/packages/cli/src/ui/utils/formatters.ts
+++ b/packages/cli/src/ui/utils/formatters.ts
@@ -14,3 +14,50 @@ export const formatMemoryUsage = (bytes: number): string => {
}
return `${gb.toFixed(2)} GB`;
};
+
+/**
+ * Formats a duration in milliseconds into a concise, human-readable string (e.g., "1h 5s").
+ * It omits any time units that are zero.
+ * @param milliseconds The duration in milliseconds.
+ * @returns A formatted string representing the duration.
+ */
+export const formatDuration = (milliseconds: number): string => {
+ if (milliseconds <= 0) {
+ return '0s';
+ }
+
+ if (milliseconds < 1000) {
+ return `${milliseconds}ms`;
+ }
+
+ const totalSeconds = milliseconds / 1000;
+
+ if (totalSeconds < 60) {
+ return `${totalSeconds.toFixed(1)}s`;
+ }
+
+ const hours = Math.floor(totalSeconds / 3600);
+ const minutes = Math.floor((totalSeconds % 3600) / 60);
+ const seconds = Math.floor(totalSeconds % 60);
+
+ const parts: string[] = [];
+
+ if (hours > 0) {
+ parts.push(`${hours}h`);
+ }
+ if (minutes > 0) {
+ parts.push(`${minutes}m`);
+ }
+ if (seconds > 0) {
+ parts.push(`${seconds}s`);
+ }
+
+ // If all parts are zero (e.g., exactly 1 hour), return the largest unit.
+ if (parts.length === 0) {
+ if (hours > 0) return `${hours}h`;
+ if (minutes > 0) return `${minutes}m`;
+ return `${seconds}s`;
+ }
+
+ return parts.join(' ');
+};