Skip to content

Commit 8218479

Browse files
committed
fix(models): sanitize formatter input before measuring width
1 parent 8352309 commit 8218479

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/commands/models/list.format.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ describe("truncate", () => {
4343
expect(result).toBe("");
4444
expect(hasLoneSurrogate(result)).toBe(false);
4545
});
46+
47+
it("sanitizes terminal controls before measuring visible width", () => {
48+
expect(truncate("ab\u001B]2;hidden\u0007cd", 6)).toBe("abcd");
49+
});
4650
});
4751

4852
describe("pad", () => {

src/commands/models/list.format.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Formatting helpers for model-list terminal tables. */
22
import { truncateToVisibleWidth, visibleWidth } from "../../../packages/terminal-core/src/ansi.js";
3+
import { sanitizeTerminalText } from "../../../packages/terminal-core/src/safe-text.js";
34
import { isRich as isRichTerminal, theme } from "../../../packages/terminal-core/src/theme.js";
45

56
const TRUNCATED_SUFFIX = "...";
@@ -45,11 +46,12 @@ export const formatTag = (tag: string, rich: boolean) => {
4546

4647
/** Truncates model-list cells to terminal visible width with an ASCII ellipsis. */
4748
export const truncate = (value: string, max: number) => {
48-
if (visibleWidth(value) <= max) {
49-
return value;
49+
const sanitized = sanitizeTerminalText(value);
50+
if (visibleWidth(sanitized) <= max) {
51+
return sanitized;
5052
}
5153
if (max <= TRUNCATED_SUFFIX.length) {
52-
return truncateToVisibleWidth(value, max);
54+
return truncateToVisibleWidth(sanitized, max);
5355
}
54-
return `${truncateToVisibleWidth(value, max - TRUNCATED_SUFFIX.length)}${TRUNCATED_SUFFIX}`;
56+
return `${truncateToVisibleWidth(sanitized, max - TRUNCATED_SUFFIX.length)}${TRUNCATED_SUFFIX}`;
5557
};

0 commit comments

Comments
 (0)