wrap-ansi 5.1.0 → 7.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 +58 -30
  2. package/license +1 -1
  3. package/package.json +12 -11
  4. package/readme.md +18 -35
package/index.js CHANGED
@@ -10,7 +10,14 @@ const ESCAPES = new Set([
10
10
 
11
11
  const END_CODE = 39;
12
12
 
13
- const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
13
+ const ANSI_ESCAPE_BELL = '\u0007';
14
+ const ANSI_CSI = '[';
15
+ const ANSI_OSC = ']';
16
+ const ANSI_SGR_TERMINATOR = 'm';
17
+ const ANSI_ESCAPE_LINK = `${ANSI_OSC}8;;`;
18
+
19
+ const wrapAnsi = code => `${ESCAPES.values().next().value}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
20
+ const wrapAnsiHyperlink = uri => `${ESCAPES.values().next().value}${ANSI_ESCAPE_LINK}${uri}${ANSI_ESCAPE_BELL}`;
14
21
 
15
22
  // Calculate the length of words split on ' ', ignoring
16
23
  // the extra characters added by ansi escape codes
@@ -21,7 +28,8 @@ const wordLengths = string => string.split(' ').map(character => stringWidth(cha
21
28
  const wrapWord = (rows, word, columns) => {
22
29
  const characters = [...word];
23
30
 
24
- let insideEscape = false;
31
+ let isInsideEscape = false;
32
+ let isInsideLinkEscape = false;
25
33
  let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
26
34
 
27
35
  for (const [index, character] of characters.entries()) {
@@ -35,13 +43,20 @@ const wrapWord = (rows, word, columns) => {
35
43
  }
36
44
 
37
45
  if (ESCAPES.has(character)) {
38
- insideEscape = true;
39
- } else if (insideEscape && character === 'm') {
40
- insideEscape = false;
41
- continue;
46
+ isInsideEscape = true;
47
+ isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK);
42
48
  }
43
49
 
44
- if (insideEscape) {
50
+ if (isInsideEscape) {
51
+ if (isInsideLinkEscape) {
52
+ if (character === ANSI_ESCAPE_BELL) {
53
+ isInsideEscape = false;
54
+ isInsideLinkEscape = false;
55
+ }
56
+ } else if (character === ANSI_SGR_TERMINATOR) {
57
+ isInsideEscape = false;
58
+ }
59
+
45
60
  continue;
46
61
  }
47
62
 
@@ -61,8 +76,8 @@ const wrapWord = (rows, word, columns) => {
61
76
  };
62
77
 
63
78
  // Trims spaces from a string ignoring invisible sequences
64
- const stringVisibleTrimSpacesRight = str => {
65
- const words = str.split(' ');
79
+ const stringVisibleTrimSpacesRight = string => {
80
+ const words = string.split(' ');
66
81
  let last = words.length;
67
82
 
68
83
  while (last > 0) {
@@ -74,17 +89,15 @@ const stringVisibleTrimSpacesRight = str => {
74
89
  }
75
90
 
76
91
  if (last === words.length) {
77
- return str;
92
+ return string;
78
93
  }
79
94
 
80
95
  return words.slice(0, last).join(' ') + words.slice(last).join('');
81
96
  };
82
97
 
83
- // The wrap-ansi module can be invoked
84
- // in either 'hard' or 'soft' wrap mode
98
+ // The wrap-ansi module can be invoked in either 'hard' or 'soft' wrap mode
85
99
  //
86
- // 'hard' will never allow a string to take up more
87
- // than columns characters
100
+ // 'hard' will never allow a string to take up more than columns characters
88
101
  //
89
102
  // 'soft' allows long words to expand past the column length
90
103
  const exec = (string, columns, options = {}) => {
@@ -92,16 +105,16 @@ const exec = (string, columns, options = {}) => {
92
105
  return '';
93
106
  }
94
107
 
95
- let pre = '';
96
- let ret = '';
108
+ let returnValue = '';
97
109
  let escapeCode;
110
+ let escapeUrl;
98
111
 
99
112
  const lengths = wordLengths(string);
100
113
  let rows = [''];
101
114
 
102
115
  for (const [index, word] of string.split(' ').entries()) {
103
116
  if (options.trim !== false) {
104
- rows[rows.length - 1] = rows[rows.length - 1].trimLeft();
117
+ rows[rows.length - 1] = rows[rows.length - 1].trimStart();
105
118
  }
106
119
 
107
120
  let rowLength = stringWidth(rows[rows.length - 1]);
@@ -119,8 +132,7 @@ const exec = (string, columns, options = {}) => {
119
132
  }
120
133
  }
121
134
 
122
- // In 'hard' wrap mode, the length of a line is
123
- // never allowed to extend past 'columns'
135
+ // In 'hard' wrap mode, the length of a line is never allowed to extend past 'columns'
124
136
  if (options.hard && lengths[index] > columns) {
125
137
  const remainingColumns = (columns - rowLength);
126
138
  const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns);
@@ -154,34 +166,50 @@ const exec = (string, columns, options = {}) => {
154
166
  rows = rows.map(stringVisibleTrimSpacesRight);
155
167
  }
156
168
 
157
- pre = rows.join('\n');
169
+ const pre = [...rows.join('\n')];
158
170
 
159
- for (const [index, character] of [...pre].entries()) {
160
- ret += character;
171
+ for (const [index, character] of pre.entries()) {
172
+ returnValue += character;
161
173
 
162
174
  if (ESCAPES.has(character)) {
163
- const code = parseFloat(/\d[^m]*/.exec(pre.slice(index, index + 4)));
164
- escapeCode = code === END_CODE ? null : code;
175
+ const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(pre.slice(index).join('')) || {groups: {}};
176
+ if (groups.code !== undefined) {
177
+ const code = Number.parseFloat(groups.code);
178
+ escapeCode = code === END_CODE ? undefined : code;
179
+ } else if (groups.uri !== undefined) {
180
+ escapeUrl = groups.uri.length === 0 ? undefined : groups.uri;
181
+ }
165
182
  }
166
183
 
167
184
  const code = ansiStyles.codes.get(Number(escapeCode));
168
185
 
169
- if (escapeCode && code) {
170
- if (pre[index + 1] === '\n') {
171
- ret += wrapAnsi(code);
172
- } else if (character === '\n') {
173
- ret += wrapAnsi(escapeCode);
186
+ if (pre[index + 1] === '\n') {
187
+ if (escapeUrl) {
188
+ returnValue += wrapAnsiHyperlink('');
189
+ }
190
+
191
+ if (escapeCode && code) {
192
+ returnValue += wrapAnsi(code);
193
+ }
194
+ } else if (character === '\n') {
195
+ if (escapeCode && code) {
196
+ returnValue += wrapAnsi(escapeCode);
197
+ }
198
+
199
+ if (escapeUrl) {
200
+ returnValue += wrapAnsiHyperlink(escapeUrl);
174
201
  }
175
202
  }
176
203
  }
177
204
 
178
- return ret;
205
+ return returnValue;
179
206
  };
180
207
 
181
208
  // For each newline, invoke the method separately
182
209
  module.exports = (string, columns, options) => {
183
210
  return String(string)
184
211
  .normalize()
212
+ .replace(/\r\n/g, '\n')
185
213
  .split('\n')
186
214
  .map(line => exec(line, columns, options))
187
215
  .join('\n');
package/license CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
4
 
5
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:
6
6
 
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "wrap-ansi",
3
- "version": "5.1.0",
3
+ "version": "7.0.0",
4
4
  "description": "Wordwrap a string with ANSI escape codes",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/wrap-ansi",
7
+ "funding": "https://github.com/chalk/wrap-ansi?sponsor=1",
7
8
  "author": {
8
9
  "name": "Sindre Sorhus",
9
10
  "email": "sindresorhus@gmail.com",
10
- "url": "sindresorhus.com"
11
+ "url": "https://sindresorhus.com"
11
12
  },
12
13
  "engines": {
13
- "node": ">=6"
14
+ "node": ">=10"
14
15
  },
15
16
  "scripts": {
16
17
  "test": "xo && nyc ava"
@@ -46,16 +47,16 @@
46
47
  "text"
47
48
  ],
48
49
  "dependencies": {
49
- "ansi-styles": "^3.2.0",
50
- "string-width": "^3.0.0",
51
- "strip-ansi": "^5.0.0"
50
+ "ansi-styles": "^4.0.0",
51
+ "string-width": "^4.1.0",
52
+ "strip-ansi": "^6.0.0"
52
53
  },
53
54
  "devDependencies": {
54
- "ava": "^1.2.1",
55
- "chalk": "^2.4.2",
55
+ "ava": "^2.1.0",
56
+ "chalk": "^4.0.0",
56
57
  "coveralls": "^3.0.3",
57
- "has-ansi": "^3.0.0",
58
- "nyc": "^13.3.0",
59
- "xo": "^0.24.0"
58
+ "has-ansi": "^4.0.0",
59
+ "nyc": "^15.0.1",
60
+ "xo": "^0.29.1"
60
61
  }
61
62
  }
package/readme.md CHANGED
@@ -1,15 +1,13 @@
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)
1
+ # wrap-ansi [![Build Status](https://travis-ci.com/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.com/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
3
  > Wordwrap a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
4
4
 
5
-
6
5
  ## Install
7
6
 
8
7
  ```
9
8
  $ npm install wrap-ansi
10
9
  ```
11
10
 
12
-
13
11
  ## Usage
14
12
 
15
13
  ```js
@@ -24,32 +22,17 @@ console.log(wrapAnsi(input, 20));
24
22
 
25
23
  <img width="331" src="screenshot.png">
26
24
 
27
- ---
28
-
29
- <div align="center">
30
- <b>
31
- <a href="https://tidelift.com/subscription/pkg/npm-wrap_ansi?utm_source=npm-wrap-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
32
- </b>
33
- <br>
34
- <sub>
35
- Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
36
- </sub>
37
- </div>
38
-
39
- ---
40
-
41
-
42
25
  ## API
43
26
 
44
- ### wrapAnsi(input, columns, [options])
27
+ ### wrapAnsi(string, columns, options?)
45
28
 
46
29
  Wrap words to the specified column width.
47
30
 
48
- #### input
31
+ #### string
49
32
 
50
33
  Type: `string`
51
34
 
52
- String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk).
35
+ String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk). Newline characters will be normalized to `\n`.
53
36
 
