Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base: v0.1.3
...
compare: master
  • 20 commits
  • 10 files changed
  • 0 commit comments
  • 5 contributors
Commits on Jul 11, 2011
Charlie Robbins indexzero [doc] Updated image URLs in README.md e08334e
Commits on Sep 11, 2011
Charlie Robbins indexzero [dist] Version bump. 0.1.4 2280bb4
Commits on Sep 12, 2011
Charlie Robbins indexzero [dist] Version bump. 0.1.5 6cc0578
Commits on Oct 04, 2011
Charlie Robbins indexzero [minor] Cleanup for JSHint 5d8bf75
Commits on Jan 23, 2012
Charlie Robbins indexzero [test refactor] Merge test/gh-1-regression-test.js into test/cliff-te…
…st.js
a7dac3a
Commits on Feb 08, 2012
Charlie Robbins indexzero [minor api] Expose `cliff.logger` for upstream integration 42b615a
Charlie Robbins indexzero [dist] Version bump. 0.1.6 2c795b1
Commits on Feb 09, 2012
Charlie Robbins indexzero [fix] Remove left-over references to `winston` in favor of `cliff.log…
…ger`
53d2ee7
Charlie Robbins indexzero [dist] Version bump. 0.1.7 026c49c
Charlie Robbins indexzero [dist doc] Update links to `flatiron/cliff` 52245a3
Charlie Robbins indexzero [fix] Assign logger correctly 1d72400
Commits on May 07, 2012
Christian Howe coderarity [dist] Update author field for consistency abc8ffb
Commits on Jun 16, 2012
Pavan Kumar Sunkara pksunkara [minor] Improve performance by removing lazy-loading after first time e716ac5
Pavan Kumar Sunkara pksunkara [dist] Update winston to v0.6.x ad9cccb
Pavan Kumar Sunkara pksunkara [dist] Version bump v0.1.8 e0ba800
Commits on Aug 20, 2012
Marak Marak [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.
1211e7b
Commits on Sep 15, 2014
Jarrett Cruger jcrugzz [dist] bump vows and winston a533413
Jarrett Cruger jcrugzz [dist] Version bump. 0.1.9 66462b3
Commits on Dec 06, 2014
Charlie Robbins indexzero [dist] Update some dependencies. 53d8e2d
Charlie Robbins indexzero [dist] Version bump. 0.1.10 3baf40a
4 LICENSE
View
@@ -1,4 +1,4 @@
-Copyright (c) 2010 Nodejitsu Inc.
+Copyright (c) 2010 Charlie Robbins & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
+THE SOFTWARE.
16 README.md
View
@@ -215,13 +215,13 @@ Cliff is the swiss army knife of CLI formatting tools. It is based on highly fle
[0]: http://github.com/indexzero/winston
[1]: http://github.com/cloudhead/eyes.js
[2]: http://github.com/marak/colors.js
-[3]: http://github.com/nodejitsu/cliff/tree/master/examples
+[3]: http://github.com/flatiron/cliff/tree/master/examples
[4]: http://vowsjs.org
-[inspect]: https://github.com/nicoreed/cliff/raw/master/assets/inspect.png
-[put-object-rows]: https://github.com/nicoreed/cliff/raw/master/assets/put-object-rows.png
-[put-object]: https://github.com/nicoreed/cliff/raw/master/assets/put-object.png
-[put-rows-colors]: https://github.com/nicoreed/cliff/raw/master/assets/put-rows-colors.png
-[put-rows]: https://github.com/nicoreed/cliff/raw/master/assets/put-rows.png
-[string-object-rows]: https://github.com/nicoreed/cliff/raw/master/assets/string-object-rows.png
-[string-rows]: https://github.com/nicoreed/cliff/raw/master/assets/string-rows.png
+[inspect]: https://github.com/flatiron/cliff/raw/master/assets/inspect.png
+[put-object-rows]: https://github.com/flatiron/cliff/raw/master/assets/put-object-rows.png
+[put-object]: https://github.com/flatiron/cliff/raw/master/assets/put-object.png
+[put-rows-colors]: https://github.com/flatiron/cliff/raw/master/assets/put-rows-colors.png
+[put-rows]: https://github.com/flatiron/cliff/raw/master/assets/put-rows.png
+[string-object-rows]: https://github.com/flatiron/cliff/raw/master/assets/string-object-rows.png
+[string-rows]: https://github.com/flatiron/cliff/raw/master/assets/string-rows.png
4 examples/inspect.js
View
@@ -1,10 +1,10 @@
/*
* put-object.js: Example usage for `cliff.putObject`.
*
- * (C) 2010, Nodejitsu Inc.
+ * (C) 2010, Charlie Robbins & the Contributors
*
*/
-
+
var cliff = require('../lib/cliff');
console.log(cliff.inspect({
4 examples/put-object-rows.js
View
@@ -1,10 +1,10 @@
/*
* put-object-rows.js: Example usage for `cliff.putObjectRows`.
*
- * (C) 2010, Nodejitsu Inc.
+ * (C) 2010, Charlie Robbins & the Contributors
*
*/
-
+
var cliff = require('../lib/cliff');
var objs = [], obj = {
4 examples/put-object.js
View
@@ -1,10 +1,10 @@
/*
* put-object.js: Example usage for `cliff.putObject`.
*
- * (C) 2010, Nodejitsu Inc.
+ * (C) 2010, Charlie Robbins & the Contributors
*
*/
-
+
var cliff = require('../lib/cliff');
cliff.putObject({
4 examples/string-object-rows.js
View
@@ -1,10 +1,10 @@
/*
* put-object-rows.js: Example usage for `cliff.putObjectRows`.
*
- * (C) 2010, Nodejitsu Inc.
+ * (C) 2010, Charlie Robbins & the Contributors
*
*/
-
+
var cliff = require('../lib/cliff');
var objs = [], obj = {
145 lib/cliff.js
View
@@ -1,22 +1,45 @@
/*
* cliff.js: CLI output formatting tools: "Your CLI Formatting Friend".
*
- * (C) 2010, Nodejitsu Inc.
+ * (C) 2010, Charlie Robbins & the Contributors
*
*/
-
+
var colors = require('colors'),
eyes = require('eyes'),
winston = require('winston');
-var cliff = exports;
+var cliff = exports,
+ logger;
+
+cliff.__defineGetter__('logger', function () {
+ delete cliff.logger;
+ return cliff.logger = logger;
+});
+
+cliff.__defineSetter__('logger', function (val) {
+ logger = val;
+
+ //
+ // Setup winston to use the `cli` formats
+ //
+ if (logger.cli) {
+ logger.cli();
+ }
+});
//
-// Setup winston to use the `cli` formats
+// Set the default logger for cliff.
//
-winston.cli();
+cliff.logger = new winston.Logger({
+ transports: [new winston.transports.Console()]
+});
-cliff.inspect = eyes.inspector({ stream: null,
+//
+// Expose a default `eyes` inspector.
+//
+cliff.inspector = eyes.inspector;
+cliff.inspect = eyes.inspector({ stream: null,
styles: { // Styles applied to stdout
all: null, // Overall style applied to everything
label: 'underline', // Inspection labels, like 'array' in `array: [1, 2, 3]`
@@ -25,13 +48,13 @@ cliff.inspect = eyes.inspector({ stream: null,
special: 'grey', // null, undefined...
number: 'blue', // 0, 1, 2...
bool: 'magenta', // true false
- regexp: 'green', // /\d+/
+ regexp: 'green' // /\d+/
}
});
//
// ### function extractFrom (obj, properties)
-// #### @obj {Object} Object to extract properties from.
+// #### @obj {Object} Object to extract properties from.
// #### @properties {Array} List of properties to output.
// Creates an array representing the values for `properties` in `obj`.
//
@@ -49,32 +72,30 @@ cliff.extractFrom = function (obj, properties) {
//
cliff.columnMajor = function (rows) {
var columns = [];
-
+
rows.forEach(function (row) {
for (var i = 0; i < row.length; i += 1) {
if (!columns[i]) {
columns[i] = [];
}
-
+
columns[i].push(row[i]);
}
});
-
+
return columns;
};
//
-// ### arrayLengths (arrs)
+// ### arrayLengths (arrs)
// #### @arrs {ArrayxArray} Arrays to calculate lengths for
// Creates an array with values each representing the length
-// of an array in the set provided.
+// of an array in the set provided.
//
cliff.arrayLengths = function (arrs) {
var i, lengths = [];
for (i = 0; i < arrs.length; i += 1) {
- lengths.push(longestElement(arrs[i].map(function(elem) {
- return cliff.stringifyLiteral(elem);
- })));
+ lengths.push(longestElement(arrs[i].map(cliff.stringifyLiteral)));
}
return lengths;
};
@@ -86,36 +107,43 @@ 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(' ');
}
-
+
output.push(rowtext);
}
-
+
// If we were passed colors, then assume the first row
// is the headers for the rows
if (colors) {
headers = rows.splice(0, 1)[0];
stringifyRow(headers, true);
}
-
+
rows.forEach(function (row) {
stringifyRow(row, false);
});
-
+
return output.join('\n');
};
@@ -127,40 +155,40 @@ 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);
};
//
-// ### function putRows (level, rows, colors)
-// #### @level {String} Log-level to use for winston
+// ### function putRows (level, rows, colors)
+// #### @level {String} Log-level to use
// #### @rows {Array} Array of rows to log at the specified level
// #### @colors {Array} Set of colors to use for the specified row(s) headers.
// Logs the stringified table result from `rows` at the appropriate `level` using
-// winston. If `colors` are supplied then use those when stringifying `rows`.
+// `cliff.logger`. If `colors` are supplied then use those when stringifying `rows`.
//
cliff.putRows = function (level, rows, colors) {
cliff.stringifyRows(rows, colors).split('\n').forEach(function (str) {
- winston.log(level, str);
+ logger.log(level, str);
});
};
//
-// ### function putObjectRows (level, rows, colors)
-// #### @level {String} Log-level to use for winston
+// ### function putObjectRows (level, rows, colors)
+// #### @level {String} Log-level to use
// #### @objs {Array} List of objects to create output for
// #### @properties {Array} List of properties to output
// #### @colors {Array} Set of colors to use for the headers
// Logs the stringified table result from `objs` at the appropriate `level` using
-// winston. If `colors` are supplied then use those when stringifying `objs`.
+// `cliff.logger`. If `colors` are supplied then use those when stringifying `objs`.
//
cliff.putObjectRows = function (level, objs, properties, colors) {
cliff.rowifyObjects(objs, properties, colors).split('\n').forEach(function (str) {
- winston.log(level, str);
+ logger.log(level, str);
});
};
@@ -170,7 +198,7 @@ cliff.putObjectRows = function (level, objs, properties, colors) {
// #### @rewriters {Object} **Optional** Set of methods to rewrite certain object keys
// #### @padding {Number} **Optional** Length of padding to put around the output.
// Inspects the object `obj` on the command line rewriting any properties which match
-// keys in `rewriters` if any. Adds additional `padding` if supplied.
+// keys in `rewriters` if any. Adds additional `padding` if supplied.
//
cliff.putObject = function (/*obj, [rewriters, padding] */) {
var args = Array.prototype.slice.call(arguments),
@@ -178,29 +206,29 @@ cliff.putObject = function (/*obj, [rewriters, padding] */) {
padding = typeof args[args.length - 1] === 'number' && args.pop(),
rewriters = typeof args[args.length -1] === 'object' && args.pop(),
keys = Object.keys(obj).sort(),
- sorted = {},
+ sorted = {},
matchers = {},
inspected;
-
+
padding = padding || 0;
rewriters = rewriters || {};
-
+
function pad () {
for (var i = 0; i < padding / 2; i++) {
- winston.data('');
+ logger.data('');
}
}
-
+
keys.forEach(function (key) {
sorted[key] = obj[key];
});
-
+
inspected = cliff.inspect(sorted);
-
+
Object.keys(rewriters).forEach(function (key) {
matchers[key] = new RegExp(key);
});
-
+
pad();
inspected.split('\n').forEach(function (line) {
Object.keys(rewriters).forEach(function (key) {
@@ -208,7 +236,7 @@ cliff.putObject = function (/*obj, [rewriters, padding] */) {
line = rewriters[key](line);
}
});
- winston.data(line);
+ logger.data(line);
});
pad();
};
@@ -223,36 +251,37 @@ cliff.stringifyLiteral = function stringifyLiteral (literal) {
}
};
-cliff.typeOf = function typeOf (value) {
+cliff.typeOf = function typeOf(value) {
var s = typeof(value),
types = [Object, Array, String, RegExp, Number, Function, Boolean, Date];
if (s === 'object' || s === 'function') {
if (value) {
types.forEach(function (t) {
- if (value instanceof t) {
+ if (value instanceof t) {
s = t.name.toLowerCase();
}
});
- } else {
+ } else {
s = 'null';
}
}
-
+
return s;
-}
+};
-var realLength = function (str) {
+function realLength(str) {
return ("" + str).replace(/\u001b\[\d+m/g,'').length;
}
-var longestElement = function (a) {
+function longestElement(a) {
var l = 0;
- for (var i = 0; i < a.length; i++) {
- var new_l = realLength(a[i]);
- if (l < new_l) {
- l = new_l;
- }
+ for (var i = 0; i < a.length; i++) {
+ var new_l = realLength(a[i]);
+ if (l < new_l) {
+ l = new_l;
}
+ }
+
return l;
}
30 package.json
View
@@ -1,22 +1,32 @@
{
"name": "cliff",
"description": "Your CLI formatting friend.",
- "version": "0.1.3",
- "author": "Nodejitsu <info@nodejitsu.com>",
+ "version": "0.1.10",
+ "author": "Charlie Robbins <charlie.robbins@gmail.com>",
"repository": {
"type": "git",
- "url": "http://github.com/nodejitsu/cliff.git"
+ "url": "http://github.com/flatiron/cliff.git"
},
- "keywords": ["cli", "logging", "tools", "winston"],
+ "keywords": [
+ "cli",
+ "logging",
+ "tools",
+ "winston"
+ ],
"dependencies": {
- "colors": "0.x.x",
- "eyes": "0.1.x",
- "winston": "0.3.x"
+ "colors": "~1.0.3",
+ "eyes": "~0.1.8",
+ "winston": "0.8.x"
},
"devDependencies": {
- "vows": "0.5.x"
+ "vows": "0.8.x"
},
"main": "./lib/cliff",
- "scripts": { "test": "vows test/*-test.js --spec" },
- "engines": { "node": ">= 0.4.0" }
+ "scripts": {
+ "test": "vows test/*-test.js --spec"
+ },
+ "engines": {
+ "node": ">= 0.4.0"
+ }
}
+
63 test/cliff-test.js
View
@@ -1,15 +1,15 @@
/*
* log-test.js: Tests for cliff.
*
- * (C) 2010, Nodejitsu Inc.
+ * (C) 2010, Charlie Robbins & the Contributors
*
*/
-
+
var assert = require('assert'),
vows = require('vows'),
eyes = require('eyes'),
cliff = require('../lib/cliff');
-
+
vows.describe('cliff').addBatch({
"When using cliff module": {
"the columnMajor() method": {
@@ -19,7 +19,7 @@ vows.describe('cliff').addBatch({
["1b", "2b", "3b", "4b"],
["1c", "2c", "3c", "4c"]
];
-
+
columns = cliff.columnMajor(rows);
for (var i = 0; i < columns.length; i++) {
columns[i].forEach(function (val) {
@@ -29,19 +29,50 @@ vows.describe('cliff').addBatch({
}
},
"the arrayLengths() method": {
- "should respond with a list of the longest elements": function () {
- var lengths, rows = [
- ["1a", "2a", "3a", "4a"],
- ["1b", "2bb", "3b", "4b"],
- ["1c", "2c", "3ccc", "4c"],
- ["1d", "2d", "3dd", "4dddd"]
+ "with a set of strings": {
+ "should respond with a list of the longest elements": function () {
+ var lengths, rows = [
+ ["1a", "2a", "3a", "4a"],
+ ["1b", "2bb", "3b", "4b"],
+ ["1c", "2c", "3ccc", "4c"],
+ ["1d", "2d", "3dd", "4dddd"]
+ ];
+
+ lengths = cliff.arrayLengths(rows);
+ assert.equal(lengths[0], 2);
+ assert.equal(lengths[1], 3);
+ assert.equal(lengths[2], 4);
+ assert.equal(lengths[3], 5);
+ }
+ },
+ "with a set of numbers and strings": {
+ "should respond with a list of the longest elements": function () {
+ var lengths, rows = [
+ [11, "2a", "3a", "4a"],
+ ["1b", 222, "3b", "4b"],
+ ["1c", "2c", 3333, "4c"],
+ ["1d", "2d", "3dd", 44444]
+ ];
+
+ lengths = cliff.arrayLengths(rows);
+ assert.equal(lengths[0], 2);
+ assert.equal(lengths[1], 3);
+ assert.equal(lengths[2], 4);
+ assert.equal(lengths[3], 5);
+ }
+ }
+ },
+ "the stringifyRows() method": {
+ "should calculate padding correctly for numbers": function() {
+ var rows = [
+ ['a', 'b'],
+ [12345, 1]
];
-
- lengths = cliff.arrayLengths(rows);
- assert.equal(lengths[0], 2);
- assert.equal(lengths[1], 3);
- assert.equal(lengths[2], 4);
- assert.equal(lengths[3], 5);
+
+ assert.equal(
+ cliff.stringifyRows(rows),
+ 'a b \n12345 1 '
+ );
}
}
}
42 test/gh-1-regression-test.js
View
@@ -1,42 +0,0 @@
-/*
- * gh-1-regression-test.js:
- *
- * (C) 2010, Nodejitsu Inc.
- *
- */
-
-var assert = require('assert'),
- vows = require('vows'),
- eyes = require('eyes'),
- cliff = require('../lib/cliff');
-
-vows.describe('cliff/gh-1 regression').addBatch({
- "When using cliff module": {
- "the arrayLengths() method called with numbers in rows": {
- "should respond with a list of the longest elements": function () {
- var lengths, rows = [
- [11, "2a", "3a", "4a"],
- ["1b", 222, "3b", "4b"],
- ["1c", "2c", 3333, "4c"],
- ["1d", "2d", "3dd", 44444]
- ];
-
- lengths = cliff.arrayLengths(rows);
- assert.equal(lengths[0], 2);
- assert.equal(lengths[1], 3);
- assert.equal(lengths[2], 4);
- assert.equal(lengths[3], 5);
- }
- },
- "the stringifyRows() method": {
- "should calculate padding correctly for numbers": function() {
- var rows = [
- ['a', 'b'],
- [12345, 1]
- ],
- output = cliff.stringifyRows(rows);
- assert.equal(output, 'a b \n12345 1 ');
- }
- }
- }
-}).export(module);

No commit comments for this range

Something went wrong with that request. Please try again.