-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.ts
More file actions
214 lines (196 loc) · 4.45 KB
/
output.ts
File metadata and controls
214 lines (196 loc) · 4.45 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
207
208
209
210
211
212
213
214
import chalk from 'chalk';
let prefix = '';
/**
* Sets a prefix string that will be prepended to all output messages.
*
* @since TBD
*
* @param {string} p - The prefix string.
*
* @returns {void}
*/
export function setPrefix(p: string): void {
prefix = p;
}
/**
* Returns the current output prefix.
*
* @since TBD
*
* @returns {string} The current prefix string.
*/
export function getPrefix(): string {
return prefix;
}
/**
* Formats a message with the current prefix, if one is set.
*
* @since TBD
*
* @param {string} message - The message to format.
*
* @returns {string} The formatted message with prefix prepended if set.
*/
function formatMessage(message: string): string {
if (prefix) {
return `[${prefix}] ${message}`;
}
return message;
}
/**
* Prints a green success message to stdout.
*
* @since TBD
*
* @param {string} message - The success message to display.
*
* @returns {void}
*/
export function success(message: string): void {
console.log(formatMessage(chalk.green(message)));
}
/**
* Prints a red error message to stderr.
*
* @since TBD
*
* @param {string} message - The error message to display.
*
* @returns {void}
*/
export function error(message: string): void {
console.error(formatMessage(chalk.red(message)));
}
/**
* Prints a yellow warning message to stdout.
*
* @since TBD
*
* @param {string} message - The warning message to display.
*
* @returns {void}
*/
export function warning(message: string): void {
console.log(formatMessage(chalk.yellow(message)));
}
/**
* Prints a blue informational message to stdout.
*
* @since TBD
*
* @param {string} message - The informational message to display.
*
* @returns {void}
*/
export function info(message: string): void {
console.log(formatMessage(chalk.blue(message)));
}
/**
* Prints a yellow title with an underline rule.
*
* @since TBD
*
* @param {string} message - The title text to display.
*
* @returns {void}
*/
export function title(message: string): void {
console.log('');
console.log(formatMessage(chalk.yellow(message)));
console.log(formatMessage(chalk.yellow('='.repeat(message.length))));
console.log('');
}
/**
* Prints a yellow section header.
*
* @since TBD
*
* @param {string} message - The section header text to display.
*
* @returns {void}
*/
export function section(message: string): void {
console.log('');
console.log(formatMessage(chalk.yellow(message)));
console.log(formatMessage(chalk.yellow('-'.repeat(message.length))));
console.log('');
}
/**
* Prints a plain message to stdout with the current prefix.
*
* @since TBD
*
* @param {string} message - The message to print.
*
* @returns {void}
*/
export function log(message: string): void {
console.log(formatMessage(message));
}
/**
* Prints a message to stdout without any prefix.
*
* @since TBD
*
* @param {string} message - The message to print.
*
* @returns {void}
*/
export function writeln(message: string): void {
console.log(message);
}
/**
* Prints an empty line to stdout.
*
* @since TBD
*
* @returns {void}
*/
export function newline(): void {
console.log('');
}
/**
* Strips ANSI escape codes from a string for accurate length calculation.
*
* @since TBD
*
* @param {string} str - The string potentially containing ANSI codes.
*
* @returns {string} The string with ANSI codes removed.
*/
function stripAnsi(str: string): string {
return str.replace(/\x1b\[[0-9;]*m/g, '');
}
/**
* Renders a formatted ASCII table to stdout.
*
* @since TBD
*
* @param {string[]} headers - Column header labels.
* @param {string[][]} rows - Array of row data, each row being an array of cell strings.
*
* @returns {void}
*/
export function table(headers: string[], rows: string[][]): void {
const colWidths: number[] = headers.map((h) => stripAnsi(h).length);
for (const row of rows) {
for (let i = 0; i < row.length; i++) {
const len = stripAnsi(row[i] || '').length;
if (len > (colWidths[i] || 0)) {
colWidths[i] = len;
}
}
}
const separator = '|' + colWidths.map((w) => '-'.repeat(w + 2)).join('|') + '|';
const formatRow = (cells: string[]): string => {
return '| ' + cells.map((cell, i) => {
const padding = (colWidths[i] || 0) - stripAnsi(cell || '').length;
return (cell || '') + ' '.repeat(Math.max(0, padding));
}).join(' | ') + ' |';
};
console.log(formatRow(headers));
console.log(separator);
for (const row of rows) {
console.log(formatRow(row));
}
}