Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKat Marchán <kzm@sykosomatic.org>2018-02-25 08:45:29 +0300
committerRebecca Turner <me@re-becca.org>2018-03-23 12:08:56 +0300
commit9a5807c4f813c49b854170b6111c099b3054faa2 (patch)
tree4f1d3d5140ad573fd4cc7938913fc41ded9edf1c /node_modules/cli-columns/index.js
parentca84be91c434fb7fa472ee4c0b7341414acf52b5 (diff)
cli-columns@3.1.2
PR-URL: https://github.com/npm/npm/pull/19910 Credit: @zkat Reviewed-By: @iarna
Diffstat (limited to 'node_modules/cli-columns/index.js')
-rw-r--r--node_modules/cli-columns/index.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/node_modules/cli-columns/index.js b/node_modules/cli-columns/index.js
new file mode 100644
index 000000000..61efe981a
--- /dev/null
+++ b/node_modules/cli-columns/index.js
@@ -0,0 +1,83 @@
+'use strict';
+
+const stringWidth = require('string-width');
+const stripAnsi = require('strip-ansi');
+
+const concat = Array.prototype.concat;
+const defaults = {
+ character: ' ',
+ newline: '\n',
+ padding: 2,
+ sort: true,
+ width: 0
+};
+
+function byPlainText(a, b) {
+ const plainA = stripAnsi(a);
+ const plainB = stripAnsi(b);
+
+ if (plainA === plainB) {
+ return 0;
+ }
+
+ if (plainA > plainB) {
+ return 1;
+ }
+
+ return -1;
+}
+
+function makeArray() {
+ return [];
+}
+
+function makeList(count) {
+ return Array.apply(null, Array(count));
+}
+
+function padCell(fullWidth, character, value) {
+ const valueWidth = stringWidth(value);
+ const filler = makeList(fullWidth - valueWidth + 1);
+
+ return value + filler.join(character);
+}
+
+function toRows(rows, cell, i) {
+ rows[i % rows.length].push(cell);
+
+ return rows;
+}
+
+function toString(arr) {
+ return arr.join('');
+}
+
+function columns(values, options) {
+ values = concat.apply([], values);
+ options = Object.assign({}, defaults, options);
+
+ let cells = values
+ .filter(Boolean)
+ .map(String);
+
+ if (options.sort !== false) {
+ cells = cells.sort(byPlainText);
+ }
+
+ const termWidth = options.width || process.stdout.columns;
+ const cellWidth = Math.max.apply(null, cells.map(stringWidth)) + options.padding;
+ const columnCount = Math.floor(termWidth / cellWidth) || 1;
+ const rowCount = Math.ceil(cells.length / columnCount) || 1;
+
+ if (columnCount === 1) {
+ return cells.join(options.newline);
+ }
+
+ return cells
+ .map(padCell.bind(null, cellWidth, options.character))
+ .reduce(toRows, makeList(rowCount).map(makeArray))
+ .map(toString)
+ .join(options.newline);
+}
+
+module.exports = columns;