cli-truncate 0.2.1 → 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 (5) hide show
  1. package/index.d.ts +96 -0
  2. package/index.js +81 -18
  3. package/license +4 -16
  4. package/package.json +44 -40
  5. package/readme.md +80 -16
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,19 +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, options) {
6
- options = options || {};
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
+ }
7
22
 
8
- var position = options.position || 'end';
9
- var ellipsis = '…';
23
+ module.exports = (text, columns, options) => {
24
+ options = {
25
+ position: 'end',
26
+ preferTruncationOnSpace: false,
27
+ ...options
28
+ };
10
29
 
11
- if (typeof input !== 'string') {
12
- 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}`);
13
36
  }
14
37
 
15
38
  if (typeof columns !== 'number') {
16
- 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}`);
17
40
  }
18
41
 
19
42
  if (columns < 1) {
@@ -24,20 +47,60 @@ module.exports = function (input, columns, options) {
24
47
  return ellipsis;
25
48
  }
26
49
 
27
- var length = stringWidth(input);
50
+ const length = stringWidth(text);
28
51
 
29
52
  if (length <= columns) {
30
- return input;
53
+ return text;
31
54
  }
32
55
 
33
56
  if (position === 'start') {
34
- return ellipsis + sliceAnsi(input, length - columns + 1, length);
35
- } else if (position === 'middle') {
36
- var half = Math.floor(columns / 2);
37
- return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);
38
- } else if (position === 'end') {
39
- return sliceAnsi(input, 0, columns - 1) + ellipsis;
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;
40
103
  }
41
104
 
42
- throw new Error('Expected `options.position` to be either `start`, `middle` or `end`, got ' + position);
105
+ throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
43
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,46 @@
1
1
  {
2
- "name": "cli-truncate",
3
- "version": "0.2.1",
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.1.0",
4
+ "description": "Truncate a string to a specific width in the terminal",
5
+ "license": "MIT",
6
+ "repository": "sindresorhus/cli-truncate",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
8
+ "author": {
9
+ "name": "Sindre Sorhus",
10
+ "email": "sindresorhus@gmail.com",
11
+ "url": "sindresorhus.com"
12
+ },
13
+ "engines": {
14
+ "node": ">=8"
15
+ },
16
+ "scripts": {
17
+ "test": "xo && ava && tsd"
18
+ },
19
+ "files": [
20
+ "index.js",
21
+ "index.d.ts"
22
+ ],
23
+ "keywords": [
24
+ "truncate",
25
+ "ellipsis",
26
+ "text",
27
+ "limit",
28
+ "slice",
29
+ "cli",
30
+ "terminal",
31
+ "term",
32
+ "shell",
33
+ "width",
34
+ "ansi",
35
+ "string"
36
+ ],
37
+ "dependencies": {
38
+ "slice-ansi": "^3.0.0",
39
+ "string-width": "^4.2.0"
40
+ },
41
+ "devDependencies": {
42
+ "ava": "^2.1.0",
43
+ "tsd": "^0.11.0",
44
+ "xo": "^0.25.3"
45
+ }
42
46
  }
package/readme.md CHANGED
@@ -2,16 +2,14 @@
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).
6
-
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.
7
6
 
8
7
  ## Install
9
8
 
10
9
  ```
11
- $ npm install --save cli-truncate
10
+ $ npm install cli-truncate
12
11
  ```
13
12
 
14
-
15
13
  ## Usage
16
14
 
17
15
  ```js
@@ -20,28 +18,38 @@ const cliTruncate = require('cli-truncate');
20
18
  cliTruncate('unicorn', 4);
21
19
  //=> 'uni…'
22
20
 
23
- // truncate at different positions
21
+ // Truncate at different positions
24
22
  cliTruncate('unicorn', 4, {position: 'start'});
25
23
  //=> '…orn'
26
24
 
27
25
  cliTruncate('unicorn', 4, {position: 'middle'});
28
26
  //=> 'un…n'
29
27
 
30
- cliTruncate('\u001b[31municorn\u001b[39m', 4);
31
- //=> '\u001b[31muni\u001b[39m…'
28
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'end'})
29
+ //=> 'unico…'
30
+
31
+ cliTruncate('\u001B[31municorn\u001B[39m', 4);
32
+ //=> '\u001B[31muni\u001B[39m…'
33
+
34
+ // Truncate Unicode surrogate pairs
35
+ cliTruncate('uni\uD83C\uDE00corn', 5);
36
+ //=> 'uni\uD83C\uDE00…'
32
37
 
33
- // truncate the paragraph to the terminal width
38
+ // Truncate fullwidth characters
39
+ cliTruncate('안녕하세요', 3);
40
+ //=> '안…'
41
+
42
+ // Truncate the paragraph to the terminal width
34
43
  const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
35
44
  cliTruncate(paragraph, process.stdout.columns));
36
45
  //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
37
46
  ```
38
47
 
39
-
40
48
  ## API
41
49
 
42
- ### cliTruncate(input, columns, [options])
50
+ ### cliTruncate(text, columns, options?)
43
51
 
44
- #### input
52
+ #### text
45
53
 
46
54
  Type: `string`
47
55
 
@@ -55,14 +63,62 @@ Columns to occupy in the terminal.
55
63
 
56
64
  #### options
57
65
 
66
+ Type: `object`
67
+
58
68
  ##### position
59
69
 
60
- Type: `string`<br>
61
- Default: `'end'`<br>
62
- Values: `'start'`, `'middle'`, `'end'`
70
+ Type: `string`\
71
+ Default: `'end'`\
72
+ Values: `'start'` `'middle'` `'end'`
63
73
 
64
74
  Position to truncate the string.
65
75
 
76
+ ##### space
77
+
78
+ Type: `boolean`\
79
+ Default: `false`
80
+
81
+ Add a space between the text and the ellipsis.
82
+
83
+ ```js
84
+ cliTruncate('unicorns', 5, {space: false});
85
+ //=> 'unic…'
86
+
87
+ cliTruncate('unicorns', 5, {space: true});
88
+ //=> 'uni …'
89
+
90
+ cliTruncate('unicorns', 6, {position: 'start', space: true});
91
+ //=> '… orns'
92
+
93
+ cliTruncate('unicorns', 7, {position: 'middle', space: true});
94
+ //=> 'uni … s'
95
+ ```
96
+
97
+ ##### preferTruncationOnSpace
98
+
99
+ Type: `boolean`\
100
+ Default: `false`
101
+
102
+ Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
103
+
104
+ ```js
105
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
106
+ //=> '…rainbow dragons'
107
+
108
+ // without preferTruncationOnSpace
109
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'start'})
110
+ //=> '…rns rainbow dragons'
111
+
112
+ cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true})
113
+ //=> 'unicorns…dragons'
114
+
115
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true})
116
+ //=> 'unico…'
117
+
118
+ // preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
119
+ cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true})
120
+ //=> 'uni…ns'
121
+ ```
66
122
 
67
123
  ## Related
68
124
 
@@ -70,6 +126,14 @@ Position to truncate the string.
70
126
  - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
71
127
 
72
128
 
73
- ## License
129
+ ---
74
130
 
75
- MIT © [Sindre Sorhus](https://sindresorhus.com)
131
+ <div align="center">
132
+ <b>
133
+ <a href="https://tidelift.com/subscription/pkg/npm-cli-truncate?utm_source=npm-cli-truncate&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
134
+ </b>
135
+ <br>
136
+ <sub>
137
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
138
+ </sub>
139
+ </div>