Skip to content

Commit 7246902

Browse files
TimAConnerbcoe
authored andcommittedApr 23, 2018
feat: add resetOutput method (#57)
1 parent a76e646 commit 7246902

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ options:
108108

109109
Similar to `div`, except the next row will be appended without
110110
a new line being created.
111+
112+
### cliui.resetOutput()
113+
114+
Resets the UI elements of the current cliui instance. Wrapping and width that would normally be reset when creating a new cliui instance by using the constructor are not reset.

‎index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ UI.prototype.span = function () {
2121
cols.span = true
2222
}
2323

24+
UI.prototype.resetOutput = function () {
25+
this.rows = []
26+
}
27+
2428
UI.prototype.div = function () {
2529
if (arguments.length === 0) this.div('')
2630
if (this.wrap && this._shouldApplyLayoutDSL.apply(this, arguments)) {
@@ -209,7 +213,7 @@ UI.prototype._rasterize = function (row) {
209213
row.forEach(function (col, c) {
210214
// leave room for left and right padding.
211215
col.width = widths[c]
212-
if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), {hard: true}).split('\n')
216+
if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), { hard: true }).split('\n')
213217
else wrapped = col.text.split('\n')
214218

215219
if (col.border) {

‎test/cliui.js

+34-25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ var cliui = require('../')
1010
var stripAnsi = require('strip-ansi')
1111

1212
describe('cliui', function () {
13+
describe('resetOutput', function () {
14+
it('should set lines to empty', function () {
15+
var ui = cliui()
16+
ui.div('i am a value that would be in a line')
17+
ui.resetOutput()
18+
ui.toString().length.should.be.equal(0)
19+
})
20+
})
21+
1322
describe('div', function () {
1423
it("wraps text at 'width' if a single column is given", function () {
1524
var ui = cliui({
@@ -29,7 +38,7 @@ describe('cliui', function () {
2938
})
3039

3140
ui.div(
32-
{text: 'i am a string that should be wrapped', width: 15},
41+
{ text: 'i am a string that should be wrapped', width: 15 },
3342
'i am a second string that should be wrapped',
3443
'i am a third string that should be wrapped'
3544
)
@@ -82,7 +91,7 @@ describe('cliui', function () {
8291
var ui = cliui({
8392
width: 40
8493
})
85-
var widths = ui._columnWidths([{width: 20}, {}, {}])
94+
var widths = ui._columnWidths([{ width: 20 }, {}, {}])
8695

8796
widths[0].should.equal(20)
8897
widths[1].should.equal(10)
@@ -93,7 +102,7 @@ describe('cliui', function () {
93102
var ui = cliui({
94103
width: 40
95104
})
96-
var widths = ui._columnWidths([{}, {width: 10}, {}])
105+
var widths = ui._columnWidths([{}, { width: 10 }, {}])
97106

98107
widths[0].should.equal(15)
99108
widths[1].should.equal(10)
@@ -104,7 +113,7 @@ describe('cliui', function () {
104113
var ui = cliui({
105114
width: 40
106115
})
107-
var widths = ui._columnWidths([{width: 20}, {width: 12}, {}])
116+
var widths = ui._columnWidths([{ width: 20 }, { width: 12 }, {}])
108117

109118
widths[0].should.equal(20)
110119
widths[1].should.equal(12)
@@ -115,7 +124,7 @@ describe('cliui', function () {
115124
var ui = cliui({
116125
width: 40
117126
})
118-
var widths = ui._columnWidths([{width: 30}, {width: 30}, {padding: [0, 2, 0, 1]}])
127+
var widths = ui._columnWidths([{ width: 30 }, { width: 30 }, { padding: [0, 2, 0, 1] }])
119128

120129
widths[0].should.equal(30)
121130
widths[1].should.equal(30)
@@ -131,7 +140,7 @@ describe('cliui', function () {
131140

132141
ui.div(
133142
'i am a string',
134-
{text: 'i am a second string', align: 'right'},
143+
{ text: 'i am a second string', align: 'right' },
135144
'i am a third string that should be wrapped'
136145
)
137146

@@ -153,7 +162,7 @@ describe('cliui', function () {
153162

154163
ui.div(
155164
'i am a string',
156-
{text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2]},
165+
{ text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2] },
157166
'i am a third string that should be wrapped'
158167
)
159168

@@ -175,9 +184,9 @@ describe('cliui', function () {
175184
})
176185

177186
ui.div(
178-
{text: 'i have padding on my left', padding: [0, 0, 0, 4]},
179-
{text: 'i have padding on my right', padding: [0, 2, 0, 0], align: 'center'},
180-
{text: 'i have no padding', padding: [0, 0, 0, 0]}
187+
{ text: 'i have padding on my left', padding: [0, 0, 0, 4] },
188+
{ text: 'i have padding on my right', padding: [0, 2, 0, 0], align: 'center' },
189+
{ text: 'i have no padding', padding: [0, 0, 0, 0] }
181190
)
182191

183192
// it should add left/right padding to columns.
@@ -198,8 +207,8 @@ describe('cliui', function () {
198207

199208
ui.div(
200209
'i am a string',
201-
{text: 'i am a second string', padding: [2, 0, 0, 0]},
202-
{text: 'i am a third string that should be wrapped', padding: [0, 0, 1, 0]}
210+
{ text: 'i am a second string', padding: [2, 0, 0, 0] },
211+
{ text: 'i am a third string that should be wrapped', padding: [0, 0, 1, 0] }
203212
)
204213

205214
// it should add top/bottom padding to second
@@ -239,8 +248,8 @@ describe('cliui', function () {
239248
})
240249

241250
ui.div(
242-
{text: 'i am a first string', padding: [0, 0, 0, 0], border: true},
243-
{text: 'i am a second string', padding: [1, 0, 0, 0], border: true}
251+
{ text: 'i am a first string', padding: [0, 0, 0, 0], border: true },
252+
{ text: 'i am a second string', padding: [1, 0, 0, 0], border: true }
244253
)
245254

246255
var expected = [
@@ -262,9 +271,9 @@ describe('cliui', function () {
262271
})
263272

264273
ui.div(
265-
{text: 'i am a string', padding: [0, 1, 0, 0]},
266-
{text: 'i am a second string', padding: [0, 2, 0, 0]},
267-
{text: 'i am a third string that should not be wrapped', padding: [0, 0, 0, 2]}
274+
{ text: 'i am a string', padding: [0, 1, 0, 0] },
275+
{ text: 'i am a second string', padding: [0, 2, 0, 0] },
276+
{ text: 'i am a third string that should not be wrapped', padding: [0, 0, 0, 2] }
268277
)
269278

270279
ui.toString().should.equal('i am a string i am a second string i am a third string that should not be wrapped')
@@ -278,11 +287,11 @@ describe('cliui', function () {
278287
})
279288

280289
ui.span(
281-
{text: 'i am a string that will be wrapped', width: 30}
290+
{ text: 'i am a string that will be wrapped', width: 30 }
282291
)
283292

284293
ui.div(
285-
{text: ' [required] [default: 99]', align: 'right'}
294+
{ text: ' [required] [default: 99]', align: 'right' }
286295
)
287296

288297
var expected = [
@@ -299,11 +308,11 @@ describe('cliui', function () {
299308
})
300309

301310
ui.span(
302-
{text: 'i am a string that will be wrapped', width: 30}
311+
{ text: 'i am a string that will be wrapped', width: 30 }
303312
)
304313

305314
ui.div(
306-
{text: 'i am a second row', align: 'left'}
315+
{ text: 'i am a second row', align: 'left' }
307316
)
308317

309318
var expected = [
@@ -322,11 +331,11 @@ describe('cliui', function () {
322331
})
323332

324333
ui.span(
325-
{text: 'i am a string that will be wrapped', width: 30}
334+
{ text: 'i am a string that will be wrapped', width: 30 }
326335
)
327336

328337
ui.div(
329-
{text: 'i am a second row', align: 'left', padding: [0, 0, 0, 3]}
338+
{ text: 'i am a second row', align: 'left', padding: [0, 0, 0, 3] }
330339
)
331340

332341
ui.div('a third line')
@@ -345,11 +354,11 @@ describe('cliui', function () {
345354
})
346355

347356
ui.span(
348-
{text: chalk.green('i am a string that will be wrapped'), width: 30}
357+
{ text: chalk.green('i am a string that will be wrapped'), width: 30 }
349358
)
350359

351360
ui.div(
352-
{text: chalk.blue(' [required] [default: 99]'), align: 'right'}
361+
{ text: chalk.blue(' [required] [default: 99]'), align: 'right' }
353362
)
354363

355364
var expected = [

0 commit comments

Comments
 (0)
Please sign in to comment.