-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcliui.mjs
More file actions
490 lines (394 loc) · 12.9 KB
/
cliui.mjs
File metadata and controls
490 lines (394 loc) · 12.9 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import chalk from 'chalk'
import cliui from '../index.mjs'
import { stripVTControlCharacters as stripAnsi } from 'node:util'
import { should } from 'chai'
/* global describe, it */
should()
// Force chalk to enable color, if it's disabled the test fails.
process.env.FORCE_COLOR = 1
describe('cliui', () => {
describe('resetOutput', () => {
it('should set lines to empty', () => {
const ui = cliui()
ui.div('i am a value that would be in a line')
ui.resetOutput()
ui.toString().length.should.be.equal(0)
})
})
describe('div', () => {
it("wraps text at 'width' if a single column is given", () => {
const ui = cliui({
width: 10
})
ui.div('i am a string that should be wrapped')
ui.toString().split('\n').forEach(row => {
row.length.should.be.lte(10)
})
})
it('evenly divides text across columns if multiple columns are given', () => {
const ui = cliui({
width: 40
})
ui.div(
{ text: 'i am a string that should be wrapped', width: 15 },
'i am a second string that should be wrapped',
'i am a third string that should be wrapped'
)
// total width of all columns is <=
// the width cliui is initialized with.
ui.toString().split('\n').forEach(row => {
row.length.should.be.lte(40)
})
// it should wrap each column appropriately.
const expected = [
'i am a string i am a i am a third',
'that should be second string that',
'wrapped string that should be',
' should be wrapped',
' wrapped'
]
ui.toString().split('\n').should.eql(expected)
})
it('allows for a blank row to be appended', () => {
const ui = cliui({
width: 40
})
ui.div()
// it should wrap each column appropriately.
const expected = ['']
ui.toString().split('\n').should.eql(expected)
})
})
describe('_columnWidths', () => {
it('uses same width for each column by default', () => {
const ui = cliui({
width: 40
})
const widths = ui.columnWidths([{}, {}, {}])
widths[0].should.equal(13)
widths[1].should.equal(13)
widths[2].should.equal(13)
})
it('divides width over remaining columns if first column has width specified', () => {
const ui = cliui({
width: 40
})
const widths = ui.columnWidths([{ width: 20 }, {}, {}])
widths[0].should.equal(20)
widths[1].should.equal(10)
widths[2].should.equal(10)
})
it('divides width over remaining columns if middle column has width specified', () => {
const ui = cliui({
width: 40
})
const widths = ui.columnWidths([{}, { width: 10 }, {}])
widths[0].should.equal(15)
widths[1].should.equal(10)
widths[2].should.equal(15)
})
it('keeps track of remaining width if multiple columns have width specified', () => {
const ui = cliui({
width: 40
})
const widths = ui.columnWidths([{ width: 20 }, { width: 12 }, {}])
widths[0].should.equal(20)
widths[1].should.equal(12)
widths[2].should.equal(8)
})
it('uses a sane default if impossible widths are specified', () => {
const ui = cliui({
width: 40
})
const widths = ui.columnWidths([{ width: 30 }, { width: 30 }, { padding: [0, 2, 0, 1] }])
widths[0].should.equal(30)
widths[1].should.equal(30)
widths[2].should.equal(4)
})
})
describe('alignment', () => {
it('allows a column to be right aligned', () => {
const ui = cliui({
width: 40
})
ui.div(
'i am a string',
{ text: 'i am a second string', align: 'right' },
'i am a third string that should be wrapped'
)
// it should right-align the second column.
const expected = [
'i am a stringi am a secondi am a third',
' stringstring that',
' should be',
' wrapped'
]
ui.toString().split('\n').should.eql(expected)
})
it('allows a column to be center aligned', () => {
const ui = cliui({
width: 60
})
ui.div(
'i am a string',
{ text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2] },
'i am a third string that should be wrapped'
)
// it should right-align the second column.
const expected = [
'i am a string i am a second i am a third string',
' string that should be',
' wrapped'
]
ui.toString().split('\n').should.eql(expected)
})
})
describe('padding', () => {
it('handles left/right padding', () => {
const ui = cliui({
width: 40
})
ui.div(
{ text: 'i have padding on my left', padding: [0, 0, 0, 4] },
{ text: 'i have padding on my right', padding: [0, 2, 0, 0], align: 'center' },
{ text: 'i have no padding', padding: [0, 0, 0, 0] }
)
// it should add left/right padding to columns.
const expected = [
' i have i have i have no',
' padding padding on padding',
' on my my right',
' left'
]
ui.toString().split('\n').should.eql(expected)
})
it('handles top/bottom padding', () => {
const ui = cliui({
width: 40
})
ui.div(
'i am a string',
{ text: 'i am a second string', padding: [2, 0, 0, 0] },
{ text: 'i am a third string that should be wrapped', padding: [0, 0, 1, 0] }
)
// it should add top/bottom padding to second
// and third columns.
const expected = [
'i am a string i am a third',
' string that',
' i am a secondshould be',
' string wrapped',
''
]
ui.toString().split('\n').should.eql(expected)
})
it('preserves leading whitespace as padding', () => {
const ui = cliui()
ui.div(' LEADING WHITESPACE')
ui.div('\u001b[34m with ansi\u001b[39m')
const expected = [
' LEADING WHITESPACE',
' with ansi'
]
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
})
})
describe('border', () => {
it('allows a border to be placed around a div', () => {
const ui = cliui({
width: 40
})
ui.div(
{ text: 'i am a first string', padding: [0, 0, 0, 0], border: true },
{ text: 'i am a second string', padding: [1, 0, 0, 0], border: true }
)
const expected = [
'.------------------.',
'| i am a first |.------------------.',
'| string || i am a second |',
"'------------------'| string |",
" '------------------'"
]
ui.toString().split('\n').should.eql(expected)
})
})
describe('wrap', () => {
it('allows wordwrap to be disabled', () => {
const ui = cliui({
wrap: false
})
ui.div(
{ text: 'i am a string', padding: [0, 1, 0, 0] },
{ text: 'i am a second string', padding: [0, 2, 0, 0] },
{ text: 'i am a third string that should not be wrapped', padding: [0, 0, 0, 2] }
)
ui.toString().should.equal('i am a string i am a second string i am a third string that should not be wrapped')
})
})
describe('span', () => {
it('appends the next row to the end of the prior row if it fits', () => {
const ui = cliui({
width: 40
})
ui.span(
{ text: 'i am a string that will be wrapped', width: 30 }
)
ui.div(
{ text: ' [required] [default: 99]', align: 'right' }
)
const expected = [
'i am a string that will be',
'wrapped [required] [default: 99]'
]
ui.toString().split('\n').should.eql(expected)
})
it('does not append the string if it does not fit on the prior row', () => {
const ui = cliui({
width: 40
})
ui.span(
{ text: 'i am a string that will be wrapped', width: 30 }
)
ui.div(
{ text: 'i am a second row', align: 'left' }
)
const expected = [
'i am a string that will be',
'wrapped',
'i am a second row'
]
ui.toString().split('\n').should.eql(expected)
})
it('always appends text to prior span if wrap is disabled', () => {
const ui = cliui({
wrap: false,
width: 40
})
ui.span(
{ text: 'i am a string that will be wrapped', width: 30 }
)
ui.div(
{ text: 'i am a second row', align: 'left', padding: [0, 0, 0, 3] }
)
ui.div('a third line')
const expected = [
'i am a string that will be wrapped i am a second row',
'a third line'
]
ui.toString().split('\n').should.eql(expected)
})
it('appends to prior line appropriately when strings contain ansi escape codes', () => {
const ui = cliui({
width: 40
})
ui.span(
{ text: chalk.green('i am a string that will be wrapped'), width: 30 }
)
ui.div(
{ text: chalk.blue(' [required] [default: 99]'), align: 'right' }
)
const expected = [
'i am a string that will be',
'wrapped [required] [default: 99]'
]
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
})
})
describe('layoutDSL', () => {
it('turns tab into multiple columns', () => {
const ui = cliui({
width: 60
})
ui.div(
' <regex> \tmy awesome regex\n <my second thing> \tanother row\t a third column'
)
const expected = [
' <regex> my awesome regex',
' <my second thing> another row a third column'
]
ui.toString().split('\n').should.eql(expected)
})
it('turns newline into multiple rows', () => {
const ui = cliui({
width: 40
})
ui.div(
'Usage: $0\n <regex>\t my awesome regex\n <glob>\t my awesome glob\t [required]'
)
const expected = [
'Usage: $0',
' <regex> my awesome regex',
' <glob> my awesome [required]',
' glob'
]
ui.toString().split('\n').should.eql(expected)
})
it('aligns rows appropriately when they contain ansi escape codes', () => {
const ui = cliui({
width: 40
})
ui.div(
' <regex>\t ' + chalk.red('my awesome regex') + '\t [regex]\n ' + chalk.blue('<glob>') + '\t my awesome glob\t [required]'
)
const expected = [
' <regex> my awesome [regex]',
' regex',
' <glob> my awesome [required]',
' glob'
]
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
})
it('ignores ansi escape codes when measuring padding', () => {
// Forcefully enable color-codes for this test
const { enabled, level } = chalk
chalk.enabled = true
chalk.level = 1
const ui = cliui({
width: 25
})
// using figlet font 'Shadow' rendering of text 'true' here
ui.div(
chalk.blue(' | \n __| __| | | _ \\ \n | | | | __/ \n \\__| _| \\__,_| \\___| \n ')
)
// relevant part is first line - leading whitespace should be preserved as left padding
const expected = [
' |',
' __| __| | | _ \\',
' | | | | __/',
' \\__| _| \\__,_| \\___|',
' '
]
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
chalk.enabled = enabled
chalk.level = level
})
it('correctly handles lack of ansi escape codes when measuring padding', () => {
const ui = cliui({
width: 25
})
// using figlet font 'Shadow' rendering of text 'true' here
ui.div(
' | \n __| __| | | _ \\ \n | | | | __/ \n \\__| _| \\__,_| \\___| \n '
)
// The difference
const expected = [
' |',
' __| __| | | _ \\',
' | | | | __/',
' \\__| _| \\__,_| \\___|',
''
]
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
})
it('does not apply DSL if wrap is false', () => {
const ui = cliui({
width: 40,
wrap: false
})
ui.div(
'Usage: $0\ttwo\tthree'
)
ui.toString().should.eql('Usage: $0\ttwo\tthree')
})
})
})