wrap-ansi 2.0.0 → 3.0.1

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.
Files changed (4) hide show
  1. package/index.js +132 -101
  2. package/license +4 -16
  3. package/package.json +12 -17
  4. package/readme.md +32 -6
package/index.js CHANGED
@@ -1,67 +1,71 @@
1
1
  'use strict';
2
- var stringWidth = require('string-width');
3
-
4
- var ESCAPES = [
5
- '\u001b',
6
- '\u009b'
7
- ];
8
-
9
- var END_CODE = 39;
10
-
11
- var ESCAPE_CODES = {
12
- 0: 0,
13
- 1: 22,
14
- 2: 22,
15
- 3: 23,
16
- 4: 24,
17
- 7: 27,
18
- 8: 28,
19
- 9: 29,
20
- 30: 39,
21
- 31: 39,
22
- 32: 39,
23
- 33: 39,
24
- 34: 39,
25
- 35: 39,
26
- 36: 39,
27
- 37: 39,
28
- 90: 39,
29
- 40: 49,
30
- 41: 49,
31
- 42: 49,
32
- 43: 49,
33
- 44: 49,
34
- 45: 49,
35
- 46: 49,
36
- 47: 49
37
- };
38
-
39
- function wrapAnsi(code) {
40
- return ESCAPES[0] + '[' + code + 'm';
41
- }
42
-
43
- // calculate the length of words split on ' ', ignoring
44
- // the extra characters added by ansi escape codes.
45
- function wordLengths(str) {
46
- return str.split(' ').map(function (s) {
47
- return stringWidth(s);
48
- });
49
- }
50
-
51
- // wrap a long word across multiple rows.
52
- // ansi escape codes do not count towards length.
53
- function wrapWord(rows, word, cols) {
54
- var insideEscape = false;
55
- var visible = rows[rows.length - 1].length;
56
-
57
- for (var i = 0; i < word.length; i++) {
58
- var x = word[i];
59
-
60
- rows[rows.length - 1] += x;
2
+ const stringWidth = require('string-width');
3
+ const stripAnsi = require('strip-ansi');
4
+
5
+ const ESCAPES = new Set([
6
+ '\u001B',
7
+ '\u009B'
8
+ ]);
9
+
10
+ const END_CODE = 39;
11
+
12
+ const ESCAPE_CODES = new Map([
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
+ const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
41
+
42
+ // Calculate the length of words split on ' ', ignoring
43
+ // the extra characters added by ansi escape codes
44
+ const wordLengths = str => str.split(' ').map(s => stringWidth(s));
45
+
46
+ // Wrap a long word across multiple rows
47
+ // Ansi escape codes do not count towards length
48
+ const wrapWord = (rows, word, cols) => {
49
+ const arr = Array.from(word);
50
+
51
+ let insideEscape = false;
52
+ let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
53
+
54
+ for (const item of arr.entries()) {
55
+ const i = item[0];
56
+ const char = item[1];
57
+ const charLength = stringWidth(char);
58
+
59
+ if (visible + charLength <= cols) {
60
+ rows[rows.length - 1] += char;
61
+ } else {
62
+ rows.push(char);
63
+ visible = 0;
64
+ }
61
65
 
62
- if (ESCAPES.indexOf(x) !== -1) {
66
+ if (ESCAPES.has(char)) {
63
67
  insideEscape = true;
64
- } else if (insideEscape && x === 'm') {
68
+ } else if (insideEscape && char === 'm') {
65
69
  insideEscape = false;
66
70
  continue;
67
71
  }
@@ -70,49 +74,63 @@ function wrapWord(rows, word, cols) {
70
74
  continue;
71
75
  }
72
76
 
73
- visible++;
77
+ visible += charLength;
74
78
 
75
- if (visible >= cols && i < word.length - 1) {
79
+ if (visible === cols && i < arr.length - 1) {
76
80
  rows.push('');
77
81
  visible = 0;
78
82
  }
79
83
  }
80
84
 
81
- // it's possible that the last row we copy over is only
82
- // ansi escape characters, handle this edge-case.
85
+ // It's possible that the last row we copy over is only
86
+ // ansi escape characters, handle this edge-case
83
87
  if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
84
88
  rows[rows.length - 2] += rows.pop();
85
89
  }
86
- }
90
+ };
87
91
 
88
- // the wrap-ansi module can be invoked
89
- // in either 'hard' or 'soft' wrap mode.
92
+ // The wrap-ansi module can be invoked
93
+ // in either 'hard' or 'soft' wrap mode
90
94
  //
91
95
  // 'hard' will never allow a string to take up more
92
- // than cols characters.
96
+ // than cols characters
93
97
  //
94
- // 'soft' allows long words to expand past the column length.
95
- function exec(str, cols, opts) {
96
- var options = opts || {};
98
+ // 'soft' allows long words to expand past the column length
99
+ const exec = (str, cols, opts) => {
100
+ const options = opts || {};
101
+
102
+ if (str.trim() === '') {
103
+ return options.trim === false ? str : str.trim();
104
+ }
105
+
106
+ let pre = '';
107
+ let ret = '';
108
+ let escapeCode;
97
109
 
98
- var pre = '';
99
- var ret = '';
100
- var escapeCode;
110
+ const lengths = wordLengths(str);
111
+ const words = str.split(' ');
112
+ const rows = [''];
101
113
 
102
- var lengths = wordLengths(str);
103
- var words = str.split(' ');
104
- var rows = [''];
114
+ for (const item of Array.from(words).entries()) {
115
+ const i = item[0];
116
+ const word = item[1];
105
117
 
106
- for (var i = 0, word; (word = words[i]) !== undefined; i++) {
107
- var rowLength = stringWidth(rows[rows.length - 1]);
118
+ rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();
119
+ let rowLength = stringWidth(rows[rows.length - 1]);
120
+
121
+ if (rowLength || word === '') {
122
+ if (rowLength === cols && options.wordWrap === false) {
123
+ // If we start with a new word but the current row length equals the length of the columns, add a new row
124
+ rows.push('');
125
+ rowLength = 0;
126
+ }
108
127
 
109
- if (rowLength) {
110
128
  rows[rows.length - 1] += ' ';
111
129
  rowLength++;
112
130
  }
113
131
 
114
- // in 'hard' wrap mode, the length of a line is
115
- // never allowed to extend past 'cols'.
132
+ // In 'hard' wrap mode, the length of a line is
133
+ // never allowed to extend past 'cols'
116
134
  if (lengths[i] > cols && options.hard) {
117
135
  if (rowLength) {
118
136
  rows.push('');
@@ -122,41 +140,54 @@ function exec(str, cols, opts) {
122
140
  }
123
141
 
124
142
  if (rowLength + lengths[i] > cols && rowLength > 0) {
143
+ if (options.wordWrap === false && rowLength < cols) {
144
+ wrapWord(rows, word, cols);
145
+ continue;
146
+ }
147
+
125
148
  rows.push('');
126
149
  }
127
150
 
151
+ if (rowLength + lengths[i] > cols && options.wordWrap === false) {
152
+ wrapWord(rows, word, cols);
153
+ continue;
154
+ }
155
+
128
156
  rows[rows.length - 1] += word;
129
157
  }
130
158
 
131
- pre = rows.map(function (r) {
132
- return r.trim();
133
- }).join('\n');
159
+ pre = rows.map(r => options.trim === false ? r : r.trim()).join('\n');
134
160
 
135
- for (var j = 0; j < pre.length; j++) {
136
- var y = pre[j];
161
+ for (const item of Array.from(pre).entries()) {
162
+ const i = item[0];
163
+ const char = item[1];
137
164
 
138
- ret += y;
165
+ ret += char;
139
166
 
140
- if (ESCAPES.indexOf(y) !== -1) {
141
- var code = parseFloat(/[0-9][^m]*/.exec(pre.slice(j, j + 4)));
167
+ if (ESCAPES.has(char)) {
168
+ const code = parseFloat(/\d[^m]*/.exec(pre.slice(i, i + 4)));
142
169
  escapeCode = code === END_CODE ? null : code;
143
170
  }
144
171
 
145
- if (escapeCode && ESCAPE_CODES[escapeCode]) {
146
- if (pre[j + 1] === '\n') {
147
- ret += wrapAnsi(ESCAPE_CODES[escapeCode]);
148
- } else if (y === '\n') {
172
+ const code = ESCAPE_CODES.get(Number(escapeCode));
173
+
174
+ if (escapeCode && code) {
175
+ if (pre[i + 1] === '\n') {
176
+ ret += wrapAnsi(code);
177
+ } else if (char === '\n') {
149
178
  ret += wrapAnsi(escapeCode);
150
179
  }
151
180
  }
152
181
  }
153
182
 
154
183
  return ret;
155
- }
184
+ };
156
185
 
157
- // for each line break, invoke the method separately.
158
- module.exports = function (str, cols, opts) {
159
- return String(str).split('\n').map(function (substr) {
160
- return exec(substr, cols, opts);
161
- }).join('\n');
186
+ // For each newline, invoke the method separately
187
+ module.exports = (str, cols, opts) => {
188
+ return String(str)
189
+ .normalize()
190
+ .split('\n')
191
+ .map(line => exec(line, cols, opts))
192
+ .join('\n');
162
193
  };
package/license CHANGED
@@ -1,21 +1,9 @@
1
- The MIT License (MIT)
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,6 +1,6 @@
1
1
  {
2
2
  "name": "wrap-ansi",
3
- "version": "2.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Wordwrap a string with ANSI escape codes",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/wrap-ansi",
@@ -9,18 +9,12 @@
9
9
  "email": "sindresorhus@gmail.com",
10
10
  "url": "sindresorhus.com"
11
11
  },
12
- "maintainers": [
13
- "Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
14
- "Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)",
15
- "JD Ballard <i.am.qix@gmail.com> (github.com/qix-)",
16
- "Benjamin Coe <ben@npmjs.com> (github.com/bcoe)"
17
- ],
18
12
  "engines": {
19
- "node": ">=0.10.0"
13
+ "node": ">=4"
20
14
  },
21
15
  "scripts": {
22
- "test": "xo && nyc node test.js",
23
- "coverage": "nyc --reporter=text-lcov node test.js | coveralls"
16
+ "test": "xo && nyc ava",
17
+ "coveralls": "nyc report --reporter=text-lcov | coveralls"
24
18
  },
25
19
  "files": [
26
20
  "index.js"
@@ -53,15 +47,16 @@
53
47
  "text"
54
48
  ],
55
49
  "dependencies": {
56
- "string-width": "^1.0.1"
50
+ "string-width": "^2.1.1",
51
+ "strip-ansi": "^4.0.0"
57
52
  },
58
53
  "devDependencies": {
59
- "ava": "0.0.4",
60
- "chalk": "^1.1.0",
54
+ "ava": "^0.21.0",
55
+ "chalk": "^2.0.1",
61
56
  "coveralls": "^2.11.4",
62
- "has-ansi": "^2.0.0",
63
- "nyc": "^3.2.2",
64
- "strip-ansi": "^3.0.0",
65
- "xo": "*"
57
+ "has-ansi": "^3.0.0",
58
+ "nyc": "^11.0.3",
59
+ "strip-ansi": "^4.0.0",
60
+ "xo": "^0.18.2"
66
61
  }
67
62
  }
package/readme.md CHANGED
@@ -1,12 +1,12 @@
1
- # wrap-ansi [![Build Status](https://travis-ci.org/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.org/chalk/wrap-ansi) [![Coverage Status](https://coveralls.io/repos/chalk/wrap-ansi/badge.svg?branch=master&service=github)](https://coveralls.io/github/chalk/wrap-ansi?branch=master)
1
+ # wrap-ansi [![Build Status](https://travis-ci.org/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.org/chalk/wrap-ansi) [![Coverage Status](https://coveralls.io/repos/github/chalk/wrap-ansi/badge.svg?branch=master)](https://coveralls.io/github/chalk/wrap-ansi?branch=master)
2
2
 
3
- > Wordwrap a string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
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 --save wrap-ansi
9
+ $ npm install wrap-ansi
10
10
  ```
11
11
 
12
12
 
@@ -43,21 +43,47 @@ Type: `number`
43
43
 
44
44
  Number of columns to wrap the text to.
45
45
 
46
- #### options.hard
46
+ #### options
47
47
 
48
- Type: `boolean`
48
+ Type: `Object`
49
+
50
+ ##### hard
51
+
52
+ Type: `boolean`<br>
49
53
  Default: `false`
50
54
 
51
55
  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
56
 
57
+ ##### wordWrap
58
+
59
+ Type: `boolean`<br>
60
+ Default: `true`
61
+
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.
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
+
53
71
 
54
72
  ## Related
55
73
 
56
74
  - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
75
+ - [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal
57
76
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
58
77
  - [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures.
59
78
 
60
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
+
61
87
  ## License
62
88
 
63
- MIT © [Sindre Sorhus](http://sindresorhus.com)
89
+ MIT