slice-ansi 0.0.4 → 2.1.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 +28 -49
  2. package/license +9 -22
  3. package/package.json +49 -54
  4. package/readme.md +20 -12
package/index.js CHANGED
@@ -1,58 +1,33 @@
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
- var ESCAPES = [
4
- '\u001b',
5
- '\u009b'
6
+ const ESCAPES = [
7
+ '\u001B',
8
+ '\u009B'
6
9
  ];
7
10
 
8
- var END_CODE = 39;
11
+ const END_CODE = 39;
9
12
 
10
- var ESCAPE_CODES = {
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
- };
13
+ const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;
14
+
15
+ module.exports = (str, begin, end) => {
16
+ const arr = [...str.normalize()];
37
17
 
38
- function wrapAnsi(code) {
39
- return ESCAPES[0] + '[' + code + 'm';
40
- }
18
+ end = typeof end === 'number' ? end : arr.length;
41
19
 
42
- module.exports = function (str, begin, end) {
43
- end = end || str.length;
44
- var insideEscape = false;
45
- var escapeCode;
46
- var visible = 0;
47
- var output = '';
20
+ let insideEscape = false;
21
+ let escapeCode = null;
22
+ let visible = 0;
23
+ let output = '';
48
24
 
49
- for (var i = 0; i < str.length; i++) {
50
- var leftEscape = false;
51
- var x = str[i];
25
+ for (const [i, x] of arr.entries()) {
26
+ let leftEscape = false;
52
27
 
53
- if (ESCAPES.indexOf(x) !== -1) {
28
+ if (ESCAPES.includes(x)) {
54
29
  insideEscape = true;
55
- var code = /[0-9][^m]*/.exec(str.slice(i, i + 4));
30
+ const code = /\d[^m]*/.exec(str.slice(i, i + 18));
56
31
  escapeCode = code === END_CODE ? null : code;
57
32
  } else if (insideEscape && x === 'm') {
58
33
  insideEscape = false;
@@ -63,18 +38,22 @@ module.exports = function (str, begin, end) {
63
38
  ++visible;
64
39
  }
65
40
 
41
+ if (!astralRegex({exact: true}).test(x) && isFullwidthCodePoint(x.codePointAt())) {
42
+ ++visible;
43
+ }
44
+
66
45
  if (visible > begin && visible <= end) {
67
46
  output += x;
68
- } else if (visible === begin && escapeCode !== undefined && escapeCode !== END_CODE) {
47
+ } else if (visible === begin && !insideEscape && escapeCode !== null && escapeCode !== END_CODE) {
69
48
  output += wrapAnsi(escapeCode);
70
49
  } else if (visible >= end) {
71
- if (escapeCode !== undefined) {
72
- output += wrapAnsi(ESCAPE_CODES[escapeCode] || END_CODE);
50
+ if (escapeCode !== null) {
51
+ output += wrapAnsi(ansiStyles.codes.get(parseInt(escapeCode, 10)) || END_CODE);
73
52
  }
53
+
74
54
  break;
75
55
  }
76
56
  }
77
57
 
78
58
  return output;
79
59
  };
80
-
package/license CHANGED
@@ -1,22 +1,9 @@
1
- (The MIT License)
2
-
3
- Copyright (c) 2015 DC <threedeecee@gmail.com>
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- 'Software'), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) DC <threedeecee@gmail.com>
4
+
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
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
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,56 +1,51 @@
1
1
  {
2
- "name": "slice-ansi",
3
- "version": "0.0.4",
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
- "maintainers": [
12
- "David Caccavella <threedeecee@gmail.com> (github.com/dthree)",
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
- ],
17
- "engines": {
18
- "node": ">=0.10.0"
19
- },
20
- "scripts": {
21
- "test": "node test.js && xo"
22
- },
23
- "files": [
24
- "index.js"
25
- ],
26
- "keywords": [
27
- "slice",
28
- "string",
29
- "ansi",
30
- "styles",
31
- "color",
32
- "colour",
33
- "colors",
34
- "terminal",
35
- "console",
36
- "cli",
37
- "tty",
38
- "escape",
39
- "formatting",
40
- "rgb",
41
- "256",
42
- "shell",
43
- "xterm",
44
- "log",
45
- "logging",
46
- "command-line",
47
- "text"
48
- ],
49
- "dependencies": {},
50
- "devDependencies": {
51
- "ava": "^0.2.0",
52
- "chalk": "^1.1.1",
53
- "strip-ansi": "^3.0.0",
54
- "xo": "*"
55
- }
2
+ "name": "slice-ansi",
3
+ "version": "2.1.0",
4
+ "description": "Slice a string with ANSI escape codes",
5
+ "license": "MIT",
6
+ "repository": "chalk/slice-ansi",
7
+ "engines": {
8
+ "node": ">=6"
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": "^3.2.0",
41
+ "astral-regex": "^1.0.0",
42
+ "is-fullwidth-code-point": "^2.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "ava": "^1.1.0",
46
+ "chalk": "^2.4.2",
47
+ "random-item": "^1.0.0",
48
+ "strip-ansi": "^5.0.0",
49
+ "xo": "^0.24.0"
50
+ }
56
51
  }
package/readme.md CHANGED
@@ -1,31 +1,31 @@
1
- # slice-ansi
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
- [![Build Status](https://travis-ci.org/vorpaljs/slice-ansi.svg?branch=master)](https://travis-ci.org/vorpaljs/slice-ansi)
4
- [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/sindresorhus/xo)
3
+ > Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
5
4
 
6
- > Slice a string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
7
5
 
8
6
  ## Install
9
7
 
10
8
  ```
11
- $ npm install --save slice-ansi
9
+ $ npm install slice-ansi
12
10
  ```
13
11
 
12
+
14
13
  ## Usage
15
14
 
16
15
  ```js
17
- var chalk = require('chalk');
18
- var sliceAnsi = require('slice-ansi');
16
+ const chalk = require('chalk');
17
+ const sliceAnsi = require('slice-ansi');
19
18
 
20
- var input = 'The quick brown ' + chalk.red('fox jumped over ') +
19
+ const input = 'The quick brown ' + chalk.red('fox jumped over ') +
21
20
  'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
22
21
 
23
22
  console.log(sliceAnsi(input, 20, 30));
24
23
  ```
25
24
 
25
+
26
26
  ## API
27
27
 
28
- ### sliceAnsi(input, beginSlice[, endSlice])
28
+ ### sliceAnsi(input, beginSlice, [endSlice])
29
29
 
30
30
  #### input
31
31
 
@@ -37,20 +37,28 @@ String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/c
37
37
 
38
38
  Type: `number`
39
39
 
40
- The zero-based index at which to begin the slice.
40
+ Zero-based index at which to begin the slice.
41
41
 
42
42
  #### endSlice
43
43
 
44
44
  Type: `number`
45
45
 
46
- Optional. The zero-based index at which to end the slice.
46
+ Zero-based index at which to end the slice.
47
47
 
48
48
 
49
49
  ## Related
50
50
 
51
+ - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
52
+ - [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal
51
53
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
52
54
 
53
55
 
56
+ ## Maintainers
57
+
58
+ - [Sindre Sorhus](https://github.com/sindresorhus)
59
+ - [Josh Junon](https://github.com/qix-)
60
+
61
+
54
62
  ## License
55
63
 
56
- MIT © [David Caccavella](https://githbu.com/dthree)
64
+ MIT