1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.columnar = exports.generateFillSpaceStringList = exports.expandPath = exports.prettyPath = exports.wordWrap = exports.indent = exports.TTY_WIDTH = exports.stripAnsi = exports.stringWidth = exports.sliceAnsi = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const os = tslib_1.__importStar(require("os"));
|
6 | const path = tslib_1.__importStar(require("path"));
|
7 | const sliceAnsi = require("slice-ansi");
|
8 | exports.sliceAnsi = sliceAnsi;
|
9 | const stringWidth = require("string-width");
|
10 | exports.stringWidth = stringWidth;
|
11 | const stripAnsi = require("strip-ansi");
|
12 | exports.stripAnsi = stripAnsi;
|
13 | const wrapAnsi = require("wrap-ansi");
|
14 | const untildify = require("untildify");
|
15 | const MIN_TTY_WIDTH = 80;
|
16 | const MAX_TTY_WIDTH = 120;
|
17 | exports.TTY_WIDTH = process.stdout.columns ? Math.max(MIN_TTY_WIDTH, Math.min(process.stdout.columns, MAX_TTY_WIDTH)) : Infinity;
|
18 | function indent(n = 4) {
|
19 | return ' '.repeat(n);
|
20 | }
|
21 | exports.indent = indent;
|
22 | function 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 | }
|
25 | exports.wordWrap = wordWrap;
|
26 | function 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 | }
|
50 | exports.prettyPath = prettyPath;
|
51 | function expandPath(p) {
|
52 | return path.resolve(untildify(p));
|
53 | }
|
54 | exports.expandPath = expandPath;
|
55 | function 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 | }
|
64 | exports.generateFillSpaceStringList = generateFillSpaceStringList;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function 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 | }
|
123 | exports.columnar = columnar;
|