cli-truncate 0.2.0 → 2.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 (5) hide show
  1. package/index.d.ts +96 -0
  2. package/index.js +87 -10
  3. package/license +4 -16
  4. package/package.json +43 -40
  5. package/readme.md +84 -12
package/index.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ declare namespace cliTruncate {
2
+ interface Options {
3
+ /**
4
+ Position to truncate the string.
5
+
6
+ @default 'end'
7
+ */
8
+ readonly position?: 'start' | 'middle' | 'end';
9
+
10
+ /**
11
+ Add a space between the text and the ellipsis.
12
+
13
+ @default false
14
+
15
+ @example
16
+ ```
17
+ cliTruncate('unicorns', 5, {position: 'end', space: true});
18
+ //=> 'uni …'
19
+
20
+ cliTruncate('unicorns', 5, {position: 'end', space: false});
21
+ //=> 'unic…'
22
+
23
+ cliTruncate('unicorns', 6, {position: 'start', space: true});
24
+ //=> '… orns'
25
+
26
+ cliTruncate('unicorns', 7, {position: 'middle', space: true});
27
+ //=> 'uni … s'
28
+ ```
29
+ */
30
+ readonly space?: boolean;
31
+
32
+ /**
33
+ Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
34
+
35
+ @default false
36
+
37
+ @example
38
+ ```
39
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
40
+ //=> '…rainbow dragons'
41
+
42
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
43
+ //=> 'unicorns…dragons'
44
+
45
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
46
+ //=> 'unico…'
47
+ ````
48
+ */
49
+ readonly preferTruncationOnSpace?: boolean;
50
+ }
51
+ }
52
+
53
+ /**
54
+ Truncate a string to a specific width in the terminal.
55
+
56
+ @param text - Text to truncate.
57
+ @param columns - Columns to occupy in the terminal.
58
+
59
+ @example
60
+ ```
61
+ import cliTruncate = require('cli-truncate');
62
+
63
+ cliTruncate('unicorn', 4);
64
+ //=> 'uni…'
65
+
66
+ // Truncate at different positions
67
+ cliTruncate('unicorn', 4, {position: 'start'});
68
+ //=> '…orn'
69
+
70
+ cliTruncate('unicorn', 4, {position: 'middle'});
71
+ //=> 'un…n'
72
+
73
+ cliTruncate('\u001B[31municorn\u001B[39m', 4);
74
+ //=> '\u001B[31muni\u001B[39m…'
75
+
76
+ // Truncate Unicode surrogate pairs
77
+ cliTruncate('uni\uD83C\uDE00corn', 5);
78
+ //=> 'uni\uD83C\uDE00…'
79
+
80
+ // Truncate fullwidth characters
81
+ cliTruncate('안녕하세요', 3);
82
+ //=> '안…'
83
+
84
+ // Truncate the paragraph to the terminal width
85
+ const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
86
+ cliTruncate(paragraph, process.stdout.columns));
87
+ //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
88
+ ```
89
+ */
90
+ declare function cliTruncate(
91
+ text: string,
92
+ columns: number,
93
+ options?: cliTruncate.Options
94
+ ): string;
95
+
96
+ export = cliTruncate;
package/index.js CHANGED
@@ -1,16 +1,42 @@
1
1
  'use strict';
2
- var sliceAnsi = require('slice-ansi');
3
- var stringWidth = require('string-width');
2
+ const sliceAnsi = require('slice-ansi');
3
+ const stringWidth = require('string-width');
4
4
 
