cli-truncate 5.2.0 → 6.0.1
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 +1 -1
- package/index.js +13 -3
- package/package.json +4 -4
- package/readme.md +0 -1
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -132,20 +132,30 @@ export default function cliTruncate(text, columns, options = {}) {
|
|
|
132
132
|
if (position === 'middle') {
|
|
133
133
|
if (space) {
|
|
134
134
|
truncationCharacter = ` ${truncationCharacter} `;
|
|
135
|
+
|
|
136
|
+
// Drop the padding spaces if the padded character does not fit, so the
|
|
137
|
+
// truncation character itself never exceeds the budget.
|
|
138
|
+
if (stringWidth(truncationCharacter) >= columns) {
|
|
139
|
+
truncationCharacter = truncationCharacter.trim();
|
|
140
|
+
}
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
const
|
|
143
|
+
const truncationWidth = stringWidth(truncationCharacter);
|
|
144
|
+
// Reserve room for the truncation character before splitting the budget
|
|
145
|
+
// between the two sides, otherwise small budgets overflow (e.g. a width of
|
|
146
|
+
// 4 was returned for `columns: 2`).
|
|
147
|
+
const half = Math.min(Math.floor(columns / 2), Math.max(0, columns - truncationWidth));
|
|
138
148
|
|
|
139
149
|
if (preferTruncationOnSpace) {
|
|
140
150
|
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
|
141
|
-
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) +
|
|
151
|
+
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + truncationWidth, true);
|
|
142
152
|
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
|
143
153
|
}
|
|
144
154
|
|
|
145
155
|
return (
|
|
146
156
|
sliceAnsi(text, 0, half)
|
|
147
157
|
+ truncationCharacter
|
|
148
|
-
+ sliceAnsi(text, length - (columns - half) +
|
|
158
|
+
+ sliceAnsi(text, length - (columns - half) + truncationWidth, length)
|
|
149
159
|
);
|
|
150
160
|
}
|
|
151
161
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-truncate",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Truncate a string to a specific width in the terminal",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/cli-truncate",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
20
|
+
"node": ">=22"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "xo && ava && tsd"
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
"string"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"slice-ansi": "^
|
|
44
|
+
"slice-ansi": "^9.0.0",
|
|
45
45
|
"string-width": "^8.2.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"ava": "^7.0.0",
|
|
49
49
|
"tsd": "^0.33.0",
|
|
50
|
-
"xo": "^
|
|
50
|
+
"xo": "^2.0.2"
|
|
51
51
|
}
|
|
52
52
|
}
|