slice-ansi 2.1.0 → 3.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 +61 -23
- package/package.json +7 -7
- package/readme.md +14 -6
package/index.js
CHANGED
@@ -8,49 +8,87 @@ const ESCAPES = [
|
|
8
8
|
'\u009B'
|
9
9
|
];
|
10
10
|
|
11
|
-
const END_CODE = 39;
|
12
|
-
|
13
11
|
const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;
|
14
12
|
|
15
|
-
|
16
|
-
|
13
|
+
const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
|
14
|
+
let output = [];
|
15
|
+
ansiCodes = [...ansiCodes];
|
17
16
|
|
18
|
-
|
17
|
+
for (let ansiCode of ansiCodes) {
|
18
|
+
const ansiCodeOrigin = ansiCode;
|
19
|
+
if (ansiCode.match(';')) {
|
20
|
+
ansiCode = ansiCode.split(';')[0][0] + '0';
|
21
|
+
}
|
19
22
|
|
20
|
-
|
21
|
-
|
23
|
+
const item = ansiStyles.codes.get(parseInt(ansiCode, 10));
|
24
|
+
if (item) {
|
25
|
+
const indexEscape = ansiCodes.indexOf(item.toString());
|
26
|
+
if (indexEscape >= 0) {
|
27
|
+
ansiCodes.splice(indexEscape, 1);
|
28
|
+
} else {
|
29
|
+
output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin));
|
30
|
+
}
|
31
|
+
} else if (isEscapes) {
|
32
|
+
output.push(wrapAnsi(0));
|
33
|
+
break;
|
34
|
+
} else {
|
35
|
+
output.push(wrapAnsi(ansiCodeOrigin));
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
if (isEscapes) {
|
40
|
+
output = output.filter((element, index) => output.indexOf(element) === index);
|
41
|
+
if (endAnsiCode !== undefined) {
|
42
|
+
const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(parseInt(endAnsiCode, 10)));
|
43
|
+
output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
return output.join('');
|
48
|
+
};
|
49
|
+
|
50
|
+
module.exports = (string, begin, end) => {
|
51
|
+
const characters = [...string.normalize()];
|
52
|
+
const ansiCodes = [];
|
53
|
+
|
54
|
+
end = typeof end === 'number' ? end : characters.length;
|
55
|
+
|
56
|
+
let isInsideEscape = false;
|
57
|
+
let ansiCode;
|
22
58
|
let visible = 0;
|
23
59
|
let output = '';
|
24
60
|
|
25
|
-
for (const [
|
61
|
+
for (const [index, character] of characters.entries()) {
|
26
62
|
let leftEscape = false;
|
27
63
|
|
28
|
-
if (ESCAPES.includes(
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
64
|
+
if (ESCAPES.includes(character)) {
|
65
|
+
const code = /\d[^m]*/.exec(string.slice(index, index + 18));
|
66
|
+
ansiCode = code && code.length > 0 ? code[0] : undefined;
|
67
|
+
if (visible < end) {
|
68
|
+
isInsideEscape = true;
|
69
|
+
if (ansiCode !== undefined) {
|
70
|
+
ansiCodes.push(ansiCode);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
} else if (isInsideEscape && character === 'm') {
|
74
|
+
isInsideEscape = false;
|
34
75
|
leftEscape = true;
|
35
76
|
}
|
36
77
|
|
37
|
-
if (!
|
78
|
+
if (!isInsideEscape && !leftEscape) {
|
38
79
|
++visible;
|
39
80
|
}
|
40
81
|
|
41
|
-
if (!astralRegex({exact: true}).test(
|
82
|
+
if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) {
|
42
83
|
++visible;
|
43
84
|
}
|
44
85
|
|
45
86
|
if (visible > begin && visible <= end) {
|
46
|
-
output +=
|
47
|
-
} else if (visible === begin && !
|
48
|
-
output
|
87
|
+
output += character;
|
88
|
+
} else if (visible === begin && !isInsideEscape && ansiCode !== undefined) {
|
89
|
+
output = checkAnsi(ansiCodes);
|
49
90
|
} else if (visible >= end) {
|
50
|
-
|
51
|
-
output += wrapAnsi(ansiStyles.codes.get(parseInt(escapeCode, 10)) || END_CODE);
|
52
|
-
}
|
53
|
-
|
91
|
+
output += checkAnsi(ansiCodes, true, ansiCode);
|
54
92
|
break;
|
55
93
|
}
|
56
94
|
}
|
package/package.json
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "slice-ansi",
|
3
|
-
"version": "
|
3
|
+
"version": "3.0.0",
|
4
4
|
"description": "Slice a string with ANSI escape codes",
|
5
5
|
"license": "MIT",
|
6
6
|
"repository": "chalk/slice-ansi",
|
7
7
|
"engines": {
|
8
|
-
"node": ">=
|
8
|
+
"node": ">=8"
|
9
9
|
},
|
10
10
|
"scripts": {
|
11
11
|
"test": "xo && ava"
|
@@ -37,14 +37,14 @@
|
|
37
37
|
"text"
|
38
38
|
],
|
39
39
|
"dependencies": {
|
40
|
-
"ansi-styles": "^
|
41
|
-
"astral-regex": "^
|
42
|
-
"is-fullwidth-code-point": "^
|
40
|
+
"ansi-styles": "^4.0.0",
|
41
|
+
"astral-regex": "^2.0.0",
|
42
|
+
"is-fullwidth-code-point": "^3.0.0"
|
43
43
|
},
|
44
44
|
"devDependencies": {
|
45
|
-
"ava": "^
|
45
|
+
"ava": "^2.1.0",
|
46
46
|
"chalk": "^2.4.2",
|
47
|
-
"random-item": "^
|
47
|
+
"random-item": "^3.0.0",
|
48
48
|
"strip-ansi": "^5.0.0",
|
49
49
|
"xo": "^0.24.0"
|
50
50
|
}
|
package/readme.md
CHANGED
@@ -16,18 +16,18 @@ $ npm install slice-ansi
|
|
16
16
|
const chalk = require('chalk');
|
17
17
|
const sliceAnsi = require('slice-ansi');
|
18
18
|
|
19
|
-
const
|
19
|
+
const string = 'The quick brown ' + chalk.red('fox jumped over ') +
|
20
20
|
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
|
21
21
|
|
22
|
-
console.log(sliceAnsi(
|
22
|
+
console.log(sliceAnsi(string, 20, 30));
|
23
23
|
```
|
24
24
|
|
25
25
|
|
26
26
|
## API
|
27
27
|
|
28
|
-
### sliceAnsi(
|
28
|
+
### sliceAnsi(string, beginSlice, endSlice?)
|
29
29
|
|
30
|
-
####
|
30
|
+
#### string
|
31
31
|
|
32
32
|
Type: `string`
|
33
33
|
|
@@ -59,6 +59,14 @@ Zero-based index at which to end the slice.
|
|
59
59
|
- [Josh Junon](https://github.com/qix-)
|
60
60
|
|
61
61
|
|
62
|
-
|
62
|
+
---
|
63
63
|
|
64
|
-
|
64
|
+
<div align="center">
|
65
|
+
<b>
|
66
|
+
<a href="https://tidelift.com/subscription/pkg/npm-slice_ansi?utm_source=npm-slice-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
67
|
+
</b>
|
68
|
+
<br>
|
69
|
+
<sub>
|
70
|
+
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
71
|
+
</sub>
|
72
|
+
</div>
|