5
- module.exports = function (input, columns) {
6
- var ellipsis = '';
5
+ function getIndexOfNearestSpace(string, index, shouldSearchRight) {
6
+ if (string.charAt(index) === ' ') {
7
+ return index;
8
+ }
9
+
10
+ for (let i = 1; i <= 3; i++) {
11
+ if (shouldSearchRight) {
12
+ if (string.charAt(index + i) === ' ') {
13
+ return index + i;
14
+ }
15
+ } else if (string.charAt(index - i) === ' ') {
16
+ return index - i;
17
+ }
18
+ }
19
+
20
+ return index;
21
+ }
22
+
23
+ module.exports = (text, columns, options) => {
24
+ options = {
25
+ position: 'end',
26
+ preferTruncationOnSpace: false,
27
+ ...options
28
+ };
7
29
 
8
- if (typeof input !== 'string') {
9
- throw new TypeError('Expected `input` to be a string, got ' + typeof input);
30
+ const {position, space, preferTruncationOnSpace} = options;
31
+ let ellipsis = '…';
32
+ let ellipsisWidth = 1;
33
+
34
+ if (typeof text !== 'string') {
35
+ throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
10
36
  }
11
37
 
12
38
  if (typeof columns !== 'number') {
13
- throw new TypeError('Expected `columns` to be a number, got ' + typeof columns);
39
+ throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
14
40
  }
15
41
 
16
42
  if (columns < 1) {
@@ -21,9 +47,60 @@ module.exports = function (input, columns) {
21
47
  return ellipsis;
22
48
  }
23
49
 
24
- if (stringWidth(input) <= columns) {
25
- return input;
50
+ const length = stringWidth(text);
51
+
52
+ if (length <= columns) {
53
+ return text;
54
+ }
55
+
56
+ if (position === 'start') {
57
+ if (preferTruncationOnSpace) {
58
+ const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
59
+ return ellipsis + sliceAnsi(text, nearestSpace, length).trim();
60
+ }
61
+
62
+ if (space === true) {
63
+ ellipsis += ' ';
64
+ ellipsisWidth = 2;
65
+ }
66
+
67
+ return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length);
68
+ }
69
+
70
+ if (position === 'middle') {
71
+ if (space === true) {
72
+ ellipsis = ' ' + ellipsis + ' ';
73
+ ellipsisWidth = 3;
74
+ }
75
+
76
+ const half = Math.floor(columns / 2);
77
+
78
+ if (preferTruncationOnSpace) {
79
+ const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
80
+ const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
81
+ return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + ellipsis + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
82
+ }
83
+
84
+ return (
85
+ sliceAnsi(text, 0, half) +
86
+ ellipsis +
87
+ sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
88
+ );
89
+ }
90
+
91
+ if (position === 'end') {
92
+ if (preferTruncationOnSpace) {
93
+ const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
94
+ return sliceAnsi(text, 0, nearestSpace) + ellipsis;
95
+ }
96
+
97
+ if (space === true) {
98
+ ellipsis = ' ' + ellipsis;
99
+ ellipsisWidth = 2;
100
+ }
101
+
102
+ return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
26
103
  }
27
104
 
28
- return sliceAnsi(input, 0, columns - 1) + ellipsis;
105
+ throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
29
106
  };
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,42 +1,45 @@
1
1
  {
2
- "name": "cli-truncate",
3
- "version": "0.2.0",
4
- "description": "Truncate a string to a specific width in the terminal",
5
- "license": "MIT",
6
- "repository": "sindresorhus/cli-truncate",
7
- "author": {
8
- "name": "Sindre Sorhus",
9
- "email": "sindresorhus@gmail.com",
10
- "url": "sindresorhus.com"
11
- },
12
- "engines": {
13
- "node": ">=0.10.0"
14
- },
15
- "scripts": {
16
- "test": "xo && ava"
17
- },
18
- "files": [
19
- "index.js"
20
- ],
21
- "keywords": [
22
- "truncate",
23
- "ellipsis",
24
- "text",
25
- "limit",
26
- "slice",
27
- "cli",
28
- "terminal",
29
- "term",
30
- "shell",
31
- "width",
32
- "ansi"
33
- ],
34
- "dependencies": {
35
- "slice-ansi": "0.0.4",
36
- "string-width": "^1.0.1"
37
- },
38
- "devDependencies": {
39
- "ava": "*",
40
- "xo": "*"
41
- }
2
+ "name": "cli-truncate",
3
+ "version": "2.0.0",
4
+ "description": "Truncate a string to a specific width in the terminal",
5
+ "license": "MIT",
6
+ "repository": "sindresorhus/cli-truncate",
7
+ "author": {
8
+ "name": "Sindre Sorhus",
9
+ "email": "sindresorhus@gmail.com",
10
+ "url": "sindresorhus.com"
11
+ },
12
+ "engines": {
13
+ "node": ">=8"
14
+ },
15
+ "scripts": {
16
+ "test": "xo && ava && tsd"
17
+ },
18
+ "files": [
19
+ "index.js",
20
+ "index.d.ts"
21
+ ],
22
+ "keywords": [
23
+ "truncate",
24
+ "ellipsis",
25
+ "text",
26
+ "limit",
27
+ "slice",
28
+ "cli",
29
+ "terminal",
30
+ "term",
31
+ "shell",
32
+ "width",
33
+ "ansi",
34
+ "string"
35
+ ],
36
+ "dependencies": {
37
+ "slice-ansi": "^2.1.0",
38
+ "string-width": "^4.1.0"
39
+ },
40
+ "devDependencies": {
41
+ "ava": "^2.1.0",
42
+ "tsd": "^0.7.2",
43
+ "xo": "^0.24.0"
44
+ }
42
45
  }
package/readme.md CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  > Truncate a string to a specific width in the terminal
4
4
 
5
- Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk).
5
+ Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk). It also supports Unicode surrogate pairs and fullwidth characters.
6
6
 
7
7
 
8
8
  ## Install
9
9
 
10
10
  ```
11
- $ npm install --save cli-truncate
11
+ $ npm install cli-truncate
12
12
  ```
13
13
 
14
14
 
@@ -20,10 +20,28 @@ const cliTruncate = require('cli-truncate');
20
20
  cliTruncate('unicorn', 4);
21
21
  //=> 'uni…'
22
22
 
23
- cliTruncate('\u001b[31municorn\u001b[39m', 4);
24
- //=> '\u001b[31muni\u001b[39m…'
23
+ // Truncate at different positions
24
+ cliTruncate('unicorn', 4, {position: 'start'});
25
+ //=> '…orn'
25
26
 
26
- // truncate the paragraph to the terminal width
27
+ cliTruncate('unicorn', 4, {position: 'middle'});
28
+ //=> 'un…n'
29
+
30
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'end'})
31
+ //=> 'unico…'
32
+
33
+ cliTruncate('\u001B[31municorn\u001B[39m', 4);
34
+ //=> '\u001B[31muni\u001B[39m…'
35
+
36
+ // Truncate Unicode surrogate pairs
37
+ cliTruncate('uni\uD83C\uDE00corn', 5);
38
+ //=> 'uni\uD83C\uDE00…'
39
+
40
+ // Truncate fullwidth characters
41
+ cliTruncate('안녕하세요', 3);
42
+ //=> '안…'
43
+
44
+ // Truncate the paragraph to the terminal width
27
45
  const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
