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 CHANGED
@@ -114,5 +114,5 @@ cliTruncate(paragraph, process.stdout.columns);
114
114
  export default function cliTruncate(
115
115
  text: string,
116
116
  columns: number,
117
- options?: Options
117
+ options?: Options,
118
118
  ): string;
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 half = Math.floor(columns / 2);
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) + 1, true);
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) + stringWidth(truncationCharacter), length)
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": "5.2.0",
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"
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": "^8.0.0",
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": "^1.2.2"
50
+ "xo": "^2.0.2"
51
51
  }
52
52
  }
package/readme.md CHANGED
@@ -145,7 +145,6 @@ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
145
145
 
146
146
  cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
147
147
  //=> 'unico'
148
-
149
148
  ```
150
149
 
151
150
  ## Related