-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathprint-tools.ts
289 lines (261 loc) · 6.19 KB
/
print-tools.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import * as CLITable from 'cli-table3'
import * as importedColors from 'colors/safe'
import { commandInfo } from './meta-tools'
import { Toolbox } from '../domain/toolbox'
import { times } from './utils'
import { GluegunPrint, GluegunPrintColors, GluegunPrintTableOptions, TableStyle } from './print-types'
// We're extending `colors` with a few more attributes
const colors = importedColors as GluegunPrintColors
colors.setTheme({
highlight: 'cyan',
info: 'reset',
warning: 'yellow',
success: 'green',
error: 'red',
line: 'grey',
muted: 'grey',
})
// Generate array of arrays of the data rows for length checking
// const getRows = t => times(flip(prop)(t), t.length)
const getRows = (t) => times((i) => t[i], t.length)
const CLI_TABLE_COMPACT = {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: ' ',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: ' ',
}
const CLI_TABLE_MARKDOWN = {
...CLI_TABLE_COMPACT,
left: '|',
right: '|',
middle: '|',
}
/**
* Print a blank line.
*/
function newline() {
console.log('')
}
/**
* Prints a divider line
*/
function divider() {
console.log(colors.line('---------------------------------------------------------------'))
}
/**
* Returns an array of the column widths.
*
* @param cliTable Data table.
* @returns Array of column widths
*/
function findWidths(cliTable: CLITable): number[] {
return [(cliTable as any).options.head]
.concat(getRows(cliTable))
.reduce((colWidths, row) => row.map((str, i) => Math.max(`${str}`.length + 1, colWidths[i] || 1)), [])
}
/**
* Returns an array of column dividers based on column widths, taking possible
* paddings into account.
*
* @param cliTable Data table.
* @returns Array of properly sized column dividers.
*/
function columnHeaderDivider(cliTable: CLITable, style: TableStyle = {}): string[] {
const padding = (style['padding-left'] || 0) + (style['padding-right'] || 0)
return findWidths(cliTable).map((w) => Array(w + padding).join('-'))
}
/**
* Resets the padding of a table.
*
* @param cliTable Data table.
*/
function resetTablePadding(cliTable: CLITable) {
const style = (cliTable as any).options.style
if (style) {
style['padding-left'] = 1
style['padding-right'] = 1
}
}
/**
* Prints an object to table format. The values will already be
* stringified.
*
* @param object The object to turn into a table.
*/
function table(data: string[][], options: GluegunPrintTableOptions = {}): void {
let t
switch (options.format) {
case 'markdown':
// eslint-disable-next-line no-case-declarations
const header = data.shift()
t = new CLITable({
head: header,
chars: CLI_TABLE_MARKDOWN,
style: options.style,
})
t.push(...data)
t.unshift(columnHeaderDivider(t, options.style))
resetTablePadding(t)
break
case 'lean':
t = new CLITable({
style: options.style,
})
t.push(...data)
break
default:
t = new CLITable({
chars: CLI_TABLE_COMPACT,
style: options.style,
})
t.push(...data)
}
console.log(t.toString())
}
/**
* Prints text without theming.
*
* Use this when you're writing stuff outside the toolbox of our
* printing scheme. hint: rarely.
*
* @param message The message to write.
*/
function fancy(message: any): void {
console.log(message)
}
/**
* Writes a normal information message.
*
* This is the default type you should use.
*
* @param message The message to show.
*/
function info(message: string): void {
console.log(colors.info(message))
}
/**
* Writes an error message.
*
* This is when something horribly goes wrong.
*
* @param message The message to show.
*/
function error(message: string): void {
console.log(colors.error(message))
}
/**
* Writes a warning message.
*
* This is when the user might not be getting what they're expecting.
*
* @param message The message to show.
*/
function warning(message: string): void {
console.log(colors.warning(message))
}
/**
* Writes a debug message.
*
* This is for devs only.
*
* @param message The message to show.
*/
function debug(message: string, title = 'DEBUG'): void {
const topLine = `vvv -----[ ${title} ]----- vvv`
const botLine = `^^^ -----[ ${title} ]----- ^^^`
console.log(colors.rainbow(topLine))
console.log(message)
console.log(colors.rainbow(botLine))
}
/**
* Writes a success message.
*
* When something is successful. Use sparingly.
*
* @param message The message to show.
*/
function success(message: string): void {
console.log(colors.success(message))
}
/**
* Writes a highlighted message.
*
* To draw attention to specific lines. Use sparingly.
*
* @param message The message to show.
*/
function highlight(message: string): void {
console.log(colors.highlight(message))
}
/**
* Writes a muted message.
*
* For ancillary info, something that's not the star of the show.
*
* @param message The message to show.
*/
function muted(message: string): void {
console.log(colors.muted(message))
}
/**
* Creates a spinner and starts it up.
*
* @param config The text for the spinner or an ora configuration object.
* @returns The spinner.
*/
function spin(config?: string | object): any {
return require('ora')(config || '').start()
}
/**
* Prints the list of commands.
*
* @param toolbox The toolbox that was used
* @param commandRoot Optional, only show commands with this root
*/
function printCommands(toolbox: Toolbox, commandRoot?: string[]): void {
const data = commandInfo(toolbox, commandRoot)
newline() // a spacer
table(data) // the data
}
function printHelp(toolbox: Toolbox): void {
const {
runtime: { brand },
} = toolbox
info(`${brand} version ${toolbox.meta.version()}`)
printCommands(toolbox)
}
const checkmark = colors.success('✔︎')
const xmark = colors.error('ⅹ')
const print: GluegunPrint = {
colors,
newline,
divider,
findWidths,
columnHeaderDivider,
table,
fancy,
info,
error,
warning,
debug,
success,
highlight,
muted,
spin,
printCommands,
printHelp,
checkmark,
xmark,
}
export { print, GluegunPrint }