wrap-ansi 2.1.0-candidate → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +90 -103
- package/license +4 -16
- package/package.json +60 -66
- package/readme.md +19 -3
package/index.js
CHANGED
@@ -1,68 +1,42 @@
|
|
1
1
|
'use strict';
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
'\
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
};
|
2
|
+
const stringWidth = require('string-width');
|
3
|
+
const stripAnsi = require('strip-ansi');
|
4
|
+
const ansiStyles = require('ansi-styles');
|
5
|
+
|
6
|
+
const ESCAPES = new Set([
|
7
|
+
'\u001B',
|
8
|
+
'\u009B'
|
9
|
+
]);
|
10
|
+
|
11
|
+
const END_CODE = 39;
|
39
12
|
|
40
|
-
|
41
|
-
return ESCAPES[0] + '[' + code + 'm';
|
42
|
-
}
|
13
|
+
const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
|
43
14
|
|
44
|
-
//
|
45
|
-
// the extra characters added by ansi escape codes
|
46
|
-
|
47
|
-
return str.split(' ').map(function (s) {
|
48
|
-
return stringWidth(s);
|
49
|
-
});
|
50
|
-
}
|
15
|
+
// Calculate the length of words split on ' ', ignoring
|
16
|
+
// the extra characters added by ansi escape codes
|
17
|
+
const wordLengths = string => string.split(' ').map(character => stringWidth(character));
|
51
18
|
|
52
|
-
//
|
53
|
-
//
|
54
|
-
|
55
|
-
|
56
|
-
var visible = stripAnsi(rows[rows.length - 1]).length;
|
19
|
+
// Wrap a long word across multiple rows
|
20
|
+
// Ansi escape codes do not count towards length
|
21
|
+
const wrapWord = (rows, word, columns) => {
|
22
|
+
const characters = [...word];
|
57
23
|
|
58
|
-
|
59
|
-
|
24
|
+
let insideEscape = false;
|
25
|
+
let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
|
60
26
|
|
61
|
-
|
27
|
+
for (const [index, character] of characters.entries()) {
|
28
|
+
const characterLength = stringWidth(character);
|
62
29
|
|
63
|
-
if (
|
30
|
+
if (visible + characterLength <= columns) {
|
31
|
+
rows[rows.length - 1] += character;
|
32
|
+
} else {
|
33
|
+
rows.push(character);
|
34
|
+
visible = 0;
|
35
|
+
}
|
36
|
+
|
37
|
+
if (ESCAPES.has(character)) {
|
64
38
|
insideEscape = true;
|
65
|
-
} else if (insideEscape &&
|
39
|
+
} else if (insideEscape && character === 'm') {
|
66
40
|
insideEscape = false;
|
67
41
|
continue;
|
68
42
|
}
|
@@ -71,98 +45,111 @@ function wrapWord(rows, word, cols) {
|
|
71
45
|
continue;
|
72
46
|
}
|
73
47
|
|
74
|
-
visible
|
48
|
+
visible += characterLength;
|
75
49
|
|
76
|
-
if (visible
|
50
|
+
if (visible === columns && index < characters.length - 1) {
|
77
51
|
rows.push('');
|
78
52
|
visible = 0;
|
79
53
|
}
|
80
54
|
}
|
81
55
|
|
82
|
-
//
|
83
|
-
// ansi escape characters, handle this edge-case
|
56
|
+
// It's possible that the last row we copy over is only
|
57
|
+
// ansi escape characters, handle this edge-case
|
84
58
|
if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
|
85
59
|
rows[rows.length - 2] += rows.pop();
|
86
60
|
}
|
87
|
-
}
|
61
|
+
};
|
88
62
|
|
89
|
-
//
|
90
|
-
// in either 'hard' or 'soft' wrap mode
|
63
|
+
// The wrap-ansi module can be invoked
|
64
|
+
// in either 'hard' or 'soft' wrap mode
|
91
65
|
//
|
92
66
|
// 'hard' will never allow a string to take up more
|
93
|
-
// than
|
67
|
+
// than columns characters
|
94
68
|
//
|
95
|
-
// 'soft' allows long words to expand past the column length
|
96
|
-
|
97
|
-
|
69
|
+
// 'soft' allows long words to expand past the column length
|
70
|
+
const exec = (string, columns, options = {}) => {
|
71
|
+
if (string.trim() === '') {
|
72
|
+
return options.trim === false ? string : string.trim();
|
73
|
+
}
|
98
74
|
|
99
|
-
|
100
|
-
|
101
|
-
|
75
|
+
let pre = '';
|
76
|
+
let ret = '';
|
77
|
+
let escapeCode;
|
102
78
|
|
103
|
-
|
104
|
-
|
105
|
-
var rows = [''];
|
79
|
+
const lengths = wordLengths(string);
|
80
|
+
const rows = [''];
|
106
81
|
|
107
|
-
for (
|
108
|
-
|
82
|
+
for (const [index, word] of string.split(' ').entries()) {
|
83
|
+
rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();
|
84
|
+
let rowLength = stringWidth(rows[rows.length - 1]);
|
85
|
+
|
86
|
+
if (rowLength || word === '') {
|
87
|
+
if (rowLength === columns && options.wordWrap === false) {
|
88
|
+
// If we start with a new word but the current row length equals the length of the columns, add a new row
|
89
|
+
rows.push('');
|
90
|
+
rowLength = 0;
|
91
|
+
}
|
109
92
|
|
110
|
-
if (rowLength) {
|
111
93
|
rows[rows.length - 1] += ' ';
|
112
94
|
rowLength++;
|
113
95
|
}
|
114
96
|
|
115
|
-
//
|
116
|
-
// never allowed to extend past '
|
117
|
-
if (lengths[
|
97
|
+
// In 'hard' wrap mode, the length of a line is
|
98
|
+
// never allowed to extend past 'columns'
|
99
|
+
if (lengths[index] > columns && options.hard) {
|
118
100
|
if (rowLength) {
|
119
101
|
rows.push('');
|
120
102
|
}
|
121
|
-
wrapWord(rows, word,
|
103
|
+
wrapWord(rows, word, columns);
|
122
104
|
continue;
|
123
105
|
}
|
124
106
|
|
125
|
-
if (rowLength + lengths[
|
126
|
-
if (options.wordWrap === false && rowLength <
|
127
|
-
wrapWord(rows, word,
|
107
|
+
if (rowLength + lengths[index] > columns && rowLength > 0) {
|
108
|
+
if (options.wordWrap === false && rowLength < columns) {
|
109
|
+
wrapWord(rows, word, columns);
|
128
110
|
continue;
|
129
111
|
}
|
130
112
|
|
131
113
|
rows.push('');
|
132
114
|
}
|
133
115
|
|
116
|
+
if (rowLength + lengths[index] > columns && options.wordWrap === false) {
|
117
|
+
wrapWord(rows, word, columns);
|
118
|
+
continue;
|
119
|
+
}
|
120
|
+
|
134
121
|
rows[rows.length - 1] += word;
|
135
122
|
}
|
136
123
|
|
137
|
-
pre = rows.map(
|
138
|
-
return r.trim();
|
139
|
-
}).join('\n');
|
140
|
-
|
141
|
-
for (var j = 0; j < pre.length; j++) {
|
142
|
-
var y = pre[j];
|
124
|
+
pre = rows.map(row => options.trim === false ? row : row.trim()).join('\n');
|
143
125
|
|
144
|
-
|
126
|
+
for (const [index, character] of [...pre].entries()) {
|
127
|
+
ret += character;
|
145
128
|
|
146
|
-
if (ESCAPES.
|
147
|
-
|
129
|
+
if (ESCAPES.has(character)) {
|
130
|
+
const code = parseFloat(/\d[^m]*/.exec(pre.slice(index, index + 4)));
|
148
131
|
escapeCode = code === END_CODE ? null : code;
|
149
132
|
}
|
150
133
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
134
|
+
const code = ansiStyles.codes.get(Number(escapeCode));
|
135
|
+
|
136
|
+
if (escapeCode && code) {
|
137
|
+
if (pre[index + 1] === '\n') {
|
138
|
+
ret += wrapAnsi(code);
|
139
|
+
} else if (character === '\n') {
|
155
140
|
ret += wrapAnsi(escapeCode);
|
156
141
|
}
|
157
142
|
}
|
158
143
|
}
|
159
144
|
|
160
145
|
return ret;
|
161
|
-
}
|
146
|
+
};
|
162
147
|
|
163
|
-
//
|
164
|
-
module.exports =
|
165
|
-
return String(
|
166
|
-
|
167
|
-
|
148
|
+
// For each newline, invoke the method separately
|
149
|
+
module.exports = (string, columns, options) => {
|
150
|
+
return String(string)
|
151
|
+
.normalize()
|
152
|
+
.split('\n')
|
153
|
+
.map(line => exec(line, columns, options))
|
154
|
+
.join('\n');
|
168
155
|
};
|
package/license
CHANGED
@@ -1,21 +1,9 @@
|
|
1
|
-
|
1
|
+
MIT License
|
2
2
|
|
3
3
|
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
4
4
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
11
6
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
14
8
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
@@ -1,68 +1,62 @@
|
|
1
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
|
-
"coveralls": "^2.11.4",
|
63
|
-
"has-ansi": "^2.0.0",
|
64
|
-
"nyc": "^6.2.1",
|
65
|
-
"strip-ansi": "^3.0.0",
|
66
|
-
"xo": "*"
|
67
|
-
}
|
2
|
+
"name": "wrap-ansi",
|
3
|
+
"version": "4.0.0",
|
4
|
+
"description": "Wordwrap a string with ANSI escape codes",
|
5
|
+
"license": "MIT",
|
6
|
+
"repository": "chalk/wrap-ansi",
|
7
|
+
"author": {
|
8
|
+
"name": "Sindre Sorhus",
|
9
|
+
"email": "sindresorhus@gmail.com",
|
10
|
+
"url": "sindresorhus.com"
|
11
|
+
},
|
12
|
+
"engines": {
|
13
|
+
"node": ">=6"
|
14
|
+
},
|
15
|
+
"scripts": {
|
16
|
+
"test": "xo && nyc ava"
|
17
|
+
},
|
18
|
+
"files": [
|
19
|
+
"index.js"
|
20
|
+
],
|
21
|
+
"keywords": [
|
22
|
+
"wrap",
|
23
|
+
"break",
|
24
|
+
"wordwrap",
|
25
|
+
"wordbreak",
|
26
|
+
"linewrap",
|
27
|
+
"ansi",
|
28
|
+
"styles",
|
29
|
+
"color",
|
30
|
+
"colour",
|
31
|
+
"colors",
|
32
|
+
"terminal",
|
33
|
+
"console",
|
34
|
+
"cli",
|
35
|
+
"string",
|
36
|
+
"tty",
|
37
|
+
"escape",
|
38
|
+
"formatting",
|
39
|
+
"rgb",
|
40
|
+
"256",
|
41
|
+
"shell",
|
42
|
+
"xterm",
|
43
|
+
"log",
|
44
|
+
"logging",
|
45
|
+
"command-line",
|
46
|
+
"text"
|
47
|
+
],
|
48
|
+
"dependencies": {
|
49
|
+
"ansi-styles": "^3.2.0",
|
50
|
+
"string-width": "^2.1.1",
|
51
|
+
"strip-ansi": "^4.0.0"
|
52
|
+
},
|
53
|
+
"devDependencies": {
|
54
|
+
"ava": "^0.25.0",
|
55
|
+
"chalk": "^2.0.1",
|
56
|
+
"coveralls": "^3.0.0",
|
57
|
+
"has-ansi": "^3.0.0",
|
58
|
+
"nyc": "^13.0.1",
|
59
|
+
"strip-ansi": "^4.0.0",
|
60
|
+
"xo": "^0.22.0"
|
61
|
+
}
|
68
62
|
}
|
package/readme.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# wrap-ansi [](https://travis-ci.org/chalk/wrap-ansi) [](https://coveralls.io/github/chalk/wrap-ansi?branch=master)
|
2
2
|
|
3
|
-
> Wordwrap a string with [ANSI escape codes](
|
3
|
+
> Wordwrap a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
|
4
4
|
|
5
5
|
|
6
6
|
## Install
|
7
7
|
|
8
8
|
```
|
9
|
-
$ npm install
|
9
|
+
$ npm install wrap-ansi
|
10
10
|
```
|
11
11
|
|
12
12
|
|
@@ -45,6 +45,8 @@ Number of columns to wrap the text to.
|
|
45
45
|
|
46
46
|
#### options
|
47
47
|
|
48
|
+
Type: `Object`
|
49
|
+
|
48
50
|
##### hard
|
49
51
|
|
50
52
|
Type: `boolean`<br>
|
@@ -59,6 +61,13 @@ Default: `true`
|
|
59
61
|
|
60
62
|
By default, an attempt is made to split words at spaces, ensuring that they don't extend past the configured columns. If wordWrap is `false`, each column will instead be completely filled splitting words as necessary.
|
61
63
|
|
64
|
+
##### trim
|
65
|
+
|
66
|
+
Type: `boolean`<br>
|
67
|
+
Default: `true`
|
68
|
+
|
69
|
+
Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim.
|
70
|
+
|
62
71
|
|
63
72
|
## Related
|
64
73
|
|
@@ -68,6 +77,13 @@ By default, an attempt is made to split words at spaces, ensuring that they don'
|
|
68
77
|
- [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures.
|
69
78
|
|
70
79
|
|
80
|
+
## Maintainers
|
81
|
+
|
82
|
+
- [Sindre Sorhus](https://github.com/sindresorhus)
|
83
|
+
- [Josh Junon](https://github.com/qix-)
|
84
|
+
- [Benjamin Coe](https://github.com/bcoe)
|
85
|
+
|
86
|
+
|
71
87
|
## License
|
72
88
|
|
73
|
-
MIT
|
89
|
+
MIT
|