wrap-ansi 0.3.0 → 2.1.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 +16 -16
- package/package.json +8 -6
- package/readme.md +14 -4
package/index.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
'use strict';
|
2
2
|
var stringWidth = require('string-width');
|
3
|
+
var stripAnsi = require('strip-ansi');
|
3
4
|
|
4
5
|
var ESCAPES = [
|
5
6
|
'\u001b',
|
@@ -52,7 +53,7 @@ function wordLengths(str) {
|
|
52
53
|
// ansi escape codes do not count towards length.
|
53
54
|
function wrapWord(rows, word, cols) {
|
54
55
|
var insideEscape = false;
|
55
|
-
var visible = rows[rows.length - 1].length;
|
56
|
+
var visible = stripAnsi(rows[rows.length - 1]).length;
|
56
57
|
|
57
58
|
for (var i = 0; i < word.length; i++) {
|
58
59
|
var x = word[i];
|
@@ -92,14 +93,12 @@ function wrapWord(rows, word, cols) {
|
|
92
93
|
// than cols characters.
|
93
94
|
//
|
94
95
|
// 'soft' allows long words to expand past the column length.
|
95
|
-
|
96
|
+
function exec(str, cols, opts) {
|
96
97
|
var options = opts || {};
|
97
98
|
|
98
99
|
var pre = '';
|
99
100
|
var ret = '';
|
100
|
-
var insideEscape = false;
|
101
101
|
var escapeCode;
|
102
|
-
var visible = 0;
|
103
102
|
|
104
103
|
var lengths = wordLengths(str);
|
105
104
|
var words = str.split(' ');
|
@@ -123,7 +122,12 @@ module.exports = function (str, cols, opts) {
|
|
123
122
|
continue;
|
124
123
|
}
|
125
124
|
|
126
|
-
if (rowLength + lengths[i] > cols) {
|
125
|
+
if (rowLength + lengths[i] > cols && rowLength > 0) {
|
126
|
+
if (options.wordWrap === false && rowLength < cols) {
|
127
|
+
wrapWord(rows, word, cols);
|
128
|
+
continue;
|
129
|
+
}
|
130
|
+
|
127
131
|
rows.push('');
|
128
132
|
}
|
129
133
|
|
@@ -134,8 +138,6 @@ module.exports = function (str, cols, opts) {
|
|
134
138
|
return r.trim();
|
135
139
|
}).join('\n');
|
136
140
|
|
137
|
-
visible = 0;
|
138
|
-
|
139
141
|
for (var j = 0; j < pre.length; j++) {
|
140
142
|
var y = pre[j];
|
141
143
|
|
@@ -144,17 +146,8 @@ module.exports = function (str, cols, opts) {
|
|
144
146
|
if (ESCAPES.indexOf(y) !== -1) {
|
145
147
|
var code = parseFloat(/[0-9][^m]*/.exec(pre.slice(j, j + 4)));
|
146
148
|
escapeCode = code === END_CODE ? null : code;
|
147
|
-
} else if (insideEscape && y === 'm') {
|
148
|
-
insideEscape = false;
|
149
|
-
continue;
|
150
149
|
}
|
151
150
|
|
152
|
-
if (insideEscape) {
|
153
|
-
continue;
|
154
|
-
}
|
155
|
-
|
156
|
-
visible++;
|
157
|
-
|
158
151
|
if (escapeCode && ESCAPE_CODES[escapeCode]) {
|
159
152
|
if (pre[j + 1] === '\n') {
|
160
153
|
ret += wrapAnsi(ESCAPE_CODES[escapeCode]);
|
@@ -165,4 +158,11 @@ module.exports = function (str, cols, opts) {
|
|
165
158
|
}
|
166
159
|
|
167
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
168
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "wrap-ansi",
|
3
|
-
"version": "
|
3
|
+
"version": "2.1.0",
|
4
4
|
"description": "Wordwrap a string with ANSI escape codes",
|
5
5
|
"license": "MIT",
|
6
6
|
"repository": "chalk/wrap-ansi",
|
@@ -19,8 +19,8 @@
|
|
19
19
|
"node": ">=0.10.0"
|
20
20
|
},
|
21
21
|
"scripts": {
|
22
|
-
"test": "xo && nyc
|
23
|
-
"
|
22
|
+
"test": "xo && nyc ava",
|
23
|
+
"coveralls": "nyc report --reporter=text-lcov | coveralls"
|
24
24
|
},
|
25
25
|
"files": [
|
26
26
|
"index.js"
|
@@ -53,13 +53,15 @@
|
|
53
53
|
"text"
|
54
54
|
],
|
55
55
|
"dependencies": {
|
56
|
-
"string-width": "^1.0.1"
|
56
|
+
"string-width": "^1.0.1",
|
57
|
+
"strip-ansi": "^3.0.1"
|
57
58
|
},
|
58
59
|
"devDependencies": {
|
59
|
-
"ava": "0.0
|
60
|
+
"ava": "^0.16.0",
|
60
61
|
"chalk": "^1.1.0",
|
61
62
|
"coveralls": "^2.11.4",
|
62
|
-
"
|
63
|
+
"has-ansi": "^2.0.0",
|
64
|
+
"nyc": "^6.2.1",
|
63
65
|
"strip-ansi": "^3.0.0",
|
64
66
|
"xo": "*"
|
65
67
|
}
|
package/readme.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# wrap-ansi [](https://travis-ci.org/chalk/wrap-ansi) [](https://travis-ci.org/chalk/wrap-ansi) [](https://coveralls.io/github/chalk/wrap-ansi?branch=master)
|
2
2
|
|
3
3
|
> Wordwrap a string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
|
4
4
|
|
@@ -43,21 +43,31 @@ Type: `number`
|
|
43
43
|
|
44
44
|
Number of columns to wrap the text to.
|
45
45
|
|
46
|
-
#### options
|
46
|
+
#### options
|
47
47
|
|
48
|
-
|
48
|
+
##### hard
|
49
|
+
|
50
|
+
Type: `boolean`<br>
|
49
51
|
Default: `false`
|
50
52
|
|
51
53
|
By default the wrap is soft, meaning long words may extend past the column width. Setting this to `true` will make it hard wrap at the column width.
|
52
54
|
|
55
|
+
##### wordWrap
|
56
|
+
|
57
|
+
Type: `boolean`<br>
|
58
|
+
Default: `true`
|
59
|
+
|
60
|
+
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
|
+
|
53
62
|
|
54
63
|
## Related
|
55
64
|
|
56
65
|
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
66
|
+
- [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal
|
57
67
|
- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
|
58
68
|
- [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures.
|
59
69
|
|
60
70
|
|
61
71
|
## License
|
62
72
|
|
63
|
-
MIT © [Sindre Sorhus](
|
73
|
+
MIT © [Sindre Sorhus](https://sindresorhus.com)
|