Permalink
Browse files

[api] Added ability to specify column spacing for rows. Colors are no…

…w optional for column headers. Column header colors can now fallback gracefully / inherit from the last column color.
  • Loading branch information...
1 parent e0ba800 commit 1211e7b900866c130262b0ccf420e598b31a1429 @Marak Marak committed Aug 20, 2012
Showing with 14 additions and 7 deletions.
  1. +14 −7 lib/cliff.js
View
@@ -107,19 +107,26 @@ cliff.arrayLengths = function (arrs) {
// Outputs the specified `rows` as fixed-width columns, adding
// colorized headers if `colors` are supplied.
//
-cliff.stringifyRows = function (rows, colors) {
+cliff.stringifyRows = function (rows, colors, options) {
var lengths, columns, output = [], headers;
-
+
+ options = options || {};
+ options.columnSpacing = options.columnSpacing || 2;
+
columns = cliff.columnMajor(rows);
lengths = cliff.arrayLengths(columns);
-
+
function stringifyRow(row, colorize) {
var rowtext = '', padding, item, i, length;
for (i = 0; i < row.length; i += 1) {
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);
- 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(' ');
}
@@ -148,12 +155,12 @@ cliff.stringifyRows = function (rows, colors) {
// Extracts the lists of `properties` from the specified `objs`
// 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) {
return cliff.extractFrom(obj, properties);
}));
- return cliff.stringifyRows(rows, colors);
+ return cliff.stringifyRows(rows, colors, options);
};
//

0 comments on commit 1211e7b

Please sign in to comment.