-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfooter.ts
More file actions
206 lines (187 loc) · 5.49 KB
/
footer.ts
File metadata and controls
206 lines (187 loc) · 5.49 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @fileoverview Console footer/summary formatting utilities.
* Provides consistent footer and summary formatting for CLI applications.
*/
import colors from '../external/yoctocolors-cjs'
import { repeatString } from '../strings'
import { ArrayPrototypePush, DateCtor, DateNow } from '../primordials'
export interface FooterOptions {
/**
* Width of the footer border in characters.
* @default 80
*/
width?: number | undefined
/**
* Character to use for the border line.
* @default '='
*/
borderChar?: string | undefined
/**
* Include ISO timestamp in footer.
* @default false
*/
showTimestamp?: boolean | undefined
/**
* Show duration since start time.
* @default false
*/
showDuration?: boolean | undefined
/**
* Start time in milliseconds (from Date.now()).
* Required when `showDuration` is true.
*/
startTime?: number | undefined
/**
* Color to apply to the footer message.
* @default 'gray'
*/
color?:
| 'cyan'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'red'
| 'gray'
| undefined
}
export interface SummaryStats {
/** Total number of items processed */
total?: number | undefined
/** Number of successful items */
success?: number | undefined
/** Number of failed items */
failed?: number | undefined
/** Number of skipped items */
skipped?: number | undefined
/** Number of warnings */
warnings?: number | undefined
/** Number of errors */
errors?: number | undefined
/** Duration in milliseconds (timestamp value, not elapsed time) */
duration?: number | undefined
}
/**
* Create a formatted footer with optional message, timestamp, and duration.
* Useful for marking the end of CLI output or showing completion status.
*
* @param message - Optional message to display in footer
* @param options - Footer formatting options
* @returns Formatted footer string with border and optional info
*
* @example
* ```ts
* const startTime = Date.now()
* // ... do work
* console.log(createFooter('Build complete', {
* width: 60,
* color: 'green',
* showDuration: true,
* startTime
* }))
* ```
*/
export function createFooter(
message?: string | undefined,
options?: FooterOptions,
): string {
const {
borderChar = '=',
color = 'gray',
showDuration = false,
showTimestamp = false,
startTime,
width = 80,
} = { __proto__: null, ...options } as FooterOptions
const border = repeatString(borderChar, width)
const lines: string[] = []
if (message) {
const colorFn = color && colors[color] ? colors[color] : (s: string) => s
ArrayPrototypePush(lines, colorFn(message))
}
if (showTimestamp) {
const timestamp = new DateCtor().toISOString()
ArrayPrototypePush(lines, colors.gray(`Completed at: ${timestamp}`))
}
if (showDuration && startTime) {
const duration = DateNow() - startTime
const seconds = (duration / 1000).toFixed(2)
ArrayPrototypePush(lines, colors.gray(`Duration: ${seconds}s`))
}
ArrayPrototypePush(lines, border)
return lines.join('\n')
}
/**
* Create a summary footer with statistics and colored status indicators.
* Automatically formats success/failure/warning counts with appropriate colors.
* Useful for test results, build summaries, or batch operation reports.
*
* @param stats - Statistics to display in the summary
* @param options - Footer formatting options
* @returns Formatted summary footer string with colored indicators
*
* @example
* ```ts
* console.log(createSummaryFooter({
* total: 150,
* success: 145,
* failed: 3,
* skipped: 2,
* warnings: 5
* }))
* // Output: Total: 150 | ✓ 145 passed | ✗ 3 failed | ○ 2 skipped | ⚠ 5 warnings
* // ========================================
* ```
*/
export function createSummaryFooter(
stats: SummaryStats,
options?: FooterOptions,
): string {
const parts: string[] = []
if (stats.total !== undefined) {
ArrayPrototypePush(parts, `Total: ${stats.total}`)
}
if (stats.success !== undefined) {
ArrayPrototypePush(parts, colors.green(`✓ ${stats.success} passed`))
}
if (stats.failed !== undefined && stats.failed > 0) {
ArrayPrototypePush(parts, colors.red(`✗ ${stats.failed} failed`))
}
if (stats.skipped !== undefined && stats.skipped > 0) {
ArrayPrototypePush(parts, colors.yellow(`○ ${stats.skipped} skipped`))
}
if (stats.warnings !== undefined && stats.warnings > 0) {
ArrayPrototypePush(parts, colors.yellow(`⚠ ${stats.warnings} warnings`))
}
if (stats.errors !== undefined && stats.errors > 0) {
ArrayPrototypePush(parts, colors.red(`✗ ${stats.errors} errors`))
}
const message = parts.join(' | ')
return createFooter(message, {
...options,
showDuration: stats.duration !== undefined,
...(stats.duration !== undefined && { startTime: stats.duration }),
})
}
/**
* Print a footer with optional success message.
* Uses `─` border character for a lighter appearance.
* Fixed width of 55 characters to match `printHeader()`.
*
* @param message - Optional message to display (shown in green)
*
* @example
* ```ts
* printFooter('Analysis complete')
* // Output:
* // ───────────────────────────────────────────────────
* // Analysis complete (in green)
* ```
*/
export function printFooter(message?: string | undefined): void {
const border = repeatString('─', 55)
console.log(border)
if (message) {
console.log(colors.green(message))
}
}