1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
var stringWidth = require('string-width');
|
3 |
|
|
var stripAnsi = require('strip-ansi');
|
4 |
|
|
|
5 |
|
|
var ESCAPES = [
|
6 |
|
|
'\u001b',
|
7 |
|
|
'\u009b'
|
8 |
|
|
];
|
9 |
|
|
|
10 |
|
|
var END_CODE = 39;
|
11 |
|
|
|
12 |
|
|
var ESCAPE_CODES = {
|
13 |
|
|
0: 0,
|
14 |
|
|
1: 22,
|
15 |
|
|
2: 22,
|
16 |
|
|
3: 23,
|
17 |
|
|
4: 24,
|
18 |
|
|
7: 27,
|
19 |
|
|
8: 28,
|
20 |
|
|
9: 29,
|
21 |
|
|
30: 39,
|
22 |
|
|
31: 39,
|
23 |
|
|
32: 39,
|
24 |
|
|
33: 39,
|
25 |
|
|
34: 39,
|
26 |
|
|
35: 39,
|
27 |
|
|
36: 39,
|
28 |
|
|
37: 39,
|
29 |
|
|
90: 39,
|
30 |
|
|
40: 49,
|
31 |
|
|
41: 49,
|
32 |
|
|
42: 49,
|
33 |
|
|
43: 49,
|
34 |
|
|
44: 49,
|
35 |
|
|
45: 49,
|
36 |
|
|
46: 49,
|
37 |
|
|
47: 49
|
38 |
|
|
};
|
39 |
|
|
|
40 |
|
|
function wrapAnsi(code) {
|
41 |
|
|
return ESCAPES[0] + '[' + code + 'm';
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
// calculate the length of words split on ' ', ignoring
|
45 |
|
|
// the extra characters added by ansi escape codes.
|
46 |
|
|
function wordLengths(str) {
|
47 |
|
|
return str.split(' ').map(function (s) {
|
48 |
|
|
return stringWidth(s);
|
49 |
|
|
});
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
// wrap a long word across multiple rows.
|
53 |
|
|
// ansi escape codes do not count towards length.
|
54 |
|
|
function wrapWord(rows, word, cols) {
|
55 |
|
|
var insideEscape = false;
|
56 |
|
|
var visible = stripAnsi(rows[rows.length - 1]).length;
|
57 |
|
|
|
58 |
|
|
for (var i = 0; i < word.length; i++) {
|
59 |
|
|
var x = word[i];
|
60 |
|
|
|
61 |
|
|
rows[rows.length - 1] += x;
|
62 |
|
|
|
63 |
|
|
if (ESCAPES.indexOf(x) !== -1) {
|
64 |
|
|
insideEscape = true;
|
65 |
|
|
} else if (insideEscape && x === 'm') {
|
66 |
|
|
insideEscape = false;
|
67 |
|
|
continue;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
if (insideEscape) {
|
71 |
|
|
continue;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
visible++;
|
75 |
|
|
|
76 |
|
|
if (visible >= cols && i < word.length - 1) {
|
77 |
|
|
rows.push('');
|
78 |
|
|
visible = 0;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
// it's possible that the last row we copy over is only
|
83 |
|
|
// ansi escape characters, handle this edge-case.
|
84 |
|
|
if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
|
85 |
|
|
rows[rows.length - 2] += rows.pop();
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
// the wrap-ansi module can be invoked
|
90 |
|
|
// in either 'hard' or 'soft' wrap mode.
|
91 |
|
|
//
|
92 |
|
|
// 'hard' will never allow a string to take up more
|
93 |
|
|
// than cols characters.
|
94 |
|
|
//
|
95 |
|
|
// 'soft' allows long words to expand past the column length.
|
96 |
|
|
function exec(str, cols, opts) {
|
97 |
|
|
var options = opts || {};
|
98 |
|
|
|
99 |
|
|
var pre = '';
|
100 |
|
|
var ret = '';
|
101 |
|
|
var escapeCode;
|
102 |
|
|
|
103 |
|
|
var lengths = wordLengths(str);
|
104 |
|
|
var words = str.split(' ');
|
105 |
|
|
var rows = [''];
|
106 |
|
|
|
107 |
|
|
for (var i = 0, word; (word = words[i]) !== undefined; i++) {
|
108 |
|
|
var rowLength = stringWidth(rows[rows.length - 1]);
|
109 |
|
|
|
110 |
|
|
if (rowLength) {
|
111 |
|
|
rows[rows.length - 1] += ' ';
|
112 |
|
|
rowLength++;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
// in 'hard' wrap mode, the length of a line is
|
116 |
|
|
// never allowed to extend past 'cols'.
|
117 |
|
|
if (lengths[i] > cols && options.hard) {
|
118 |
|
|
if (rowLength) {
|
119 |
|
|
rows.push('');
|
120 |
|
|
}
|
121 |
|
|
wrapWord(rows, word, cols);
|
122 |
|
|
continue;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
if (rowLength + lengths[i] > cols && rowLength > 0) {
|
126 |
|
|
if (options.wordWrap === false && rowLength < cols) {
|
127 |
|
|
wrapWord(rows, word, cols);
|
128 |
|
|
continue;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
rows.push('');
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
rows[rows.length - 1] += word;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
pre = rows.map(function (r) {
|
138 |
|
|
return r.trim();
|
139 |
|
|
}).join('\n');
|
140 |
|
|
|
141 |
|
|
for (var j = 0; j < pre.length; j++) {
|
142 |
|
|
var y = pre[j];
|
143 |
|
|
|
144 |
|
|
ret += y;
|
145 |
|
|
|
146 |
|
|
if (ESCAPES.indexOf(y) !== -1) {
|
147 |
|
|
var code = parseFloat(/[0-9][^m]*/.exec(pre.slice(j, j + 4)));
|
148 |
|
|
escapeCode = code === END_CODE ? null : code;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
if (escapeCode && ESCAPE_CODES[escapeCode]) {
|
152 |
|
|
if (pre[j + 1] === '\n') {
|
153 |
|
|
ret += wrapAnsi(ESCAPE_CODES[escapeCode]);
|
154 |
|
|
} else if (y === '\n') {
|
155 |
|
|
ret += wrapAnsi(escapeCode);
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
return ret;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
// for each line break, invoke the method separately.
|
164 |
|
|
module.exports = function (str, cols, opts) {
|
165 |
|
|
return String(str).split('\n').map(function (substr) {
|
166 |
|
|
return exec(substr, cols, opts);
|
167 |
|
|
}).join('\n');
|
168 |
|
|
};
|