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