Comparing version 2.0.0 to 2.1.0
44
index.js
@@ -24,2 +24,5 @@ var wrap = require('wordwrap'), | ||
if (arguments.length === 0) this.div('') | ||
if (this.wrap && this._shouldApplyLayoutDSL.apply(this, arguments)) { | ||
return this._applyLayoutDSL(arguments[0]) | ||
} | ||
@@ -37,2 +40,43 @@ var cols = [] | ||
UI.prototype._shouldApplyLayoutDSL = function () { | ||
return arguments.length === 1 && typeof arguments[0] === 'string' && | ||
/[\t\n]/.test(arguments[0]) | ||
} | ||
UI.prototype._applyLayoutDSL = function (str) { | ||
var _this = this, | ||
rows = str.split('\n'), | ||
leftColumnWidth = 0 | ||
// simple heuristic for layout, make sure the | ||
// second column lines up along the left-hand. | ||
// don't allow the first column to take up more | ||
// than 50% of the screen. | ||
rows.forEach(function (row) { | ||
var columns = row.split('\t') | ||
if (columns.length > 1 && columns[0].length > leftColumnWidth) { | ||
leftColumnWidth = Math.min( | ||
Math.floor(_this.width * 0.5), | ||
columns[0].length | ||
) | ||
} | ||
}) | ||
// generate a table: | ||
// replacing ' ' with padding calculations. | ||
// using the algorithmically generated width. | ||
rows.forEach(function (row) { | ||
var columns = row.split('\t') | ||
_this.div.apply(_this, columns.map(function (r, i) { | ||
return { | ||
text: r.trim(), | ||
padding: [0, r.match(/\s*$/)[0].length, 0, r.match(/^\s*/)[0].length], | ||
width: (i === 0 && columns.length > 1) ? leftColumnWidth : undefined | ||
} | ||
})) | ||
}) | ||
return this.rows[this.rows.length - 1] | ||
} | ||
UI.prototype._colFromString = function (str) { | ||
@@ -39,0 +83,0 @@ return { |
{ | ||
"name": "cliui", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "easily create complex multi-column command-line-interfaces", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -42,2 +42,37 @@ # cliui | ||
## Layout DSL | ||
cliui exposes a simple layout DSL: | ||
If you create a single `ui.row`, passing a string rather than an | ||
object: | ||
* `\n`: characters will be interpreted as new rows. | ||
* `\t`: characters will be interpreted as new columns. | ||
* ` `: characters will be interpreted as padding. | ||
**as an example...** | ||
```js | ||
var ui = require('./')({ | ||
width: 60 | ||
}) | ||
ui.div( | ||
'Usage: node ./bin/foo.js\n' + | ||
' <regex>\t provide a regex\n' + | ||
' <glob>\t provide a glob\t [required]' | ||
) | ||
console.log(ui.toString()) | ||
``` | ||
**will output:** | ||
```shell | ||
Usage: node ./bin/foo.js | ||
<regex> provide a regex | ||
<glob> provide a glob [required] | ||
``` | ||
## Methods | ||
@@ -44,0 +79,0 @@ |
@@ -298,2 +298,53 @@ /* global describe, it */ | ||
}) | ||
describe('layoutDSL', function () { | ||
it('turns tab into multiple columns', function () { | ||
var ui = cliui({ | ||
width: 60 | ||
}) | ||
ui.div( | ||
' <regex> \tmy awesome regex\n <my second thing> \tanother row\t a third column' | ||
) | ||
var 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', function () { | ||
var ui = cliui({ | ||
width: 40 | ||
}) | ||
ui.div( | ||
'Usage: $0\n <regex>\t my awesome regex\n <glob>\t my awesome glob\t [required]' | ||
) | ||
var expected = [ | ||
'Usage: $0', | ||
' <regex> my awesome regex', | ||
' <glob> my awesome [required]', | ||
' glob' | ||
] | ||
ui.toString().split('\n').should.eql(expected) | ||
}) | ||
it('does not apply DSL if wrap is false', function () { | ||
var ui = cliui({ | ||
width: 40, | ||
wrap: false | ||
}) | ||
ui.div( | ||
'Usage: $0\ttwo\tthree' | ||
) | ||
ui.toString().should.eql('Usage: $0\ttwo\tthree') | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20365
506
105