-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.ts
More file actions
73 lines (63 loc) · 2.02 KB
/
Copy pathanalytics.ts
File metadata and controls
73 lines (63 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @module cli/analytics
*
* Post-run summary printed to stdout after each CLI command.
*/
import { getVersion } from "./version.js"
/** Print package version and exit (for `-V` / `--version`). */
export function printVersion(): void {
console.log(`text-compress v${getVersion()}`)
}
/** Format a byte count for human-readable CLI output. */
export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`
const units = ["KB", "MB", "GB", "TB"] as const
let value = bytes / 1024
for (const unit of units) {
if (value < 1024) {
const digits = value < 10 ? 1 : 0
return `${value.toFixed(digits)} ${unit}`
}
value /= 1024
}
return `${value.toFixed(1)} TB`
}
/** Format integers with thousands separators. */
export function formatCount(value: number): string {
return value.toLocaleString("en-US")
}
export interface RunSummary {
title: string
outputPaths: string[]
stats: Record<string, string | number>
}
/** Print a versioned, aligned summary block after a successful run. */
export function printRunSummary(summary: RunSummary): void {
console.log(`text-compress v${getVersion()}`)
console.log()
console.log(summary.title)
if (summary.outputPaths.length > 1) {
for (const path of summary.outputPaths) {
console.log(` ${path}`)
}
} else if (summary.outputPaths.length === 1) {
console.log(` ${summary.outputPaths[0]}`)
}
if (Object.keys(summary.stats).length === 0) return
console.log()
const width = Math.max(...Object.keys(summary.stats).map((key) => key.length))
for (const [key, value] of Object.entries(summary.stats)) {
console.log(` ${key.padEnd(width)} ${value}`)
}
}
/** Build split-related analytics fields when output was split. */
export function splitAnalytics(
splitChunkSize: number | undefined,
outputParts: number,
): Record<string, string | number> {
if (splitChunkSize === undefined) return {}
return {
"Split limit": `${formatCount(splitChunkSize)} chars / part`,
Parts: outputParts,
}
}