cli-truncate 3.0.0 → 4.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.
- package/index.d.ts +24 -4
- package/index.js +24 -30
- package/package.json +10 -7
- package/readme.md +25 -17
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Options = {
|
|
2
2
|
/**
|
|
3
3
|
The position to truncate the string.
|
|
4
4
|
|
|
@@ -50,12 +50,32 @@ export interface Options {
|
|
|
50
50
|
````
|
|
51
51
|
*/
|
|
52
52
|
readonly preferTruncationOnSpace?: boolean;
|
|
53
|
-
|
|
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
|
+
};
|
|
54
74
|
|
|
55
75
|
/**
|
|
56
76
|
Truncate a string to a specific width in the terminal.
|
|
57
77
|
|
|
58
|
-
@param text -
|
|
78
|
+
@param text - The text to truncate.
|
|
59
79
|
@param columns - The number of columns to occupy in the terminal.
|
|
60
80
|
|
|
61
81
|
@example
|
|
@@ -85,7 +105,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
85
105
|
|
|
86
106
|
// Truncate the paragraph to the terminal width
|
|
87
107
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
88
|
-
cliTruncate(paragraph, process.stdout.columns)
|
|
108
|
+
cliTruncate(paragraph, process.stdout.columns);
|
|
89
109
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
90
110
|
```
|
|
91
111
|
*/
|
package/index.js
CHANGED
|
@@ -6,29 +6,26 @@ function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
|
6
6
|
return wantedIndex;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return wantedIndex - index;
|
|
9
|
+
const direction = shouldSearchRight ? 1 : -1;
|
|
10
|
+
|
|
11
|
+
for (let index = 0; index <= 3; index++) {
|
|
12
|
+
const finalIndex = wantedIndex + (index * direction);
|
|
13
|
+
if (string.charAt(finalIndex) === ' ') {
|
|
14
|
+
return finalIndex;
|
|
16
15
|
}
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
return wantedIndex;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
export default function cliTruncate(text, columns, options) {
|
|
23
|
-
|
|
24
|
-
position
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
21
|
+
export default function cliTruncate(text, columns, options = {}) {
|
|
22
|
+
const {
|
|
23
|
+
position = 'end',
|
|
24
|
+
space = false,
|
|
25
|
+
preferTruncationOnSpace = false,
|
|
26
|
+
} = options;
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
let ellipsis = '…';
|
|
31
|
-
let ellipsisWidth = 1;
|
|
28
|
+
let {truncationCharacter = '…'} = options;
|
|
32
29
|
|
|
33
30
|
if (typeof text !== 'string') {
|
|
34
31
|
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
@@ -43,7 +40,7 @@ export default function cliTruncate(text, columns, options) {
|
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
if (columns === 1) {
|
|
46
|
-
return
|
|
43
|
+
return truncationCharacter;
|
|
47
44
|
}
|
|
48
45
|
|
|
49
46
|
const length = stringWidth(text);
|
|
@@ -55,21 +52,19 @@ export default function cliTruncate(text, columns, options) {
|
|
|
55
52
|
if (position === 'start') {
|
|
56
53
|
if (preferTruncationOnSpace) {
|
|
57
54
|
const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
|
|
58
|
-
return
|
|
55
|
+
return truncationCharacter + sliceAnsi(text, nearestSpace, length).trim();
|
|
59
56
|
}
|
|
60
57
|
|
|
61
58
|
if (space === true) {
|
|
62
|
-
|
|
63
|
-
ellipsisWidth = 2;
|
|
59
|
+
truncationCharacter += ' ';
|
|
64
60
|
}
|
|
65
61
|
|
|
66
|
-
return
|
|
62
|
+
return truncationCharacter + sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
|
|
67
63
|
}
|
|
68
64
|
|
|
69
65
|
if (position === 'middle') {
|
|
70
66
|
if (space === true) {
|
|
71
|
-
|
|
72
|
-
ellipsisWidth = 3;
|
|
67
|
+
truncationCharacter = ` ${truncationCharacter} `;
|
|
73
68
|
}
|
|
74
69
|
|
|
75
70
|
const half = Math.floor(columns / 2);
|
|
@@ -77,28 +72,27 @@ export default function cliTruncate(text, columns, options) {
|
|
|
77
72
|
if (preferTruncationOnSpace) {
|
|
78
73
|
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
|
79
74
|
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
|
|
80
|
-
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) +
|
|
75
|
+
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
|
81
76
|
}
|
|
82
77
|
|
|
83
78
|
return (
|
|
84
79
|
sliceAnsi(text, 0, half)
|
|
85
|
-
+
|
|
86
|
-
+ sliceAnsi(text, length - (columns - half) +
|
|
80
|
+
+ truncationCharacter
|
|
81
|
+
+ sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length)
|
|
87
82
|
);
|
|
88
83
|
}
|
|
89
84
|
|
|
90
85
|
if (position === 'end') {
|
|
91
86
|
if (preferTruncationOnSpace) {
|
|
92
87
|
const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
|
|
93
|
-
return sliceAnsi(text, 0, nearestSpace) +
|
|
88
|
+
return sliceAnsi(text, 0, nearestSpace) + truncationCharacter;
|
|
94
89
|
}
|
|
95
90
|
|
|
96
91
|
if (space === true) {
|
|
97
|
-
|
|
98
|
-
ellipsisWidth = 2;
|
|
92
|
+
truncationCharacter = ` ${truncationCharacter}`;
|
|
99
93
|
}
|
|
100
94
|
|
|
101
|
-
return sliceAnsi(text, 0, columns -
|
|
95
|
+
return sliceAnsi(text, 0, columns - stringWidth(truncationCharacter)) + truncationCharacter;
|
|
102
96
|
}
|
|
103
97
|
|
|
104
98
|
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-truncate",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Truncate a string to a specific width in the terminal",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/cli-truncate",
|
|
@@ -11,9 +11,12 @@
|
|
|
11
11
|
"url": "https://sindresorhus.com"
|
|
12
12
|
},
|
|
13
13
|
"type": "module",
|
|
14
|
-
"exports":
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
15
18
|
"engines": {
|
|
16
|
-
"node": "
|
|
19
|
+
"node": ">=18"
|
|
17
20
|
},
|
|
18
21
|
"scripts": {
|
|
19
22
|
"test": "xo && ava && tsd"
|
|
@@ -38,11 +41,11 @@
|
|
|
38
41
|
],
|
|
39
42
|
"dependencies": {
|
|
40
43
|
"slice-ansi": "^5.0.0",
|
|
41
|
-
"string-width": "^
|
|
44
|
+
"string-width": "^7.0.0"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
|
-
"ava": "^3.
|
|
45
|
-
"tsd": "^0.
|
|
46
|
-
"xo": "^0.
|
|
47
|
+
"ava": "^5.3.1",
|
|
48
|
+
"tsd": "^0.29.0",
|
|
49
|
+
"xo": "^0.56.0"
|
|
47
50
|
}
|
|
48
51
|
}
|
package/readme.md
CHANGED
|
@@ -6,8 +6,8 @@ Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code
|
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
|
|
9
|
+
```sh
|
|
10
|
+
npm install cli-truncate
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
@@ -41,7 +41,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
41
41
|
|
|
42
42
|
// Truncate the paragraph to the terminal width
|
|
43
43
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
44
|
-
cliTruncate(paragraph, process.stdout.columns)
|
|
44
|
+
cliTruncate(paragraph, process.stdout.columns);
|
|
45
45
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
46
46
|
```
|
|
47
47
|
|
|
@@ -53,7 +53,7 @@ cliTruncate(paragraph, process.stdout.columns));
|
|
|
53
53
|
|
|
54
54
|
Type: `string`
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
The text to truncate.
|
|
57
57
|
|
|
58
58
|
#### columns
|
|
59
59
|
|
|
@@ -69,7 +69,7 @@ Type: `object`
|
|
|
69
69
|
|
|
70
70
|
Type: `string`\
|
|
71
71
|
Default: `'end'`\
|
|
72
|
-
Values: `'start'
|
|
72
|
+
Values: `'start' | 'middle' | 'end'`
|
|
73
73
|
|
|
74
74
|
The position to truncate the string.
|
|
75
75
|
|
|
@@ -124,19 +124,27 @@ cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncation
|
|
|
124
124
|
//=> 'uni…ns'
|
|
125
125
|
```
|
|
126
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
|
+
```
|
|
146
|
+
|
|
127
147
|
## Related
|
|
128
148
|
|
|
129
149
|
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
|
130
150
|
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
|
131
|
-
|
|
132
|
-
---
|
|
133
|
-
|
|
134
|
-
<div align="center">
|
|
135
|
-
<b>
|
|
136
|
-
<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>
|
|
137
|
-
</b>
|
|
138
|
-
<br>
|
|
139
|
-
<sub>
|
|
140
|
-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
141
|
-
</sub>
|
|
142
|
-
</div>
|