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