Skip to content

Commit

Permalink
feat: add resetOutput method (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimAConner authored and bcoe committed Apr 23, 2018
1 parent a76e646 commit 7246902
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ options:

Similar to `div`, except the next row will be appended without
a new line being created.

### cliui.resetOutput()

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.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ UI.prototype.span = function () {
cols.span = true
}

UI.prototype.resetOutput = function () {
this.rows = []
}

UI.prototype.div = function () {
if (arguments.length === 0) this.div('')
if (this.wrap && this._shouldApplyLayoutDSL.apply(this, arguments)) {
Expand Down Expand Up @@ -209,7 +213,7 @@ UI.prototype._rasterize = function (row) {
row.forEach(function (col, c) {
// leave room for left and right padding.
col.width = widths[c]
if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), {hard: true}).split('\n')
if (_this.wrap) wrapped = wrap(col.text, _this._negatePadding(col), { hard: true }).split('\n')
else wrapped = col.text.split('\n')

if (col.border) {
Expand Down
59 changes: 34 additions & 25 deletions test/cliui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ var cliui = require('../')
var stripAnsi = require('strip-ansi')

describe('cliui', function () {
describe('resetOutput', function () {
it('should set lines to empty', function () {
var 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', function () {
it("wraps text at 'width' if a single column is given", function () {
var ui = cliui({
Expand All @@ -29,7 +38,7 @@ describe('cliui', function () {
})

ui.div(
{text: 'i am a string that should be wrapped', width: 15},
{ 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'
)
Expand Down Expand Up @@ -82,7 +91,7 @@ describe('cliui', function () {
var ui = cliui({
width: 40
})
var widths = ui._columnWidths([{width: 20}, {}, {}])
var widths = ui._columnWidths([{ width: 20 }, {}, {}])

widths[0].should.equal(20)
widths[1].should.equal(10)
Expand All @@ -93,7 +102,7 @@ describe('cliui', function () {
var ui = cliui({
width: 40
})
var widths = ui._columnWidths([{}, {width: 10}, {}])
var widths = ui._columnWidths([{}, { width: 10 }, {}])

widths[0].should.equal(15)
widths[1].should.equal(10)
Expand All @@ -104,7 +113,7 @@ describe('cliui', function () {
var ui = cliui({
width: 40
})
var widths = ui._columnWidths([{width: 20}, {width: 12}, {}])
var widths = ui._columnWidths([{ width: 20 }, { width: 12 }, {}])

widths[0].should.equal(20)
widths[1].should.equal(12)
Expand All @@ -115,7 +124,7 @@ describe('cliui', function () {
var ui = cliui({
width: 40
})
var widths = ui._columnWidths([{width: 30}, {width: 30}, {padding: [0, 2, 0, 1]}])
var widths = ui._columnWidths([{ width: 30 }, { width: 30 }, { padding: [0, 2, 0, 1] }])

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

ui.div(
'i am a string',
{text: 'i am a second string', align: 'right'},
{ text: 'i am a second string', align: 'right' },
'i am a third string that should be wrapped'
)

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

ui.div(
'i am a string',
{text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2]},
{ text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2] },
'i am a third string that should be wrapped'
)

Expand All @@ -175,9 +184,9 @@ describe('cliui', function () {
})

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]}
{ 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.
Expand All @@ -198,8 +207,8 @@ describe('cliui', function () {

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]}
{ 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
Expand Down Expand Up @@ -239,8 +248,8 @@ describe('cliui', function () {
})

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}
{ 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 }
)

var expected = [
Expand All @@ -262,9 +271,9 @@ describe('cliui', function () {
})

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]}
{ 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')
Expand All @@ -278,11 +287,11 @@ describe('cliui', function () {
})

ui.span(
{text: 'i am a string that will be wrapped', width: 30}
{ text: 'i am a string that will be wrapped', width: 30 }
)

ui.div(
{text: ' [required] [default: 99]', align: 'right'}
{ text: ' [required] [default: 99]', align: 'right' }
)

var expected = [
Expand All @@ -299,11 +308,11 @@ describe('cliui', function () {
})

ui.span(
{text: 'i am a string that will be wrapped', width: 30}
{ text: 'i am a string that will be wrapped', width: 30 }
)

ui.div(
{text: 'i am a second row', align: 'left'}
{ text: 'i am a second row', align: 'left' }
)

var expected = [
Expand All @@ -322,11 +331,11 @@ describe('cliui', function () {
})

ui.span(
{text: 'i am a string that will be wrapped', width: 30}
{ 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]}
{ text: 'i am a second row', align: 'left', padding: [0, 0, 0, 3] }
)

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

ui.span(
{text: chalk.green('i am a string that will be wrapped'), width: 30}
{ text: chalk.green('i am a string that will be wrapped'), width: 30 }
)

ui.div(
{text: chalk.blue(' [required] [default: 99]'), align: 'right'}
{ text: chalk.blue(' [required] [default: 99]'), align: 'right' }
)

var expected = [
Expand Down

0 comments on commit 7246902

Please sign in to comment.