-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
renderers.ts
188 lines (159 loc) Β· 3.96 KB
/
renderers.ts
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
import tty from 'tty';
import terminalColumns, {
breakpoints,
Options as TerminalColumnsOptions,
} from 'terminal-columns';
import type { HelpDocumentNode } from '../types';
import type { FlagData } from './render-flags';
type TypeFunction = (value: any) => any;
/**
* process.stdout.hasColors() may not be available if stdout is not a TTY,
* but whether the viewer can render colors is an environment concern:
* https://github.com/nodejs/node/blob/v18.0.0/lib/internal/tty.js#L106
*
* In the future, they may deprecate the prototype method in favor of a
* standalone function:
* https://github.com/nodejs/node/pull/40240
*/
const stdoutHasColors = tty.WriteStream.prototype.hasColors();
type HelpDocumentNodeOrString<Type extends PropertyKey> = string | HelpDocumentNode<Type>;
export class Renderers {
// Useful for associating an id with data:
// { id: 'title', type: 'string' }
text(text: string) {
return text;
}
bold(text: string) {
return stdoutHasColors
? `\u001B[1m${text}\u001B[22m`
: text.toLocaleUpperCase();
}
indentText({ text, spaces }: { text: string; spaces: number }) {
return text.replace(/^/gm, ' '.repeat(spaces));
}
heading(text: string) {
return this.bold(text);
}
section({
title,
body,
indentBody = 2,
}: {
title?: string;
body?: string;
indentBody?: number;
}) {
return (
`${
(
title
? `${this.heading(title)}\n`
: ''
)
+ (
body
? this.indentText({
text: this.render(body),
spaces: indentBody,
})
: ''
)
}\n`
);
}
table({
tableData,
tableOptions,
tableBreakpoints,
}: {
tableData: string[][];
tableOptions?: TerminalColumnsOptions;
tableBreakpoints?: Record<string, TerminalColumnsOptions>;
}) {
return terminalColumns(
tableData.map(row => row.map(cell => this.render(cell))),
tableBreakpoints ? breakpoints(tableBreakpoints) : tableOptions,
);
}
flagParameter(
typeFunction: TypeFunction | readonly [TypeFunction],
): string {
if (typeFunction === Boolean) {
return '';
}
if (typeFunction === String) {
return '<string>';
}
if (typeFunction === Number) {
return '<number>';
}
if (Array.isArray(typeFunction)) {
return this.flagParameter(typeFunction[0]);
}
return '<value>';
}
flagOperator(_: FlagData) {
return ' ';
}
flagName(flagData: FlagData) {
const {
flag,
flagFormatted,
aliasesEnabled,
aliasFormatted,
} = flagData;
let flagText = '';
if (aliasFormatted) {
flagText += `${aliasFormatted}, `;
} else if (aliasesEnabled) {
flagText += ' ';
}
flagText += flagFormatted;
if ('placeholder' in flag && typeof flag.placeholder === 'string') {
flagText += `${this.flagOperator(flagData)}${flag.placeholder}`;
} else {
// Test: default flag for String type short-hand
const flagPlaceholder = this.flagParameter('type' in flag ? flag.type : flag);
if (flagPlaceholder) {
flagText += `${this.flagOperator(flagData)}${flagPlaceholder}`;
}
}
return flagText;
}
flagDefault(value: any) {
return JSON.stringify(value);
}
flagDescription({ flag }: FlagData) {
let descriptionText = 'description' in flag ? (flag.description ?? '') : '';
if ('default' in flag) {
let { default: flagDefault } = flag;
if (typeof flagDefault === 'function') {
flagDefault = flagDefault();
}
if (flagDefault) {
descriptionText += ` (default: ${this.flagDefault(flagDefault)})`;
}
}
return descriptionText;
}
render(
nodes: (
HelpDocumentNodeOrString<keyof this>
| HelpDocumentNodeOrString<keyof this>[]
),
): string {
if (typeof nodes === 'string') {
return nodes;
}
if (Array.isArray(nodes)) {
return nodes.map(node => this.render(node)).join('\n');
}
if ('type' in nodes && this[nodes.type]) {
const renderer = this[nodes.type];
if (typeof renderer === 'function') {
return renderer.call(this, nodes.data);
}
}
throw new Error(`Invalid node type: ${JSON.stringify(nodes)}`);
}
}