UNPKG

4.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.columnar = exports.generateFillSpaceStringList = exports.expandPath = exports.prettyPath = exports.wordWrap = exports.indent = exports.TTY_WIDTH = exports.stripAnsi = exports.stringWidth = exports.sliceAnsi = void 0;
4const tslib_1 = require("tslib");
5const os = tslib_1.__importStar(require("os"));
6const path = tslib_1.__importStar(require("path"));
7const sliceAnsi = require("slice-ansi");
8exports.sliceAnsi = sliceAnsi;
9const stringWidth = require("string-width");
10exports.stringWidth = stringWidth;
11const stripAnsi = require("strip-ansi");
12exports.stripAnsi = stripAnsi;
13const wrapAnsi = require("wrap-ansi");
14const untildify = require("untildify");
15const MIN_TTY_WIDTH = 80;
16const MAX_TTY_WIDTH = 120;
17exports.TTY_WIDTH = process.stdout.columns ? Math.max(MIN_TTY_WIDTH, Math.min(process.stdout.columns, MAX_TTY_WIDTH)) : Infinity;
18function indent(n = 4) {
19 return ' '.repeat(n);
20}
21exports.indent = indent;
22function wordWrap(msg, { width = exports.TTY_WIDTH, indentation = 0, append = '' }) {
23 return wrapAnsi(msg, width - indentation - append.length, { trim: true }).split('\n').join(`${append}\n${indent(indentation)}`);
24}
25exports.wordWrap = wordWrap;
26function prettyPath(p) {
27 p = expandPath(p);
28 const cwd = process.cwd();
29 const d = path.dirname(p);
30 const h = os.homedir();
31 const distanceFromCwd = Math.abs(d.split(path.sep).length - cwd.split(path.sep).length);
32 if (cwd === d) {
33 return '.' + path.sep + path.basename(p);
34 }
35 else if (d.startsWith(cwd)) {
36 return '.' + path.sep + p.substring(cwd.length + 1);
37 }
38 else if (distanceFromCwd <= 2) {
39 const rel = path.relative(cwd, p);
40 return rel ? rel : '.';
41 }
42 else if (p === h) {
43 return '~';
44 }
45 else if (p.indexOf(h) === 0) {
46 return '~' + path.sep + p.substring(h.length + 1);
47 }
48 return p;
49}
50exports.prettyPath = prettyPath;
51function expandPath(p) {
52 return path.resolve(untildify(p));
53}
54exports.expandPath = expandPath;
55function generateFillSpaceStringList(list, optimalLength = 1, fillCharacter = ' ') {
56 if (optimalLength < 2) {
57 optimalLength = 2;
58 }
59 const longestItem = Math.max(...list.map(item => stringWidth(item)));
60 const fullLength = longestItem > optimalLength ? longestItem + 1 : optimalLength;
61 const fullLengthString = fillCharacter.repeat(fullLength);
62 return list.map(item => sliceAnsi(fullLengthString, 0, fullLength - stringWidth(item)));
63}
64exports.generateFillSpaceStringList = generateFillSpaceStringList;
65/**
66 * Basic CLI table generator with support for ANSI colors.
67 *
68 * @param rows 2-dimensional matrix containing cells. An array of columns,
69 * which are arrays of cells.
70 * @param options.vsep The vertical separator character, default is
71 * `chalk.dim('|')`. Supply an empty string to hide
72 * the separator altogether.
73 * @param options.hsep The horizontal separator character, default is
74 * `chalk.dim('-')`. This is used under the headers,
75 * if supplied. Supply an empty string to hide the
76 * separator altogether.
77 * @param options.headers An array of header cells.
78 */
79function columnar(rows, { hsep = '-', vsep = '|', headers }) {
80 const includeHeaders = headers ? true : false;
81 if (!rows[0]) {
82 return '';
83 }
84 const columnCount = headers ? headers.length : rows[0].length;
85 const columns = headers ?
86 headers.map(header => [header]) :
87 rows[0].map(() => []);
88 for (const row of rows) {
89 let highestLineCount = 0;
90 const splitRows = row.map(cell => {
91 const lines = cell.split('\n');
92 highestLineCount = Math.max(highestLineCount, lines.length);
93 return lines;
94 });
95 for (const rowIndex in row) {
96 if (columns[rowIndex]) {
97 columns[rowIndex].push(...splitRows[rowIndex], ...Array(highestLineCount - splitRows[rowIndex].length).fill(''));
98 }
99 }
100 }
101 const paddedColumns = columns.map((col, columnIndex) => {
102 if (columnIndex < columnCount - 1) {
103 const spaceCol = generateFillSpaceStringList(col);
104 return col.map((cell, cellIndex) => `${cell}${spaceCol[cellIndex]}${vsep === '' ? '' : `${vsep} `}`);
105 }
106 else {
107 return col;
108 }
109 });
110 let longestRowLength = 0;
111 const singleColumn = paddedColumns.reduce((a, b) => {
112 return a.map((_, i) => {
113 const r = a[i] + b[i];
114 longestRowLength = Math.max(longestRowLength, stringWidth(r));
115 return r;
116 });
117 });
118 if (includeHeaders && hsep !== '') {
119 singleColumn.splice(1, 0, hsep.repeat(longestRowLength));
120 }
121 return singleColumn.join('\n');
122}
123exports.columnar = columnar;