54
37
  #### columns
55
38
 
@@ -59,30 +42,29 @@ Number of columns to wrap the text to.
59
42
 
60
43
  #### options
61
44
 
62
- Type: `Object`
45
+ Type: `object`
63
46
 
64
47
  ##### hard
65
48
 
66
- Type: `boolean`<br>
49
+ Type: `boolean`\
67
50
  Default: `false`
68
51
 
69
52
  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.
70
53
 
71
54
  ##### wordWrap
72
55
 
73
- Type: `boolean`<br>
56
+ Type: `boolean`\
74
57
  Default: `true`
75
58
 
76
59
  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.
77
60
 
78
61
  ##### trim
79
62
 
80
- Type: `boolean`<br>
63
+ Type: `boolean`\
81
64
  Default: `true`
82
65
 
83
66
  Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim.
84
67
 
85
-
86
68
  ## Related
87
69
 
88
70
  - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
@@ -90,19 +72,20 @@ Whitespace on all lines is removed by default. Set this option to `false` if you
90
72
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
91
73
  - [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures.
92
74
 
93
-
94
75
  ## Maintainers
95
76
 
96
77
  - [Sindre Sorhus](https://github.com/sindresorhus)
97
78
  - [Josh Junon](https://github.com/qix-)
98
79
  - [Benjamin Coe](https://github.com/bcoe)
99
80
 
81
+ ---
100
82
 
101
- ## Security
102
-
103
- To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
104
-
105
-
106
- ## License
107
-
108
- MIT
83
+ <div align="center">
84
+ <b>
85
+ <a href="https://tidelift.com/subscription/pkg/npm-wrap_ansi?utm_source=npm-wrap-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
86
+ </b>
87
+ <br>
88
+ <sub>
89
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
90
+ </sub>
91
+ </div>