UNPKG

2.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const indent = require("indent-string");
4const stripAnsi = require("strip-ansi");
5const width = require('string-width');
6const wrap = require('wrap-ansi');
7const widestLine = require('widest-line');
8function renderList(input, opts) {
9 if (input.length === 0) {
10 return '';
11 }
12 const renderMultiline = () => {
13 let output = '';
14 for (let [left, right] of input) {
15 if (!left && !right)
16 continue;
17 if (left) {
18 if (opts.stripAnsi)
19 left = stripAnsi(left);
20 output += wrap(left.trim(), opts.maxWidth, { hard: true, trim: false });
21 }
22 if (right) {
23 if (opts.stripAnsi)
24 right = stripAnsi(right);
25 output += '\n';
26 output += indent(wrap(right.trim(), opts.maxWidth - 2, { hard: true, trim: false }), 4);
27 }
28 output += '\n\n';
29 }
30 return output.trim();
31 };
32 if (opts.multiline)
33 return renderMultiline();
34 const maxLength = widestLine(input.map(i => i[0]).join('\n'));
35 let output = '';
36 let spacer = opts.spacer || '\n';
37 let cur = '';
38 for (const [left, r] of input) {
39 let right = r;
40 if (cur) {
41 output += spacer;
42 output += cur;
43 }
44 cur = left || '';
45 if (opts.stripAnsi)
46 cur = stripAnsi(cur);
47 if (!right) {
48 cur = cur.trim();
49 continue;
50 }
51 if (opts.stripAnsi)
52 right = stripAnsi(right);
53 right = wrap(right.trim(), opts.maxWidth - (maxLength + 2), { hard: true, trim: false });
54 // right = wrap(right.trim(), screen.stdtermwidth - (maxLength + 4), {hard: true, trim: false})
55 const [first, ...lines] = right.split('\n').map(s => s.trim());
56 cur += ' '.repeat(maxLength - width(cur) + 2);
57 cur += first;
58 if (lines.length === 0) {
59 continue;
60 }
61 // if we start putting too many lines down, render in multiline format
62 if (lines.length > 4)
63 return renderMultiline();
64 // if spacer is not defined, separate all rows with extra newline
65 if (!opts.spacer)
66 spacer = '\n\n';
67 cur += '\n';
68 cur += indent(lines.join('\n'), maxLength + 2);
69 }
70 if (cur) {
71 output += spacer;
72 output += cur;
73 }
74 return output.trim();
75}
76exports.renderList = renderList;