Skip to content

Commit 62056df

Browse files
coreyfarrellbcoe
authored andcommittedNov 10, 2019
refactor!: update deps, drop Node 6
1 parent e49b32f commit 62056df

File tree

4 files changed

+374
-354
lines changed

4 files changed

+374
-354
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "6"
43
- "8"
54
- "10"
5+
- "12"
66
after_script: npm run coverage

‎index.js

+269-241
Original file line numberDiff line numberDiff line change
@@ -1,324 +1,352 @@
1-
var stringWidth = require('string-width')
2-
var stripAnsi = require('strip-ansi')
3-
var wrap = require('wrap-ansi')
4-
var align = {
5-
right: alignRight,
6-
center: alignCenter
7-
}
8-
var top = 0
9-
var right = 1
10-
var bottom = 2
11-
var left = 3
12-
13-
function UI (opts) {
14-
this.width = opts.width
15-
this.wrap = opts.wrap
16-
this.rows = []
17-
}
1+
'use strict'
182

19-
UI.prototype.span = function () {
20-
var cols = this.div.apply(this, arguments)
21-
cols.span = true
22-
}
3+
const stringWidth = require('string-width')
4+
const stripAnsi = require('strip-ansi')
5+
const wrap = require('wrap-ansi')
236

24-
UI.prototype.resetOutput = function () {
25-
this.rows = []
7+
const align = {
8+
right: alignRight,
9+
center: alignCenter
2610
}
27-
28-
UI.prototype.div = function () {
29-
if (arguments.length === 0) this.div('')
30-
if (this.wrap && this._shouldApplyLayoutDSL.apply(this, arguments)) {
31-
return this._applyLayoutDSL(arguments[0])
11+
const top = 0
12+
const right = 1
13+
const bottom = 2
14+
const left = 3
15+
16+
class UI {
17+
constructor (opts) {
18+
this.width = opts.width
19+
this.wrap = opts.wrap
20+
this.rows = []
3221
}
3322

34-
var cols = []
35-
36-
for (var i = 0, arg; (arg = arguments[i]) !== undefined; i++) {
37-
if (typeof arg === 'string') cols.push(this._colFromString(arg))
38-
else cols.push(arg)
23+
span (...args) {
24+
const cols = this.div(...args)
25+
cols.span = true
3926
}
4027

41-
this.rows.push(cols)
42-
return cols
43-
}
28+
resetOutput () {
29+
this.rows = []
30+
}
4431

45-
UI.prototype._shouldApplyLayoutDSL = function () {
46-
return arguments.length === 1 && typeof arguments[0] === 'string' &&
47-
/[\t\n]/.test(arguments[0])
48-
}
32+
div (...args) {
33+
if (args.length === 0) {
34+
this.div('')
35+
}
4936

50-
UI.prototype._applyLayoutDSL = function (str) {
51-
var _this = this
52-
var rows = str.split('\n')
53-
var leftColumnWidth = 0
54-
55-
// simple heuristic for layout, make sure the
56-
// second column lines up along the left-hand.
57-
// don't allow the first column to take up more
58-
// than 50% of the screen.
59-
rows.forEach(function (row) {
60-
var columns = row.split('\t')
61-
if (columns.length > 1 && stringWidth(columns[0]) > leftColumnWidth) {
62-
leftColumnWidth = Math.min(
63-
Math.floor(_this.width * 0.5),
64-
stringWidth(columns[0])
65-
)
37+
if (this.wrap && this._shouldApplyLayoutDSL(...args)) {
38+
return this._applyLayoutDSL(args[0])
6639
}
67-
})
6840

69-
// generate a table:
70-
// replacing ' ' with padding calculations.
71-
// using the algorithmically generated width.
72-
rows.forEach(function (row) {
73-
var columns = row.split('\t')
74-
_this.div.apply(_this, columns.map(function (r, i) {
75-
return {
76-
text: r.trim(),
77-
padding: _this._measurePadding(r),
78-
width: (i === 0 && columns.length > 1) ? leftColumnWidth : undefined
41+
const cols = args.map(arg => {
42+
if (typeof arg === 'string') {
43+
return this._colFromString(arg)
7944
}
80-
}))
81-
})
8245

83-
return this.rows[this.rows.length - 1]
84-
}
46+
return arg
47+
})
8548

86-
UI.prototype._colFromString = function (str) {
87-
return {
88-
text: str,
89-
padding: this._measurePadding(str)
49+
this.rows.push(cols)
50+
return cols
9051
}
91-
}
9252

93-
UI.prototype._measurePadding = function (str) {
94-
// measure padding without ansi escape codes
95-
var noAnsi = stripAnsi(str)
96-
return [0, noAnsi.match(/\s*$/)[0].length, 0, noAnsi.match(/^\s*/)[0].length]
97-
}
53+
_shouldApplyLayoutDSL (...args) {
54+
return args.length === 1 && typeof args[0] === 'string' &&
55+
/[\t\n]/.test(args[0])
56+
}
9857

99-
UI.prototype.toString = function () {
100-
var _this = this
101-
var lines = []
58+
_applyLayoutDSL (str) {
59+
const rows = str.split('\n').map(row => row.split('\t'))
60+
let leftColumnWidth = 0
61+
62+
// simple heuristic for layout, make sure the
63+
// second column lines up along the left-hand.
64+
// don't allow the first column to take up more
65+
// than 50% of the screen.
66+
rows.forEach(columns => {
67+
if (columns.length > 1 && stringWidth(columns[0]) > leftColumnWidth) {
68+
leftColumnWidth = Math.min(
69+
Math.floor(this.width * 0.5),
70+
stringWidth(columns[0])
71+
)
72+
}
73+
})
10274

103-
_this.rows.forEach(function (row, i) {
104-
_this.rowToString(row, lines)
105-
})
75+
// generate a table:
76+
// replacing ' ' with padding calculations.
77+
// using the algorithmically generated width.
78+
rows.forEach(columns => {
79+
this.div(...columns.map((r, i) => {
80+
return {
81+
text: r.trim(),
82+
padding: this._measurePadding(r),
83+
width: (i === 0 && columns.length > 1) ? leftColumnWidth : undefined
84+
}
85+
}))
86+
})
10687

107-
// don't display any lines with the
108-
// hidden flag set.
109-
lines = lines.filter(function (line) {
110-
return !line.hidden
111-
})
88+
return this.rows[this.rows.length - 1]
89+
}
11290

113-
return lines.map(function (line) {
114-
return line.text
115-
}).join('\n')
116-
}
91+
_colFromString (text) {
92+
return {
93+
text,
94+
padding: this._measurePadding(text)
95+
}
96+
}
11797

118-
UI.prototype.rowToString = function (row, lines) {
119-
var _this = this
120-
var padding
121-
var rrows = this._rasterize(row)
122-
var str = ''
123-
var ts
124-
var width
125-
var wrapWidth
126-
127-
rrows.forEach(function (rrow, r) {
128-
str = ''
129-
rrow.forEach(function (col, c) {
130-
ts = '' // temporary string used during alignment/padding.
131-
width = row[c].width // the width with padding.
132-
wrapWidth = _this._negatePadding(row[c]) // the width without padding.
133-
134-
ts += col
135-
136-
for (var i = 0; i < wrapWidth - stringWidth(col); i++) {
137-
ts += ' '
138-
}
98+
_measurePadding (str) {
99+
// measure padding without ansi escape codes
100+
const noAnsi = stripAnsi(str)
101+
return [0, noAnsi.match(/\s*$/)[0].length, 0, noAnsi.match(/^\s*/)[0].length]
102+
}
139103

140-
// align the string within its column.
141-
if (row[c].align && row[c].align !== 'left' && _this.wrap) {
142-
ts = align[row[c].align](ts, wrapWidth)
143-
if (stringWidth(ts) < wrapWidth) ts += new Array(width - stringWidth(ts)).join(' ')
144-
}
104+
toString () {
105+
const lines = []
145106

146-
// apply border and padding to string.
147-
padding = row[c].padding || [0, 0, 0, 0]
148-
if (padding[left]) str += new Array(padding[left] + 1).join(' ')
149-
str += addBorder(row[c], ts, '| ')
150-
str += ts
151-
str += addBorder(row[c], ts, ' |')
152-
if (padding[right]) str += new Array(padding[right] + 1).join(' ')
153-
154-
// if prior row is span, try to render the
155-
// current row on the prior line.
156-
if (r === 0 && lines.length > 0) {
157-
str = _this._renderInline(str, lines[lines.length - 1])
158-
}
107+
this.rows.forEach(row => {
108+
this.rowToString(row, lines)
159109
})
160110

161-
// remove trailing whitespace.
162-
lines.push({
163-
text: str.replace(/ +$/, ''),
164-
span: row.span
165-
})
166-
})
111+
// don't display any lines with the
112+
// hidden flag set.
113+
return lines
114+
.filter(line => !line.hidden)
115+
.map(line => line.text)
116+
.join('\n')
117+
}
167118

168-
return lines
169-
}
119+
rowToString (row, lines) {
120+
this._rasterize(row).forEach((rrow, r) => {
121+
let str = ''
122+
rrow.forEach((col, c) => {
123+
const { width } = row[c] // the width with padding.
124+
const wrapWidth = this._negatePadding(row[c]) // the width without padding.
125+
126+
let ts = col // temporary string used during alignment/padding.
127+
128+
if (wrapWidth > stringWidth(col)) {
129+
ts += ' '.repeat(wrapWidth - stringWidth(col))
130+
}
131+
132+
// align the string within its column.
133+
if (row[c].align && row[c].align !== 'left' && this.wrap) {
134+
ts = align[row[c].align](ts, wrapWidth)
135+
if (stringWidth(ts) < wrapWidth) {
136+
ts += ' '.repeat(width - stringWidth(ts) - 1)
137+
}
138+
}
139+
140+
// apply border and padding to string.
141+
const padding = row[c].padding || [0, 0, 0, 0]
142+
if (padding[left]) {
143+
str += ' '.repeat(padding[left])
144+
}
145+
146+
str += addBorder(row[c], ts, '| ')
147+
str += ts
148+
str += addBorder(row[c], ts, ' |')
149+
if (padding[right]) {
150+
str += ' '.repeat(padding[right])
151+
}
152+
153+
// if prior row is span, try to render the
154+
// current row on the prior line.
155+
if (r === 0 && lines.length > 0) {
156+
str = this._renderInline(str, lines[lines.length - 1])
157+
}
158+
})
159+
160+
// remove trailing whitespace.
161+
lines.push({
162+
text: str.replace(/ +$/, ''),
163+
span: row.span
164+
})
165+
})
170166

171-
function addBorder (col, ts, style) {
172-
if (col.border) {
173-
if (/[.']-+[.']/.test(ts)) return ''
174-
else if (ts.trim().length) return style
175-
else return ' '
167+
return lines
176168
}
177-
return ''
178-
}
179169

180-
// if the full 'source' can render in
181-
// the target line, do so.
182-
UI.prototype._renderInline = function (source, previousLine) {
183-
var leadingWhitespace = source.match(/^ */)[0].length
184-
var target = previousLine.text
185-
var targetTextWidth = stringWidth(target.trimRight())
170+
// if the full 'source' can render in
171+
// the target line, do so.
172+
_renderInline (source, previousLine) {
173+
const leadingWhitespace = source.match(/^ */)[0].length
174+
const target = previousLine.text
175+
const targetTextWidth = stringWidth(target.trimRight())
186176

187-
if (!previousLine.span) return source
177+
if (!previousLine.span) {
178+
return source
179+
}
180+
181+
// if we're not applying wrapping logic,
182+
// just always append to the span.
183+
if (!this.wrap) {
184+
previousLine.hidden = true
185+
return target + source
186+
}
187+
188+
if (leadingWhitespace < targetTextWidth) {
189+
return source
190+
}
188191

189-
// if we're not applying wrapping logic,
190-
// just always append to the span.
191-
if (!this.wrap) {
192192
previousLine.hidden = true
193-
return target + source
193+
194+
return target.trimRight() + ' '.repeat(leadingWhitespace - targetTextWidth) + source.trimLeft()
194195
}
195196

196-
if (leadingWhitespace < targetTextWidth) return source
197+
_rasterize (row) {
198+
const rrows = []
199+
const widths = this._columnWidths(row)
200+
let wrapped
201+
202+
// word wrap all columns, and create
203+
// a data-structure that is easy to rasterize.
204+
row.forEach((col, c) => {
205+
// leave room for left and right padding.
206+
col.width = widths[c]
207+
if (this.wrap) {
208+
wrapped = wrap(col.text, this._negatePadding(col), { hard: true }).split('\n')
209+
} else {
210+
wrapped = col.text.split('\n')
211+
}
197212

198-
previousLine.hidden = true
213+
if (col.border) {
214+
wrapped.unshift('.' + '-'.repeat(this._negatePadding(col) + 2) + '.')
215+
wrapped.push("'" + '-'.repeat(this._negatePadding(col) + 2) + "'")
216+
}
199217

200-
return target.trimRight() + new Array(leadingWhitespace - targetTextWidth + 1).join(' ') + source.trimLeft()
201-
}
218+
// add top and bottom padding.
219+
if (col.padding) {
220+
wrapped.unshift(...new Array(col.padding[top] || 0).fill(''))
221+
wrapped.push(...new Array(col.padding[bottom] || 0).fill(''))
222+
}
202223

203-
UI.prototype._rasterize = function (row) {
204-
var _this = this
205-
var i
206-
var rrow
207-
var rrows = []
208-
var widths = this._columnWidths(row)
209-
var wrapped
210-
211-
// word wrap all columns, and create
212-
// a data-structure that is easy to rasterize.
213-
row.forEach(function (col, c) {
214-
// leave room for left and right padding.
215-
col.width = widths[c]
216-
if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), { hard: true }).split('\n')
217-
else wrapped = col.text.split('\n')
224+
wrapped.forEach((str, r) => {
225+
if (!rrows[r]) {
226+
rrows.push([])
227+
}
218228

219-
if (col.border) {
220-
wrapped.unshift('.' + new Array(_this._negatePadding(col) + 3).join('-') + '.')
221-
wrapped.push("'" + new Array(_this._negatePadding(col) + 3).join('-') + "'")
222-
}
229+
const rrow = rrows[r]
223230

224-
// add top and bottom padding.
231+
for (let i = 0; i < c; i++) {
232+
if (rrow[i] === undefined) {
233+
rrow.push('')
234+
}
235+
}
236+
237+
rrow.push(str)
238+
})
239+
})
240+
241+
return rrows
242+
}
243+
244+
_negatePadding (col) {
245+
let wrapWidth = col.width
225246
if (col.padding) {
226-
for (i = 0; i < (col.padding[top] || 0); i++) wrapped.unshift('')
227-
for (i = 0; i < (col.padding[bottom] || 0); i++) wrapped.push('')
247+
wrapWidth -= (col.padding[left] || 0) + (col.padding[right] || 0)
228248
}
229249

230-
wrapped.forEach(function (str, r) {
231-
if (!rrows[r]) rrows.push([])
250+
if (col.border) {
251+
wrapWidth -= 4
252+
}
253+
254+
return wrapWidth
255+
}
256+
257+
_columnWidths (row) {
258+
if (!this.wrap) {
259+
return row.map(col => {
260+
return col.width || stringWidth(col.text)
261+
})
262+
}
232263

233-
rrow = rrows[r]
264+
let unset = row.length
265+
let remainingWidth = this.width
234266

235-
for (var i = 0; i < c; i++) {
236-
if (rrow[i] === undefined) rrow.push('')
267+
// column widths can be set in config.
268+
const widths = row.map(col => {
269+
if (col.width) {
270+
unset--
271+
remainingWidth -= col.width
272+
return col.width
237273
}
238-
rrow.push(str)
274+
275+
return undefined
239276
})
240-
})
241277

242-
return rrows
243-
}
278+
// any unset widths should be calculated.
279+
const unsetWidth = unset ? Math.floor(remainingWidth / unset) : 0
280+
281+
return widths.map((w, i) => {
282+
if (w === undefined) {
283+
return Math.max(unsetWidth, _minWidth(row[i]))
284+
}
244285

245-
UI.prototype._negatePadding = function (col) {
246-
var wrapWidth = col.width
247-
if (col.padding) wrapWidth -= (col.padding[left] || 0) + (col.padding[right] || 0)
248-
if (col.border) wrapWidth -= 4
249-
return wrapWidth
286+
return w
287+
})
288+
}
250289
}
251290

252-
UI.prototype._columnWidths = function (row) {
253-
var _this = this
254-
var widths = []
255-
var unset = row.length
256-
var unsetWidth
257-
var remainingWidth = this.width
258-
259-
// column widths can be set in config.
260-
row.forEach(function (col, i) {
261-
if (col.width) {
262-
unset--
263-
widths[i] = col.width
264-
remainingWidth -= col.width
265-
} else {
266-
widths[i] = undefined
291+
function addBorder (col, ts, style) {
292+
if (col.border) {
293+
if (/[.']-+[.']/.test(ts)) {
294+
return ''
267295
}
268-
})
269296

270-
// any unset widths should be calculated.
271-
if (unset) unsetWidth = Math.floor(remainingWidth / unset)
272-
widths.forEach(function (w, i) {
273-
if (!_this.wrap) widths[i] = row[i].width || stringWidth(row[i].text)
274-
else if (w === undefined) widths[i] = Math.max(unsetWidth, _minWidth(row[i]))
275-
})
297+
if (ts.trim().length !== 0) {
298+
return style
299+
}
276300

277-
return widths
301+
return ' '
302+
}
303+
304+
return ''
278305
}
279306

280307
// calculates the minimum width of
281308
// a column, based on padding preferences.
282309
function _minWidth (col) {
283-
var padding = col.padding || []
284-
var minWidth = 1 + (padding[left] || 0) + (padding[right] || 0)
285-
if (col.border) minWidth += 4
310+
const padding = col.padding || []
311+
const minWidth = 1 + (padding[left] || 0) + (padding[right] || 0)
312+
if (col.border) {
313+
return minWidth + 4
314+
}
315+
286316
return minWidth
287317
}
288318

289319
function getWindowWidth () {
290-
if (typeof process === 'object' && process.stdout && process.stdout.columns) return process.stdout.columns
320+
if (typeof process === 'object' && process.stdout && process.stdout.columns) {
321+
return process.stdout.columns
322+
}
291323
}
292324

293325
function alignRight (str, width) {
294326
str = str.trim()
295-
var padding = ''
296-
var strWidth = stringWidth(str)
327+
const strWidth = stringWidth(str)
297328

298329
if (strWidth < width) {
299-
padding = new Array(width - strWidth + 1).join(' ')
330+
return ' '.repeat(width - strWidth) + str
300331
}
301332

302-
return padding + str
333+
return str
303334
}
304335

305336
function alignCenter (str, width) {
306337
str = str.trim()
307-
var padding = ''
308-
var strWidth = stringWidth(str.trim())
338+
const strWidth = stringWidth(str)
309339

310340
if (strWidth < width) {
311-
padding = new Array(parseInt((width - strWidth) / 2, 10) + 1).join(' ')
341+
return ' '.repeat((width - strWidth) >> 1) + str
312342
}
313343

314-
return padding + str
344+
return str
315345
}
316346

317-
module.exports = function (opts) {
318-
opts = opts || {}
319-
347+
module.exports = function (opts = {}) {
320348
return new UI({
321-
width: (opts || {}).width || getWindowWidth() || 80,
322-
wrap: typeof opts.wrap === 'boolean' ? opts.wrap : true
349+
width: opts.width || getWindowWidth() || 80,
350+
wrap: opts.wrap !== false
323351
})
324352
}

‎package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@
4545
"author": "Ben Coe <ben@npmjs.com>",
4646
"license": "ISC",
4747
"dependencies": {
48-
"string-width": "^3.1.0",
49-
"strip-ansi": "^5.2.0",
50-
"wrap-ansi": "^5.1.0"
48+
"string-width": "^4.1.0",
49+
"strip-ansi": "^6.0.0",
50+
"wrap-ansi": "^6.1.0"
5151
},
5252
"devDependencies": {
5353
"chai": "^4.2.0",
54-
"chalk": "^2.4.2",
54+
"chalk": "^3.0.0",
5555
"coveralls": "^3.0.3",
56-
"mocha": "^6.0.2",
57-
"nyc": "^13.3.0",
56+
"mocha": "^6.2.2",
57+
"nyc": "^14.1.1",
5858
"standard": "^12.0.1",
59-
"standard-version": "^5.0.2"
59+
"standard-version": "^7.0.0"
6060
},
6161
"files": [
6262
"index.js"
6363
],
6464
"engine": {
65-
"node": ">=6"
65+
"node": ">=8"
6666
}
6767
}

‎test/cliui.js

+96-104
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1+
'use strict'
2+
13
/* global describe, it */
24

35
require('chai').should()
46

57
// Force chalk to enable color, if it's disabled the test fails.
68
process.env['FORCE_COLOR'] = 1
79

8-
var chalk = require('chalk')
9-
var cliui = require('../')
10-
var stripAnsi = require('strip-ansi')
10+
const chalk = require('chalk')
11+
const cliui = require('../')
12+
const stripAnsi = require('strip-ansi')
1113

12-
describe('cliui', function () {
13-
describe('resetOutput', function () {
14-
it('should set lines to empty', function () {
15-
var ui = cliui()
14+
describe('cliui', () => {
15+
describe('resetOutput', () => {
16+
it('should set lines to empty', () => {
17+
const ui = cliui()
1618
ui.div('i am a value that would be in a line')
1719
ui.resetOutput()
1820
ui.toString().length.should.be.equal(0)
1921
})
2022
})
2123

22-
describe('div', function () {
23-
it("wraps text at 'width' if a single column is given", function () {
24-
var ui = cliui({
24+
describe('div', () => {
25+
it("wraps text at 'width' if a single column is given", () => {
26+
const ui = cliui({
2527
width: 10
2628
})
2729

2830
ui.div('i am a string that should be wrapped')
2931

30-
ui.toString().split('\n').forEach(function (row) {
32+
ui.toString().split('\n').forEach(row => {
3133
row.length.should.be.lte(10)
3234
})
3335
})
3436

35-
it('evenly divides text across columns if multiple columns are given', function () {
36-
var ui = cliui({
37+
it('evenly divides text across columns if multiple columns are given', () => {
38+
const ui = cliui({
3739
width: 40
3840
})
3941

@@ -45,12 +47,12 @@ describe('cliui', function () {
4547

4648
// total width of all columns is <=
4749
// the width cliui is initialized with.
48-
ui.toString().split('\n').forEach(function (row) {
50+
ui.toString().split('\n').forEach(row => {
4951
row.length.should.be.lte(40)
5052
})
5153

5254
// it should wrap each column appropriately.
53-
var expected = [
55+
const expected = [
5456
'i am a string i am a i am a third',
5557
'that should be second string that',
5658
'wrapped string that should be',
@@ -61,80 +63,80 @@ describe('cliui', function () {
6163
ui.toString().split('\n').should.eql(expected)
6264
})
6365

64-
it('allows for a blank row to be appended', function () {
65-
var ui = cliui({
66+
it('allows for a blank row to be appended', () => {
67+
const ui = cliui({
6668
width: 40
6769
})
6870

6971
ui.div()
7072

7173
// it should wrap each column appropriately.
72-
var expected = ['']
74+
const expected = ['']
7375

7476
ui.toString().split('\n').should.eql(expected)
7577
})
7678
})
7779

78-
describe('_columnWidths', function () {
79-
it('uses same width for each column by default', function () {
80-
var ui = cliui({
80+
describe('_columnWidths', () => {
81+
it('uses same width for each column by default', () => {
82+
const ui = cliui({
8183
width: 40
8284
})
83-
var widths = ui._columnWidths([{}, {}, {}])
85+
const widths = ui._columnWidths([{}, {}, {}])
8486

8587
widths[0].should.equal(13)
8688
widths[1].should.equal(13)
8789
widths[2].should.equal(13)
8890
})
8991

90-
it('divides width over remaining columns if first column has width specified', function () {
91-
var ui = cliui({
92+
it('divides width over remaining columns if first column has width specified', () => {
93+
const ui = cliui({
9294
width: 40
9395
})
94-
var widths = ui._columnWidths([{ width: 20 }, {}, {}])
96+
const widths = ui._columnWidths([{ width: 20 }, {}, {}])
9597

9698
widths[0].should.equal(20)
9799
widths[1].should.equal(10)
98100
widths[2].should.equal(10)
99101
})
100102

101-
it('divides width over remaining columns if middle column has width specified', function () {
102-
var ui = cliui({
103+
it('divides width over remaining columns if middle column has width specified', () => {
104+
const ui = cliui({
103105
width: 40
104106
})
105-
var widths = ui._columnWidths([{}, { width: 10 }, {}])
107+
const widths = ui._columnWidths([{}, { width: 10 }, {}])
106108

107109
widths[0].should.equal(15)
108110
widths[1].should.equal(10)
109111
widths[2].should.equal(15)
110112
})
111113

112-
it('keeps track of remaining width if multiple columns have width specified', function () {
113-
var ui = cliui({
114+
it('keeps track of remaining width if multiple columns have width specified', () => {
115+
const ui = cliui({
114116
width: 40
115117
})
116-
var widths = ui._columnWidths([{ width: 20 }, { width: 12 }, {}])
118+
const widths = ui._columnWidths([{ width: 20 }, { width: 12 }, {}])
117119

118120
widths[0].should.equal(20)
119121
widths[1].should.equal(12)
120122
widths[2].should.equal(8)
121123
})
122124

123-
it('uses a sane default if impossible widths are specified', function () {
124-
var ui = cliui({
125+
it('uses a sane default if impossible widths are specified', () => {
126+
const ui = cliui({
125127
width: 40
126128
})
127-
var widths = ui._columnWidths([{ width: 30 }, { width: 30 }, { padding: [0, 2, 0, 1] }])
129+
const widths = ui._columnWidths([{ width: 30 }, { width: 30 }, { padding: [0, 2, 0, 1] }])
128130

129131
widths[0].should.equal(30)
130132
widths[1].should.equal(30)
131133
widths[2].should.equal(4)
132134
})
133135
})
134136

135-
describe('alignment', function () {
136-
it('allows a column to be right aligned', function () {
137-
var ui = cliui({
137+
describe('alignment', () => {
138+
it('allows a column to be right aligned', () => {
139+
const ui = cliui({
138140
width: 40
139141
})
140142

@@ -145,7 +147,7 @@ describe('cliui', function () {
145147
)
146148

147149
// it should right-align the second column.
148-
var expected = [
150+
const expected = [
149151
'i am a stringi am a secondi am a third',
150152
' stringstring that',
151153
' should be',
@@ -155,8 +157,8 @@ describe('cliui', function () {
155157
ui.toString().split('\n').should.eql(expected)
156158
})
157159

158-
it('allows a column to be center aligned', function () {
159-
var ui = cliui({
160+
it('allows a column to be center aligned', () => {
161+
const ui = cliui({
160162
width: 60
161163
})
162164

@@ -167,7 +169,7 @@ describe('cliui', function () {
167169
)
168170

169171
// it should right-align the second column.
170-
var expected = [
172+
const expected = [
171173
'i am a string i am a second i am a third string',
172174
' string that should be',
173175
' wrapped'
@@ -177,9 +179,9 @@ describe('cliui', function () {
177179
})
178180
})
179181

180-
describe('padding', function () {
181-
it('handles left/right padding', function () {
182-
var ui = cliui({
182+
describe('padding', () => {
183+
it('handles left/right padding', () => {
184+
const ui = cliui({
183185
width: 40
184186
})
185187

@@ -190,7 +192,7 @@ describe('cliui', function () {
190192
)
191193

192194
// it should add left/right padding to columns.
193-
var expected = [
195+
const expected = [
194196
' i have i have i have no',
195197
' padding padding on padding',
196198
' on my my right',
@@ -200,8 +202,8 @@ describe('cliui', function () {
200202
ui.toString().split('\n').should.eql(expected)
201203
})
202204

203-
it('handles top/bottom padding', function () {
204-
var ui = cliui({
205+
it('handles top/bottom padding', () => {
206+
const ui = cliui({
205207
width: 40
206208
})
207209

@@ -213,7 +215,7 @@ describe('cliui', function () {
213215

214216
// it should add top/bottom padding to second
215217
// and third columns.
216-
var expected = [
218+
const expected = [
217219
'i am a string i am a third',
218220
' string that',
219221
' i am a secondshould be',
@@ -224,26 +226,24 @@ describe('cliui', function () {
224226
ui.toString().split('\n').should.eql(expected)
225227
})
226228

227-
it('preserves leading whitespace as padding', function () {
228-
var ui = cliui()
229+
it('preserves leading whitespace as padding', () => {
230+
const ui = cliui()
229231

230232
ui.div(' LEADING WHITESPACE')
231233
ui.div('\u001b[34m with ansi\u001b[39m')
232234

233-
var expected = [
235+
const expected = [
234236
' LEADING WHITESPACE',
235237
' with ansi'
236238
]
237239

238-
ui.toString().split('\n').map(function (l) {
239-
return stripAnsi(l)
240-
}).should.eql(expected)
240+
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
241241
})
242242
})
243243

244-
describe('border', function () {
245-
it('allows a border to be placed around a div', function () {
246-
var ui = cliui({
244+
describe('border', () => {
245+
it('allows a border to be placed around a div', () => {
246+
const ui = cliui({
247247
width: 40
248248
})
249249

@@ -252,7 +252,7 @@ describe('cliui', function () {
252252
{ text: 'i am a second string', padding: [1, 0, 0, 0], border: true }
253253
)
254254

255-
var expected = [
255+
const expected = [
256256
'.------------------.',
257257
'| i am a first |.------------------.',
258258
'| string || i am a second |',
@@ -264,9 +264,9 @@ describe('cliui', function () {
264264
})
265265
})
266266

267-
describe('wrap', function () {
268-
it('allows wordwrap to be disabled', function () {
269-
var ui = cliui({
267+
describe('wrap', () => {
268+
it('allows wordwrap to be disabled', () => {
269+
const ui = cliui({
270270
wrap: false
271271
})
272272

@@ -280,9 +280,9 @@ describe('cliui', function () {
280280
})
281281
})
282282

283-
describe('span', function () {
284-
it('appends the next row to the end of the prior row if it fits', function () {
285-
var ui = cliui({
283+
describe('span', () => {
284+
it('appends the next row to the end of the prior row if it fits', () => {
285+
const ui = cliui({
286286
width: 40
287287
})
288288

@@ -294,16 +294,16 @@ describe('cliui', function () {
294294
{ text: ' [required] [default: 99]', align: 'right' }
295295
)
296296

297-
var expected = [
297+
const expected = [
298298
'i am a string that will be',
299299
'wrapped [required] [default: 99]'
300300
]
301301

302302
ui.toString().split('\n').should.eql(expected)
303303
})
304304

305-
it('does not append the string if it does not fit on the prior row', function () {
306-
var ui = cliui({
305+
it('does not append the string if it does not fit on the prior row', () => {
306+
const ui = cliui({
307307
width: 40
308308
})
309309

@@ -315,7 +315,7 @@ describe('cliui', function () {
315315
{ text: 'i am a second row', align: 'left' }
316316
)
317317

318-
var expected = [
318+
const expected = [
319319
'i am a string that will be',
320320
'wrapped',
321321
'i am a second row'
@@ -324,8 +324,8 @@ describe('cliui', function () {
324324
ui.toString().split('\n').should.eql(expected)
325325
})
326326

327-
it('always appends text to prior span if wrap is disabled', function () {
328-
var ui = cliui({
327+
it('always appends text to prior span if wrap is disabled', () => {
328+
const ui = cliui({
329329
wrap: false,
330330
width: 40
331331
})
@@ -340,16 +340,16 @@ describe('cliui', function () {
340340

341341
ui.div('a third line')
342342

343-
var expected = [
343+
const expected = [
344344
'i am a string that will be wrapped i am a second row',
345345
'a third line'
346346
]
347347

348348
ui.toString().split('\n').should.eql(expected)
349349
})
350350

351-
it('appends to prior line appropriately when strings contain ansi escape codes', function () {
352-
var ui = cliui({
351+
it('appends to prior line appropriately when strings contain ansi escape codes', () => {
352+
const ui = cliui({
353353
width: 40
354354
})
355355

@@ -361,44 +361,42 @@ describe('cliui', function () {
361361
{ text: chalk.blue(' [required] [default: 99]'), align: 'right' }
362362
)
363363

364-
var expected = [
364+
const expected = [
365365
'i am a string that will be',
366366
'wrapped [required] [default: 99]'
367367
]
368368

369-
ui.toString().split('\n').map(function (l) {
370-
return stripAnsi(l)
371-
}).should.eql(expected)
369+
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
372370
})
373371
})
374372

375-
describe('layoutDSL', function () {
376-
it('turns tab into multiple columns', function () {
377-
var ui = cliui({
373+
describe('layoutDSL', () => {
374+
it('turns tab into multiple columns', () => {
375+
const ui = cliui({
378376
width: 60
379377
})
380378

381379
ui.div(
382380
' <regex> \tmy awesome regex\n <my second thing> \tanother row\t a third column'
383381
)
384382

385-
var expected = [
383+
const expected = [
386384
' <regex> my awesome regex',
387385
' <my second thing> another row a third column'
388386
]
389387

390388
ui.toString().split('\n').should.eql(expected)
391389
})
392390

393-
it('turns newline into multiple rows', function () {
394-
var ui = cliui({
391+
it('turns newline into multiple rows', () => {
392+
const ui = cliui({
395393
width: 40
396394
})
397395

398396
ui.div(
399397
'Usage: $0\n <regex>\t my awesome regex\n <glob>\t my awesome glob\t [required]'
400398
)
401-
var expected = [
399+
const expected = [
402400
'Usage: $0',
403401
' <regex> my awesome regex',
404402
' <glob> my awesome [required]',
@@ -408,34 +406,32 @@ describe('cliui', function () {
408406
ui.toString().split('\n').should.eql(expected)
409407
})
410408

411-
it('aligns rows appropriately when they contain ansi escape codes', function () {
412-
var ui = cliui({
409+
it('aligns rows appropriately when they contain ansi escape codes', () => {
410+
const ui = cliui({
413411
width: 40
414412
})
415413

416414
ui.div(
417415
' <regex>\t ' + chalk.red('my awesome regex') + '\t [regex]\n ' + chalk.blue('<glob>') + '\t my awesome glob\t [required]'
418416
)
419417

420-
var expected = [
418+
const expected = [
421419
' <regex> my awesome [regex]',
422420
' regex',
423421
' <glob> my awesome [required]',
424422
' glob'
425423
]
426424

427-
ui.toString().split('\n').map(function (l) {
428-
return stripAnsi(l)
429-
}).should.eql(expected)
425+
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
430426
})
431427

432-
it('ignores ansi escape codes when measuring padding', function () {
428+
it('ignores ansi escape codes when measuring padding', () => {
433429
// Forcefully enable color-codes for this test
434430
const { enabled, level } = chalk
435431
chalk.enabled = true
436432
chalk.level = 1
437433

438-
var ui = cliui({
434+
const ui = cliui({
439435
width: 25
440436
})
441437

@@ -445,23 +441,21 @@ describe('cliui', function () {
445441
)
446442

447443
// relevant part is first line - leading whitespace should be preserved as left padding
448-
var expected = [
444+
const expected = [
449445
' |',
450446
' __| __| | | _ \\',
451447
' | | | | __/',
452448
' \\__| _| \\__,_| \\___|',
453449
' '
454450
]
455451

456-
ui.toString().split('\n').map(function (l) {
457-
return stripAnsi(l)
458-
}).should.eql(expected)
452+
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
459453
chalk.enabled = enabled
460454
chalk.level = level
461455
})
462456

463-
it('correctly handles lack of ansi escape codes when measuring padding', function () {
464-
var ui = cliui({
457+
it('correctly handles lack of ansi escape codes when measuring padding', () => {
458+
const ui = cliui({
465459
width: 25
466460
})
467461

@@ -471,21 +465,19 @@ describe('cliui', function () {
471465
)
472466

473467
// The difference
474-
var expected = [
468+
const expected = [
475469
' |',
476470
' __| __| | | _ \\',
477471
' | | | | __/',
478472
' \\__| _| \\__,_| \\___|',
479473
''
480474
]
481475

482-
ui.toString().split('\n').map(function (l) {
483-
return stripAnsi(l)
484-
}).should.eql(expected)
476+
ui.toString().split('\n').map(l => stripAnsi(l)).should.eql(expected)
485477
})
486478

487-
it('does not apply DSL if wrap is false', function () {
488-
var ui = cliui({
479+
it('does not apply DSL if wrap is false', () => {
480+
const ui = cliui({
489481
width: 40,
490482
wrap: false
491483
})

0 commit comments

Comments
 (0)
Please sign in to comment.