cli-color
Advanced tools
Comparing version 1.1.0 to 1.2.0
'use strict'; | ||
var from = require('es5-ext/array/from') | ||
, iterable = require('es5-ext/iterable/validate-object') | ||
, stringifiable = require('es5-ext/object/validate-stringifiable') | ||
, pad = require('es5-ext/string/#/pad'); | ||
var from = require('es5-ext/array/from') | ||
, iterable = require('es5-ext/iterable/validate-object') | ||
, stringifiable = require('es5-ext/object/validate-stringifiable') | ||
, repeat = require('es5-ext/string/#/repeat') | ||
, getStrippedLength = require('./get-stripped-length'); | ||
module.exports = function (rows/*, options*/) { | ||
var options = Object(arguments[1]), cols = []; | ||
var options = Object(arguments[1]), cols = [] | ||
, colsOptions = options.columns || []; | ||
return from(iterable(rows), function (row, index) { | ||
return from(iterable(row), function (str, index) { | ||
var col = cols[index]; | ||
var col = cols[index], strLength; | ||
if (!col) col = cols[index] = { width: 0 }; | ||
str = stringifiable(str); | ||
if (str.length > col.width) col.width = str.length; | ||
return str; | ||
strLength = getStrippedLength(str); | ||
if (strLength > col.width) col.width = strLength; | ||
return { str: str, length: strLength }; | ||
}); | ||
}).map(function (row) { | ||
return row.map(function (item, index) { | ||
return pad.call(item, ' ', -cols[index].width); | ||
var pad, align = 'left', colOptions = colsOptions && colsOptions[index]; | ||
align = (colOptions && (colOptions.align === 'right')) ? 'right' : 'left'; | ||
pad = repeat.call(' ', cols[index].width - item.length); | ||
if (align === 'left') return item.str + pad; | ||
return pad + item.str; | ||
}).join((options.sep == null) ? ' | ' : options.sep); | ||
}).join('\n') + '\n'; | ||
}; |
{ | ||
"name": "cli-color", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Colors, formatting and other tools for the console", | ||
@@ -22,11 +22,11 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", | ||
"dependencies": { | ||
"ansi-regex": "2", | ||
"d": "^0.1.1", | ||
"es5-ext": "^0.10.8", | ||
"ansi-regex": "^2.1.1", | ||
"d": "1", | ||
"es5-ext": "^0.10.12", | ||
"es6-iterator": "2", | ||
"memoizee": "^0.3.9", | ||
"memoizee": "^0.4.3", | ||
"timers-ext": "0.1" | ||
}, | ||
"devDependencies": { | ||
"tad": "^0.2.3", | ||
"tad": "^0.2.7", | ||
"xlint": "^0.2.2", | ||
@@ -33,0 +33,0 @@ "xlint-jslint-medikoo": "^0.1.4" |
@@ -595,3 +595,3 @@ # cli-color | ||
```javascript | ||
var clc = require('cli-color') | ||
var clc = require('cli-color'); | ||
@@ -612,5 +612,5 @@ var str = clc.bold('foo') + 'bar' + clc.red('elo'); | ||
#### clc.getStrippedLength(str, begin, end) | ||
#### clc.getStrippedLength(str) | ||
Slice provided string with preservation of eventual ANSI formatting | ||
Get actual length of ANSI-formatted string | ||
@@ -637,2 +637,32 @@ ```javascript | ||
#### clc.columns(data[, options]) | ||
Outputs aligned table of columns. | ||
`data` is expected to be an array (or other iterable structure) of rows, where each row is also an array (or other iterable structure) of content to display. | ||
Supported `options`: | ||
- `sep`: Custom colums separator (defaults to `|`) | ||
- `columns`: Per column customizations, as e.g. `[{ align: 'right' }, null, { align: 'left' }]`: | ||
- `align`: Possible options: `'left'`, `'right` (efaults to `'left'`) | ||
```javascript | ||
var clc = require('cli-color'); | ||
process.stdout.write(clc.columns([ | ||
[clc.bold('First Name'), clc.bold('Last Name'), clc.bold('Age')], | ||
['John', 'Doe', 34], | ||
['Martha', 'Smith', 20], | ||
['Jan', 'Kowalski', 30] | ||
])); | ||
/* Outputs: | ||
First Name | Last Name | Age | ||
John | Doe | 34 | ||
Martha | Smith | 20 | ||
Jan | Kowalski | 30 | ||
*/ | ||
``` | ||
##### throbber(write, interval[, format]) | ||
@@ -639,0 +669,0 @@ |
@@ -6,4 +6,4 @@ 'use strict'; | ||
a(typeof t.height, 'number', "Height"); | ||
a(t.width > 0, true); | ||
a(t.height > 0, true); | ||
a(t.width >= 0, true); | ||
a(t.height >= 0, true); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91149
1276
692
+ Addedes6-weak-map@2.0.3(transitive)
+ Addedis-promise@2.2.2(transitive)
+ Addedmemoizee@0.4.17(transitive)
- Removedd@0.1.1(transitive)
- Removedes6-iterator@0.1.3(transitive)
- Removedes6-symbol@2.0.1(transitive)
- Removedes6-weak-map@0.1.4(transitive)
- Removedmemoizee@0.3.10(transitive)
- Removednext-tick@0.2.2(transitive)
Updatedansi-regex@^2.1.1
Updatedd@1
Updatedes5-ext@^0.10.12
Updatedmemoizee@^0.4.3