cliff.js
7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* cliff.js: CLI output formatting tools: "Your CLI Formatting Friend".
*
* (C) 2010, Charlie Robbins & the Contributors
*
*/
var colors = require('colors'),
eyes = require('eyes'),
winston = require('winston');
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();
}
});
//
// Set the default logger for cliff.
//
cliff.logger = new winston.Logger({
transports: [new winston.transports.Console()]
});
//
// 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]`
other: 'inverted', // Objects which don't have a literal representation, such as functions
key: 'grey', // The keys in object literals, like 'a' in `{a: 1}`
special: 'grey', // null, undefined...
number: 'blue', // 0, 1, 2...
bool: 'magenta', // true false
regexp: 'green' // /\d+/
}
});
//
// ### function extractFrom (obj, properties)
// #### @obj {Object} Object to extract properties from.
// #### @properties {Array} List of properties to output.
// Creates an array representing the values for `properties` in `obj`.
//
cliff.extractFrom = function (obj, properties) {
return properties.map(function (p) {
return obj[p];
});
};
//
// ### function columnMajor (rows)
// #### @rows {ArrayxArray} Row-major Matrix to transpose
// Transposes the row-major Matrix, represented as an array of rows,
// into column major form (i.e. an array of columns).
//
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)
// #### @arrs {ArrayxArray} Arrays to calculate lengths for
// Creates an array with values each representing the length
// 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(cliff.stringifyLiteral)));
}
return lengths;
};
//
// ### function stringifyRows (rows, colors)
// #### @rows {ArrayxArray} Matrix of properties to output in row major form
// #### @colors {Array} Set of colors to use for the headers
// Outputs the specified `rows` as fixed-width columns, adding
// colorized headers if `colors` are supplied.
//
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]);
if(colorize) {
item = item[colors[i]] || item[colors[colors.length -1]] || item;
}
length = realLength(item);
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');
};
//
// ### function rowifyObjects (objs, properties, colors)
// #### @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
// Extracts the lists of `properties` from the specified `objs`
// and formats them according to `cliff.stringifyRows`.
//
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, options);
};
//
// ### 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
// `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) {
logger.log(level, str);
});
};
//
// ### 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
// `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) {
logger.log(level, str);
});
};
//
// ### function putObject (obj, [rewriters, padding])
// #### @obj {Object} Object to log to the command line
// #### @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.
//
cliff.putObject = function (/*obj, [rewriters, padding] */) {
var args = Array.prototype.slice.call(arguments),
obj = args.shift(),
padding = typeof args[args.length - 1] === 'number' && args.pop(),
rewriters = typeof args[args.length -1] === 'object' && args.pop(),
keys = Object.keys(obj).sort(),
sorted = {},
matchers = {},
inspected;
padding = padding || 0;
rewriters = rewriters || {};
function pad () {
for (var i = 0; i < padding / 2; i++) {
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) {
if (matchers[key].test(line)) {
line = rewriters[key](line);
}
});
logger.data(line);
});
pad();
};
cliff.stringifyLiteral = function stringifyLiteral (literal) {
switch (cliff.typeOf(literal)) {
case 'number' : return literal + '';
case 'null' : return 'null';
case 'undefined': return 'undefined';
case 'boolean' : return literal + '';
default : return literal;
}
};
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) {
s = t.name.toLowerCase();
}
});
} else {
s = 'null';
}
}
return s;
};
function realLength(str) {
return ("" + str).replace(/\u001b\[\d+m/g,'').length;
}
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;
}
}
return l;
}