cli-truncate 6.0.0 → 6.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 +2 -2
- package/index.js +38 -12
- package/package.json +2 -2
- package/readme.md +2 -2
package/index.d.ts
CHANGED
|
@@ -76,7 +76,7 @@ export type Options = {
|
|
|
76
76
|
Truncate a string to a specific width in the terminal.
|
|
77
77
|
|
|
78
78
|
@param text - The text to truncate.
|
|
79
|
-
@param columns - The number of columns to occupy in the terminal.
|
|
79
|
+
@param columns - The finite number of columns to occupy in the terminal.
|
|
80
80
|
|
|
81
81
|
@example
|
|
82
82
|
```
|
|
@@ -107,7 +107,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
107
107
|
|
|
108
108
|
// Truncate the paragraph to the terminal width
|
|
109
109
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
110
|
-
cliTruncate(paragraph, process.stdout.columns);
|
|
110
|
+
cliTruncate(paragraph, process.stdout.columns ?? 80);
|
|
111
111
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
112
112
|
```
|
|
113
113
|
*/
|
package/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import sliceAnsi from 'slice-ansi';
|
|
2
2
|
import stringWidth from 'string-width';
|
|
3
3
|
|
|
4
|
+
const validPositions = new Set(['start', 'middle', 'end']);
|
|
5
|
+
|
|
4
6
|
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
5
7
|
if (string.charAt(wantedIndex) === ' ') {
|
|
6
8
|
return wantedIndex;
|
|
@@ -18,6 +20,28 @@ function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
|
18
20
|
return wantedIndex;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
function validateInput(text, columns, position, truncationCharacter) {
|
|
24
|
+
if (typeof text !== 'string') {
|
|
25
|
+
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof columns !== 'number') {
|
|
29
|
+
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!Number.isFinite(columns)) {
|
|
33
|
+
throw new TypeError(`Expected \`columns\` to be a finite number, got ${columns}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!validPositions.has(position)) {
|
|
37
|
+
throw new TypeError(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (typeof truncationCharacter !== 'string') {
|
|
41
|
+
throw new TypeError(`Expected \`options.truncationCharacter\` to be a string, got ${typeof truncationCharacter}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
21
45
|
export default function cliTruncate(text, columns, options = {}) {
|
|
22
46
|
const {
|
|
23
47
|
position = 'end',
|
|
@@ -27,13 +51,7 @@ export default function cliTruncate(text, columns, options = {}) {
|
|
|
27
51
|
|
|
28
52
|
let {truncationCharacter = '…'} = options;
|
|
29
53
|
|
|
30
|
-
|
|
31
|
-
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (typeof columns !== 'number') {
|
|
35
|
-
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
|
|
36
|
-
}
|
|
54
|
+
validateInput(text, columns, position, truncationCharacter);
|
|
37
55
|
|
|
38
56
|
if (columns < 1) {
|
|
39
57
|
return '';
|
|
@@ -132,20 +150,30 @@ export default function cliTruncate(text, columns, options = {}) {
|
|
|
132
150
|
if (position === 'middle') {
|
|
133
151
|
if (space) {
|
|
134
152
|
truncationCharacter = ` ${truncationCharacter} `;
|
|
153
|
+
|
|
154
|
+
// Drop the padding spaces if the padded character does not fit, so the
|
|
155
|
+
// truncation character itself never exceeds the budget.
|
|
156
|
+
if (stringWidth(truncationCharacter) >= columns) {
|
|
157
|
+
truncationCharacter = truncationCharacter.trim();
|
|
158
|
+
}
|
|
135
159
|
}
|
|
136
160
|
|
|
137
|
-
const
|
|
161
|
+
const truncationWidth = stringWidth(truncationCharacter);
|
|
162
|
+
// Reserve room for the truncation character before splitting the budget
|
|
163
|
+
// between the two sides, otherwise small budgets overflow (e.g. a width of
|
|
164
|
+
// 4 was returned for `columns: 2`).
|
|
165
|
+
const half = Math.min(Math.floor(columns / 2), Math.max(0, columns - truncationWidth));
|
|
138
166
|
|
|
139
167
|
if (preferTruncationOnSpace) {
|
|
140
168
|
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
|
141
|
-
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) +
|
|
169
|
+
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + truncationWidth, true);
|
|
142
170
|
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
|
143
171
|
}
|
|
144
172
|
|
|
145
173
|
return (
|
|
146
174
|
sliceAnsi(text, 0, half)
|
|
147
175
|
+ truncationCharacter
|
|
148
|
-
+ sliceAnsi(text, length - (columns - half) +
|
|
176
|
+
+ sliceAnsi(text, length - (columns - half) + truncationWidth, length)
|
|
149
177
|
);
|
|
150
178
|
}
|
|
151
179
|
|
|
@@ -163,6 +191,4 @@ export default function cliTruncate(text, columns, options = {}) {
|
|
|
163
191
|
const left = sliceAnsi(text, 0, columns - stringWidth(truncationCharacter));
|
|
164
192
|
return appendWithInheritedStyleFromEnd(left, truncationCharacter);
|
|
165
193
|
}
|
|
166
|
-
|
|
167
|
-
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
|
168
194
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-truncate",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"description": "Truncate a string to a specific width in the terminal",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"repository": "sindresorhus/cli-truncate",
|
|
6
|
+
"repository": "github:sindresorhus/cli-truncate",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
8
|
"author": {
|
|
9
9
|
"name": "Sindre Sorhus",
|
package/readme.md
CHANGED
|
@@ -44,7 +44,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
44
44
|
|
|
45
45
|
// Truncate the paragraph to the terminal width
|
|
46
46
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
47
|
-
cliTruncate(paragraph, process.stdout.columns);
|
|
47
|
+
cliTruncate(paragraph, process.stdout.columns ?? 80);
|
|
48
48
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
49
49
|
```
|
|
50
50
|
|
|
@@ -62,7 +62,7 @@ The text to truncate.
|
|
|
62
62
|
|
|
63
63
|
Type: `number`
|
|
64
64
|
|
|
65
|
-
The number of columns to occupy in the terminal.
|
|
65
|
+
The finite number of columns to occupy in the terminal.
|
|
66
66
|
|
|
67
67
|
#### options
|
|
68
68
|
|