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.
Files changed (4) hide show
  1. package/index.js +90 -103
  2. package/license +4 -16
  3. package/package.json +60 -66
  4. package/readme.md +19 -3
package/index.js CHANGED
@@ -1,68 +1,42 @@
1
1
  '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
- };
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
- function wrapAnsi(code) {
41
- return ESCAPES[0] + '[' + code + 'm';
42
- }
13
+ const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
43
14
 
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
- }
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
- // 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;
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
- for (var i = 0; i < word.length; i++) {
59
- var x = word[i];
24
+ let insideEscape = false;
25
+ let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
60
26
 
61
- rows[rows.length - 1] += x;
27
+ for (const [index, character] of characters.entries()) {
28
+ const characterLength = stringWidth(character);
62
29
 
63
- if (ESCAPES.indexOf(x) !== -1) {
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 && x === 'm') {
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 >= cols && i < word.length - 1) {
50
+ if (visible === columns && index < characters.length - 1) {
77
51
  rows.push('');
78
52
  visible = 0;
79
53
  }
80
54
  }
81
55
 
82
- // it's possible that the last row we copy over is only
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
- // the wrap-ansi module can be invoked
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 cols characters.
67
+ // than columns characters
94
68
  //
95
- // 'soft' allows long words to expand past the column length.
96
- function exec(str, cols, opts) {
97
- var options = opts || {};
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
- var pre = '';
100
- var ret = '';
101
- var escapeCode;
75
+ let pre = '';
76
+ let ret = '';
77
+ let escapeCode;
102
78
 
103
- var lengths = wordLengths(str);
104
- var words = str.split(' ');
105
- var rows = [''];
79
+ const lengths = wordLengths(string);
80
+ const rows = [''];
106
81
 
107
- for (var i = 0, word; (word = words[i]) !== undefined; i++) {
108
- var rowLength = stringWidth(rows[rows.length - 1]);
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
- // in 'hard' wrap mode, the length of a line is
116
- // never allowed to extend past 'cols'.
117
- if (lengths[i] > cols && options.hard) {
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, cols);
103
+ wrapWord(rows, word, columns);
122
104
  continue;
123
105
  }
124
106
 
125
- if (rowLength + lengths[i] > cols && rowLength > 0) {
126
- if (options.wordWrap === false && rowLength < cols) {
127
- wrapWord(rows, word, cols);
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(function (r) {
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
- ret += y;
126
+ for (const [index, character] of [...pre].entries()) {
127
+ ret += character;
145
128
 
146
- if (ESCAPES.indexOf(y) !== -1) {
147
- var code = parseFloat(/[0-9][^m]*/.exec(pre.slice(j, j + 4)));
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
- if (escapeCode && ESCAPE_CODES[escapeCode]) {
152
- if (pre[j + 1] === '\n') {
153
- ret += wrapAnsi(ESCAPE_CODES[escapeCode]);
154
- } else if (y === '\n') {
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
- // 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');
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
- 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,68 +1,62 @@
1
1
  {
2
- "name": "wrap-ansi",
3
- "version": "2.1.0-candidate",
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
- "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
- "engines": {
19
- "node": ">=0.10.0"
20
- },
21
- "scripts": {
22
- "test": "xo && nyc ava",
23
- "coveralls": "nyc report --reporter=text-lcov | coveralls"
24
- },
25
- "files": [
26
- "index.js"
27
- ],
28
- "keywords": [
29
- "wrap",
30
- "break",
31
- "wordwrap",
32
- "wordbreak",
33
- "linewrap",
34
- "ansi",
35
- "styles",
36
- "color",
37
- "colour",
38
- "colors",
39
- "terminal",
40
- "console",
41
- "cli",
42
- "string",
43
- "tty",
44
- "escape",
45
- "formatting",
46
- "rgb",
47
- "256",
48
- "shell",
49
- "xterm",
50
- "log",
51
- "logging",
52
- "command-line",
53
- "text"
54
- ],
55
- "dependencies": {
56
- "string-width": "^1.0.1",
57
- "strip-ansi": "^3.0.1"
58
- },
59
- "devDependencies": {
60
- "ava": "*",
61
- "chalk": "^1.1.0",
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 [![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
 
@@ -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 © [Sindre Sorhus](https://sindresorhus.com)
89
+ MIT