cliui
Advanced tools
Comparing version 1.0.0 to 1.1.0
54
index.js
@@ -6,5 +6,5 @@ var wrap = require('wordwrap'), | ||
}, | ||
// top = 0, | ||
top = 0, | ||
right = 1, | ||
// bottom = 2, | ||
bottom = 2, | ||
left = 3 | ||
@@ -47,6 +47,8 @@ | ||
UI.prototype.rowToString = function (row) { | ||
var widths = this._columnWidths(row), | ||
rrows = this._rasterize(row, widths), | ||
var _this = this, | ||
rrows = this._rasterize(row), | ||
str = '', | ||
ts | ||
ts, | ||
width, | ||
wrapWidth | ||
@@ -56,4 +58,7 @@ rrows.forEach(function (rrow, r) { | ||
rrow.forEach(function (col, c) { | ||
ts = '' | ||
for (var i = 0; i < widths[c]; i++) { | ||
ts = '' // temporary string used during alignment/padding. | ||
width = row[c].width // the width with padding. | ||
wrapWidth = _this._negatePadding(row[c]) // the width without padding. | ||
for (var i = 0; i < wrapWidth; i++) { | ||
ts += col.charAt(i) || ' ' | ||
@@ -64,8 +69,11 @@ } | ||
if (row[c].align && row[c].align !== 'left') { | ||
ts = align[row[c].align](ts.trim() + '\n' + new Array(widths[c] + 1).join(' ')) | ||
ts = align[row[c].align](ts.trim() + '\n' + new Array(wrapWidth + 1).join(' ')) | ||
.split('\n')[0] | ||
if (ts.length < widths[c]) ts += new Array(widths[c] - ts.length).join(' ') | ||
if (ts.length < wrapWidth) ts += new Array(width - ts.length).join(' ') | ||
} | ||
// add left/right padding and print string. | ||
if (row[c].padding && row[c].padding[left]) str += new Array(row[c].padding[left] + 1).join(' ') | ||
str += ts | ||
if (row[c].padding && row[c].padding[right]) str += new Array(row[c].padding[right] + 1).join(' ') | ||
}) | ||
@@ -77,5 +85,9 @@ }) | ||
UI.prototype._rasterize = function (row, widths) { | ||
var rrow, | ||
rrows = [] | ||
UI.prototype._rasterize = function (row) { | ||
var _this = this, | ||
i, | ||
rrow, | ||
rrows = [], | ||
widths = this._columnWidths(row), | ||
wrapped | ||
@@ -85,3 +97,13 @@ // word wrap all columns, and create | ||
row.forEach(function (col, c) { | ||
wrap.hard(widths[c])(col.text).split('\n').forEach(function (str, r) { | ||
// leave room for left and right padding. | ||
col.width = widths[c] | ||
wrapped = wrap.hard(_this._negatePadding(col))(col.text).split('\n') | ||
// add top and bottom padding. | ||
if (col.padding) { | ||
for (i = 0; i < (col.padding[top] || 0); i++) wrapped.unshift('') | ||
for (i = 0; i < (col.padding[bottom] || 0); i++) wrapped.push('') | ||
} | ||
wrapped.forEach(function (str, r) { | ||
if (!rrows[r]) rrows.push([]) | ||
@@ -99,2 +121,8 @@ rrow = rrows[r] | ||
UI.prototype._negatePadding = function (col) { | ||
var wrapWidth = col.width | ||
if (col.padding) wrapWidth -= (col.padding[left] || 0) + (col.padding[right] || 0) | ||
return wrapWidth | ||
} | ||
UI.prototype._columnWidths = function (row) { | ||
@@ -101,0 +129,0 @@ var widths = [], |
{ | ||
"name": "cliui", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "easily create complex multi-column command-line-interfaces", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,3 +21,3 @@ /* global describe, it */ | ||
it('evenly divides text across columns if multiple tds are given', function () { | ||
it('evenly divides text across columns if multiple columns are given', function () { | ||
var ui = cliui({ | ||
@@ -64,3 +64,3 @@ width: 40 | ||
it('divides width over remaining columns if first width specified', function () { | ||
it('divides width over remaining columns if first column has width specified', function () { | ||
var ui = cliui({ | ||
@@ -76,3 +76,3 @@ width: 40 | ||
it('divides width over remaining columns if middle width specified', function () { | ||
it('divides width over remaining columns if middle column has width specified', function () { | ||
var ui = cliui({ | ||
@@ -88,3 +88,3 @@ width: 40 | ||
it('assigns remaining width if multiple widths specified', function () { | ||
it('keeps track of remaining width if multiple columns have width specified', function () { | ||
var ui = cliui({ | ||
@@ -113,3 +113,3 @@ width: 40 | ||
describe('alignment', function () { | ||
it('allows a column to be right-aligned', function () { | ||
it('allows a column to be right aligned', function () { | ||
var ui = cliui({ | ||
@@ -125,3 +125,3 @@ width: 40 | ||
// it should wrap each column appropriately. | ||
// it should right-align the second column. | ||
var expected = [ | ||
@@ -136,21 +136,72 @@ 'i am a stringi am a secondi am a third ', | ||
}) | ||
it('allows a column to be center aligned', function () { | ||
var ui = cliui({ | ||
width: 60 | ||
}) | ||
ui.row( | ||
'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. | ||
var 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', function () { | ||
it('allows for top/bottom padding', function () { | ||
it('handles left/right padding', function () { | ||
var ui = cliui({ | ||
width: 80 | ||
width: 40 | ||
}) | ||
ui.row( | ||
{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. | ||
var 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', function () { | ||
var ui = cliui({ | ||
width: 40 | ||
}) | ||
ui.row( | ||
'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, 0, 2]} | ||
{text: 'i am a third string that should be wrapped', padding: [0, 0, 1, 0]} | ||
) | ||
console.log(ui.toString()) | ||
// it should add top/bottom padding to second | ||
// and third columns. | ||
var 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) | ||
ui.toString().split('\n').should.eql(expected) | ||
}) | ||
}) | ||
}) |
11702
6
296