slice-ansi 0.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.
Files changed (3) hide show
  1. package/index.js +70 -50
  2. package/package.json +49 -48
  3. package/readme.md +15 -7
package/index.js CHANGED
@@ -1,74 +1,94 @@
1
1
  'use strict';
2
+ const isFullwidthCodePoint = require('is-fullwidth-code-point');
3
+ const astralRegex = require('astral-regex');
4
+ const ansiStyles = require('ansi-styles');
2
5
 
3
6
  const ESCAPES = [
4
7
  '\u001B',
5
8
  '\u009B'
6
9
  ];
7
10
 
8
- const END_CODE = 39;
9
-
10
- const ESCAPE_CODES = new Map([
11
- [0, 0],
12
- [1, 22],
13
- [2, 22],
14
- [3, 23],
15
- [4, 24],
16
- [7, 27],
17
- [8, 28],
18
- [9, 29],
19
- [30, 39],
20
- [31, 39],
21
- [32, 39],
22
- [33, 39],
23
- [34, 39],
24
- [35, 39],
25
- [36, 39],
26
- [37, 39],
27
- [90, 39],
28
- [40, 49],
29
- [41, 49],
30
- [42, 49],
31
- [43, 49],
32
- [44, 49],
33
- [45, 49],
34
- [46, 49],
35
- [47, 49]
36
- ]);
37
-
38
11
  const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;
39
12
 
40
- module.exports = (str, begin, end) => {
41
- end = end || str.length;
42
- let insideEscape = false;
43
- let escapeCode;
13
+ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
14
+ let output = [];
15
+ ansiCodes = [...ansiCodes];
16
+
17
+ for (let ansiCode of ansiCodes) {
18
+ const ansiCodeOrigin = ansiCode;
19
+ if (ansiCode.match(';')) {
20
+ ansiCode = ansiCode.split(';')[0][0] + '0';
21
+ }
22
+
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;
44
58
  let visible = 0;
45
59
  let output = '';
46
60
 
47
- for (let i = 0; i < str.length; i++) {
61
+ for (const [index, character] of characters.entries()) {
48
62
  let leftEscape = false;
49
- const x = str[i];
50
-
51
- if (ESCAPES.indexOf(x) !== -1) {
52
- insideEscape = true;
53
- const code = /\d[^m]*/.exec(str.slice(i, i + 4));
54
- escapeCode = code === END_CODE ? null : code;
55
- } else if (insideEscape && x === 'm') {
56
- insideEscape = false;
63
+
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;
57
75
  leftEscape = true;
58
76
  }
59
77
 
60
- if (!insideEscape && !leftEscape) {
78
+ if (!isInsideEscape && !leftEscape) {
79
+ ++visible;
80
+ }
81
+
82
+ if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) {
61
83
  ++visible;
62
84
  }
63
85
 
64
86
  if (visible > begin && visible <= end) {
65
- output += x;
66
- } else if (visible === begin && escapeCode !== undefined && escapeCode !== END_CODE) {
67
- output += wrapAnsi(escapeCode);
87
+ output += character;
88
+ } else if (visible === begin && !isInsideEscape && ansiCode !== undefined) {
89
+ output = checkAnsi(ansiCodes);
68
90
  } else if (visible >= end) {
69
- if (escapeCode !== undefined) {
70
- output += wrapAnsi(ESCAPE_CODES.get(parseInt(escapeCode, 10)) || END_CODE);
71
- }
91
+ output += checkAnsi(ansiCodes, true, ansiCode);
72
92
  break;
73
93
  }
74
94
  }
package/package.json CHANGED
@@ -1,50 +1,51 @@
1
1
  {
2
- "name": "slice-ansi",
3
- "version": "0.1.0",
4
- "description": "Slice a string with ANSI escape codes",
5
- "license": "MIT",
6
- "repository": "chalk/slice-ansi",
7
- "author": {
8
- "name": "David Caccavella",
9
- "email": "threedeecee@gmail.com"
10
- },
11
- "engines": {
12
- "node": ">=4"
13
- },
14
- "scripts": {
15
- "test": "xo && ava"
16
- },
17
- "files": [
18
- "index.js"
19
- ],
20
- "keywords": [
21
- "slice",
22
- "string",
23
- "ansi",
24
- "styles",
25
- "color",
26
- "colour",
27
- "colors",
28
- "terminal",
29
- "console",
30
- "cli",
31
- "tty",
32
- "escape",
33
- "formatting",
34
- "rgb",
35
- "256",
36
- "shell",
37
- "xterm",
38
- "log",
39
- "logging",
40
- "command-line",
41
- "text"
42
- ],
43
- "devDependencies": {
44
- "ava": "*",
45
- "chalk": "^1.1.1",
46
- "random-item": "^1.0.0",
47
- "strip-ansi": "^4.0.0",
48
- "xo": "*"
49
- }
2
+ "name": "slice-ansi",
3
+ "version": "3.0.0",
4
+ "description": "Slice a string with ANSI escape codes",
5
+ "license": "MIT",
6
+ "repository": "chalk/slice-ansi",
7
+ "engines": {
8
+ "node": ">=8"
9
+ },
10
+ "scripts": {
11
+ "test": "xo && ava"
12
+ },
13
+ "files": [
14
+ "index.js"
15
+ ],
16
+ "keywords": [
17
+ "slice",
18
+ "string",
19
+ "ansi",
20
+ "styles",
21
+ "color",
22
+ "colour",
23
+ "colors",
24
+ "terminal",
25
+ "console",
26
+ "cli",
27
+ "tty",
28
+ "escape",
29
+ "formatting",
30
+ "rgb",
31
+ "256",
32
+ "shell",
33
+ "xterm",
34
+ "log",
35
+ "logging",
36
+ "command-line",
37
+ "text"
38
+ ],
39
+ "dependencies": {
40
+ "ansi-styles": "^4.0.0",
41
+ "astral-regex": "^2.0.0",
42
+ "is-fullwidth-code-point": "^3.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "ava": "^2.1.0",
46
+ "chalk": "^2.4.2",
47
+ "random-item": "^3.0.0",
48
+ "strip-ansi": "^5.0.0",
49
+ "xo": "^0.24.0"
50
+ }
50
51
  }
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # slice-ansi [![Build Status](https://travis-ci.org/chalk/slice-ansi.svg?branch=master)](https://travis-ci.org/chalk/slice-ansi) [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/sindresorhus/xo)
1
+ # slice-ansi [![Build Status](https://travis-ci.org/chalk/slice-ansi.svg?branch=master)](https://travis-ci.org/chalk/slice-ansi) [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/xojs/xo)
2
2
 
3
3
  > Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
4
4
 
@@ -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 input = 'The quick brown ' + chalk.red('fox jumped over ') +
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(input, 20, 30));
22
+ console.log(sliceAnsi(string, 20, 30));
23
23
  ```
24
24
 
25
25
 
26
26
  ## API
27
27
 
28
- ### sliceAnsi(input, beginSlice, [endSlice])
28
+ ### sliceAnsi(string, beginSlice, endSlice?)
29
29
 
30
- #### input
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
- ## License
62
+ ---
63
63
 
64
- MIT
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>