Skip to content

Commit

Permalink
[api] Added ability to specify column spacing for rows. Colors are no…
Browse files Browse the repository at this point in the history
…w optional for column headers. Column header colors can now fallback gracefully / inherit from the last column color.
  • Loading branch information
Marak committed Aug 20, 2012
1 parent e0ba800 commit 1211e7b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/cliff.js
Expand Up @@ -107,19 +107,26 @@ cliff.arrayLengths = function (arrs) {
// Outputs the specified `rows` as fixed-width columns, adding // Outputs the specified `rows` as fixed-width columns, adding
// colorized headers if `colors` are supplied. // colorized headers if `colors` are supplied.
// //
cliff.stringifyRows = function (rows, colors) { cliff.stringifyRows = function (rows, colors, options) {
var lengths, columns, output = [], headers; var lengths, columns, output = [], headers;


options = options || {};
options.columnSpacing = options.columnSpacing || 2;

columns = cliff.columnMajor(rows); columns = cliff.columnMajor(rows);
lengths = cliff.arrayLengths(columns); lengths = cliff.arrayLengths(columns);

function stringifyRow(row, colorize) { function stringifyRow(row, colorize) {
var rowtext = '', padding, item, i, length; var rowtext = '', padding, item, i, length;
for (i = 0; i < row.length; i += 1) { for (i = 0; i < row.length; i += 1) {
item = cliff.stringifyLiteral(row[i]); item = cliff.stringifyLiteral(row[i]);
item = colorize ? item[colors[i]] : item;
if(colorize) {
item = item[colors[i]] || item[colors[colors.length -1]] || item;
}

length = realLength(item); length = realLength(item);
padding = length < lengths[i] ? lengths[i] - length + 2 : 2; padding = length < lengths[i] ? lengths[i] - length + options.columnSpacing : options.columnSpacing ;
rowtext += item + new Array(padding).join(' '); rowtext += item + new Array(padding).join(' ');
} }


Expand Down Expand Up @@ -148,12 +155,12 @@ cliff.stringifyRows = function (rows, colors) {
// Extracts the lists of `properties` from the specified `objs` // Extracts the lists of `properties` from the specified `objs`
// and formats them according to `cliff.stringifyRows`. // and formats them according to `cliff.stringifyRows`.
// //
cliff.stringifyObjectRows = cliff.rowifyObjects = function (objs, properties, colors) { cliff.stringifyObjectRows = cliff.rowifyObjects = function (objs, properties, colors, options) {
var rows = [properties].concat(objs.map(function (obj) { var rows = [properties].concat(objs.map(function (obj) {
return cliff.extractFrom(obj, properties); return cliff.extractFrom(obj, properties);
})); }));


return cliff.stringifyRows(rows, colors); return cliff.stringifyRows(rows, colors, options);
}; };


// //
Expand Down

0 comments on commit 1211e7b

Please sign in to comment.