28
46
  cliTruncate(paragraph, process.stdout.columns));
29
47
  //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
@@ -32,9 +50,9 @@ cliTruncate(paragraph, process.stdout.columns));
32
50
 
33
51
  ## API
34
52
 
35
- ### cliTruncate(input, columns)
53
+ ### cliTruncate(text, columns, options?)
36
54
 
37
- #### input
55
+ #### text
38
56
 
39
57
  Type: `string`
40
58
 
@@ -46,13 +64,67 @@ Type: `number`
46
64
 
47
65
  Columns to occupy in the terminal.
48
66
 
67
+ #### options
49
68
 
50
- ## Related
69
+ Type: `object`
51
70
 
52
- - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
53
- - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
71
+ ##### position
72
+
73
+ Type: `string`<br>
74
+ Default: `'end'`<br>
75
+ Values: `'start'` `'middle'` `'end'`
76
+
77
+ Position to truncate the string.
78
+
79
+ ##### space
80
+
81
+ Type: `boolean`<br>
82
+ Default: `false`
83
+
84
+ Add a space between the text and the ellipsis.
85
+
86
+ ```js
87
+ cliTruncate('unicorns', 5, {space: false});
88
+ //=> 'unic…'
89
+
90
+ cliTruncate('unicorns', 5, {space: true});
91
+ //=> 'uni …'
54
92
 
93
+ cliTruncate('unicorns', 6, {position: 'start', space: true});
94
+ //=> '… orns'
55
95
 
56
- ## License
96
+ cliTruncate('unicorns', 7, {position: 'middle', space: true});
97
+ //=> 'uni … s'
98
+ ```
99
+
100
+ ##### preferTruncationOnSpace
101
+
102
+ Type: `boolean`<br>
103
+ Default: `false`
104
+
105
+ Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
106
+
107
+ ```js
108
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
109
+ //=> '…rainbow dragons'
110
+
111
+ // without preferTruncationOnSpace
112
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'start'})
113
+ //=> '…rns rainbow dragons'
114
+
115
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true})
116
+ //=> 'unicorns…dragons'
57
117
 
58
- MIT © [Sindre Sorhus](https://sindresorhus.com)
118
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true})
119
+ //=> 'unico…'
120
+
121
+ // preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
122
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true})
123
+ //=> 'uni…ns'
124
+ ```
125
+
126
+
127
+ ## Related
128
+
129
+ - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
130
+ - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes