Comparing version 1.0.16 to 1.0.17
72
cli.js
@@ -237,2 +237,72 @@ // Tools for writing command-line apps in Node. | ||
autoFitTableRows: function(rows, args) { | ||
// add ellipsis to rows as needed to fit entire table into horiz terminal width | ||
var self = this; | ||
var avail_width = (this.width() - (stringWidth(args.indent) * 2)); | ||
if (avail_width < 1) return; | ||
var max_col_widths = []; | ||
rows.forEach( function(cols, idx) { | ||
cols.forEach( function(col, idy) { | ||
max_col_widths[idy] = Math.max( max_col_widths[idy] || 0, stringWidth(''+col) ); | ||
} ); | ||
} ); | ||
var measureTableWidth = function() { | ||
// measure table width with current settings and max_col_widths "virtually" applied | ||
var widestCols = []; | ||
rows.forEach( function(cols, idx) { | ||
cols.forEach( function(col, idy) { | ||
var sw = Math.min( stringWidth(''+col), max_col_widths[idy] ); | ||
widestCols[idy] = Math.max( widestCols[idy] || 0, sw + 2 ); | ||
} ); | ||
} ); | ||
var numCols = widestCols.length; | ||
var line = "┌"; | ||
widestCols.forEach( function(num, idx) { | ||
line += self.repeat("─", num); | ||
if (idx < numCols - 1) line += "┬"; | ||
} ); | ||
line += "┐"; | ||
return line.length; | ||
}; // measureTableWidth | ||
// now keep chopping down max_col_widths until we fit | ||
while (measureTableWidth() > avail_width) { | ||
// find largest max_col_widths and decrement it by 1 | ||
var longest_col_width = Math.max.apply( Math, max_col_widths ); | ||
if (longest_col_width < 2) return; // e-brake | ||
var longest_col_idx = max_col_widths.indexOf(longest_col_width); | ||
if (longest_col_idx == -1) return; // sanity | ||
max_col_widths[longest_col_idx]--; | ||
} | ||
// finally prune affected columns, trying to preserve ANSI color inside column value | ||
rows.forEach( function(cols, idx) { | ||
cols.forEach( function(col, idy) { | ||
col = '' + col; | ||
if (stringWidth(col) > max_col_widths[idy]) { | ||
var suffix = ''; | ||
var prefix = ''; | ||
while (col.match(/^(\u001b\[[^m]*?m)/)) { | ||
prefix += RegExp.$1; | ||
col = col.replace(/^(\u001b\[[^m]*?m)/, ''); | ||
} | ||
while (col.match(/(\u001b\[[^m]*?m)$/)) { | ||
suffix = RegExp.$1 + suffix; | ||
col = col.replace(/(\u001b\[[^m]*?m)$/, ''); | ||
} | ||
cols[idy] = prefix + col.substring(0, max_col_widths[idy] - 1) + '…' + suffix; | ||
} // too wide | ||
}); | ||
}); | ||
}, | ||
table: function(rows, args) { | ||
@@ -251,2 +321,4 @@ // render table of cols/rows with unicode borders | ||
if (args.autoFit) this.autoFitTableRows(rows, args); | ||
// calculate widest columns (+1spc of hpadding) | ||
@@ -253,0 +325,0 @@ var widestCols = []; |
{ | ||
"name": "pixl-cli", | ||
"version": "1.0.16", | ||
"version": "1.0.17", | ||
"description": "Tools for building command-line apps for Node.js.", | ||
@@ -5,0 +5,0 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", |
@@ -427,2 +427,3 @@ <details><summary>Table of Contents</summary> | ||
| `indent` | Number of characters to indent the table by (defaults to `0`). | | ||
| `autoFit` | Automatically "fit" table into the available terminal width, if it is too wide. This will add ellipsis to longer columns as required. | | ||
@@ -437,3 +438,4 @@ Here is an example specifying all the possible options: | ||
borderStyles: ["gray"], | ||
indent: 0 | ||
indent: 0, | ||
autoFit: true | ||
}) + "\n" | ||
@@ -440,0 +442,0 @@ ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53258
592
698