@cjser/cli-truncate 6.0.0-cjser.2 → 6.0.1-cjser.2

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.
@@ -26,7 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  ));
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
 
29
- // packages/@cjser/cli-truncate/index.js
29
+ // packages/@cjser/cli-truncate.tmp-26-1782231691521/index.js
30
30
  var index_exports = {};
31
31
  __export(index_exports, {
32
32
  default: () => cliTruncate
@@ -135,14 +135,18 @@ function cliTruncate(text, columns, options = {}) {
135
135
  if (position === "middle") {
136
136
  if (space) {
137
137
  truncationCharacter = ` ${truncationCharacter} `;
138
+ if ((0, import_string_width.default)(truncationCharacter) >= columns) {
139
+ truncationCharacter = truncationCharacter.trim();
140
+ }
138
141
  }
139
- const half = Math.floor(columns / 2);
142
+ const truncationWidth = (0, import_string_width.default)(truncationCharacter);
143
+ const half = Math.min(Math.floor(columns / 2), Math.max(0, columns - truncationWidth));
140
144
  if (preferTruncationOnSpace) {
141
145
  const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
142
- const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
146
+ const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + truncationWidth, true);
143
147
  return (0, import_slice_ansi.default)(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + (0, import_slice_ansi.default)(text, spaceNearSecondBreakPoint, length).trim();
144
148
  }
145
- return (0, import_slice_ansi.default)(text, 0, half) + truncationCharacter + (0, import_slice_ansi.default)(text, length - (columns - half) + (0, import_string_width.default)(truncationCharacter), length);
149
+ return (0, import_slice_ansi.default)(text, 0, half) + truncationCharacter + (0, import_slice_ansi.default)(text, length - (columns - half) + truncationWidth, length);
146
150
  }
147
151
  if (position === "end") {
148
152
  if (preferTruncationOnSpace) {
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": "@cjser/cli-truncate",
3
- "version": "6.0.0-cjser.2",
3
+ "version": "6.0.1-cjser.2",
4
4
  "description": "Truncate a string to a specific width in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -57,11 +57,11 @@
57
57
  "types": "./index.d.ts",
58
58
  "main": "./dist-cjser/index.cjs",
59
59
  "cjser": {
60
- "sourceVersion": "6.0.0",
60
+ "sourceVersion": "6.0.1",
61
61
  "cjserVersion": 2,
62
62
  "original": {
63
63
  "name": "cli-truncate",
64
- "version": "6.0.0",
64
+ "version": "6.0.1",
65
65
  "exports": {
66
66
  "types": "./index.d.ts",
67
67
  "default": "./index.js"