pixl-cli 1.0.16 → 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/cli.js +72 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -424,6 +424,7 @@ Of course, if your Terminal supports ANSI color and font styles, the header woul
|
|
|
424
424
|
| `textStyles` | An array of [chalk](https://www.npmjs.com/package/chalk) styles or functions to apply to the table cell text. |
|
|
425
425
|
| `borderStyles` | An array of [chalk](https://www.npmjs.com/package/chalk) styles or functions to apply to the border graphics. |
|
|
426
426
|
| `indent` | Number of characters to indent the table by (defaults to `0`). |
|
|
427
|
+
| `autoFit` | Automatically "fit" table into the available terminal width, if it is too wide. This will add ellipsis to longer columns as required. |
|
|
427
428
|
|
|
428
429
|
Here is an example specifying all the possible options:
|
|
429
430
|
|
|
@@ -433,7 +434,8 @@ cli.print(
|
|
|
433
434
|
headerStyles: ["bold", "yellow"],
|
|
434
435
|
textStyles: ["cyan", function( text ) { return text.toUpperCase() }],
|
|
435
436
|
borderStyles: ["gray"],
|
|
436
|
-
indent: 0
|
|
437
|
+
indent: 0,
|
|
438
|
+
autoFit: true
|
|
437
439
|
}) + "\n"
|
|
438
440
|
);
|
|
439
441
|
```
|
package/cli.js
CHANGED
|
@@ -234,6 +234,76 @@ var cli = module.exports = {
|
|
|
234
234
|
return output.length ? output.join("\n") : "";
|
|
235
235
|
},
|
|
236
236
|
|
|
237
|
+
autoFitTableRows: function(rows, args) {
|
|
238
|
+
// add ellipsis to rows as needed to fit entire table into horiz terminal width
|
|
239
|
+
var self = this;
|
|
240
|
+
var avail_width = (this.width() - (stringWidth(args.indent) * 2));
|
|
241
|
+
if (avail_width < 1) return;
|
|
242
|
+
|
|
243
|
+
var max_col_widths = [];
|
|
244
|
+
rows.forEach( function(cols, idx) {
|
|
245
|
+
cols.forEach( function(col, idy) {
|
|
246
|
+
max_col_widths[idy] = Math.max( max_col_widths[idy] || 0, stringWidth(''+col) );
|
|
247
|
+
} );
|
|
248
|
+
} );
|
|
249
|
+
|
|
250
|
+
var measureTableWidth = function() {
|
|
251
|
+
// measure table width with current settings and max_col_widths "virtually" applied
|
|
252
|
+
var widestCols = [];
|
|
253
|
+
rows.forEach( function(cols, idx) {
|
|
254
|
+
cols.forEach( function(col, idy) {
|
|
255
|
+
var sw = Math.min( stringWidth(''+col), max_col_widths[idy] );
|
|
256
|
+
widestCols[idy] = Math.max( widestCols[idy] || 0, sw + 2 );
|
|
257
|
+
} );
|
|
258
|
+
} );
|
|
259
|
+
|
|
260
|
+
var numCols = widestCols.length;
|
|
261
|
+
var line = "┌";
|
|
262
|
+
widestCols.forEach( function(num, idx) {
|
|
263
|
+
line += self.repeat("─", num);
|
|
264
|
+
if (idx < numCols - 1) line += "┬";
|
|
265
|
+
} );
|
|
266
|
+
line += "┐";
|
|
267
|
+
|
|
268
|
+
return line.length;
|
|
269
|
+
}; // measureTableWidth
|
|
270
|
+
|
|
271
|
+
// now keep chopping down max_col_widths until we fit
|
|
272
|
+
while (measureTableWidth() > avail_width) {
|
|
273
|
+
// find largest max_col_widths and decrement it by 1
|
|
274
|
+
var longest_col_width = Math.max.apply( Math, max_col_widths );
|
|
275
|
+
if (longest_col_width < 2) return; // e-brake
|
|
276
|
+
|
|
277
|
+
var longest_col_idx = max_col_widths.indexOf(longest_col_width);
|
|
278
|
+
if (longest_col_idx == -1) return; // sanity
|
|
279
|
+
|
|
280
|
+
max_col_widths[longest_col_idx]--;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// finally prune affected columns, trying to preserve ANSI color inside column value
|
|
284
|
+
rows.forEach( function(cols, idx) {
|
|
285
|
+
cols.forEach( function(col, idy) {
|
|
286
|
+
col = '' + col;
|
|
287
|
+
if (stringWidth(col) > max_col_widths[idy]) {
|
|
288
|
+
var suffix = '';
|
|
289
|
+
var prefix = '';
|
|
290
|
+
|
|
291
|
+
while (col.match(/^(\u001b\[[^m]*?m)/)) {
|
|
292
|
+
prefix += RegExp.$1;
|
|
293
|
+
col = col.replace(/^(\u001b\[[^m]*?m)/, '');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
while (col.match(/(\u001b\[[^m]*?m)$/)) {
|
|
297
|
+
suffix = RegExp.$1 + suffix;
|
|
298
|
+
col = col.replace(/(\u001b\[[^m]*?m)$/, '');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
cols[idy] = prefix + col.substring(0, max_col_widths[idy] - 1) + '…' + suffix;
|
|
302
|
+
} // too wide
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
|
|
237
307
|
table: function(rows, args) {
|
|
238
308
|
// render table of cols/rows with unicode borders
|
|
239
309
|
// rows should be an array of arrays (columns), with row 0 being the header
|
|
@@ -247,6 +317,8 @@ var cli = module.exports = {
|
|
|
247
317
|
args.indent = args.indent || "";
|
|
248
318
|
if (typeof(args.indent) == 'number') args.indent = cli.space(args.indent);
|
|
249
319
|
|
|
320
|
+
if (args.autoFit) this.autoFitTableRows(rows, args);
|
|
321
|
+
|
|
250
322
|
// calculate widest columns (+1spc of hpadding)
|
|
251
323
|
var widestCols = [];
|
|
252
324
|
rows.forEach( function(cols, idx) {
|
package/package.json
CHANGED