From ccd406b00fdee7fff72a0719b568e55d66297f20 Mon Sep 17 00:00:00 2001 From: Filip <34747899+neriousy@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:07:23 +0100 Subject: [PATCH] fix(tui): prevent truncateMiddle overflow at small widths (#60287) --- packages/tui/src/util/locale.ts | 2 ++ packages/tui/test/util/locale.test.ts | 12 ++++++++++++ packages/util/src/path.test.ts | 11 +++++++++++ packages/util/src/path.ts | 2 ++ 4 files changed, 27 insertions(+) diff --git a/packages/tui/src/util/locale.ts b/packages/tui/src/util/locale.ts index 4dedfdb8b81..e5efebcbb2c 100644 --- a/packages/tui/src/util/locale.ts +++ b/packages/tui/src/util/locale.ts @@ -67,11 +67,13 @@ export function truncateLeft(str: string, len: number): string { } export function truncateMiddle(str: string, maxLength: number = 35): string { + if (maxLength <= 0) return "" if (str.length <= maxLength) return str const ellipsis = "…" const keepStart = Math.ceil((maxLength - ellipsis.length) / 2) const keepEnd = Math.floor((maxLength - ellipsis.length) / 2) + if (keepEnd <= 0) return str.slice(0, keepStart) + ellipsis return str.slice(0, keepStart) + ellipsis + str.slice(-keepEnd) } diff --git a/packages/tui/test/util/locale.test.ts b/packages/tui/test/util/locale.test.ts index 2d5a008f0ae..b6a1547b78a 100644 --- a/packages/tui/test/util/locale.test.ts +++ b/packages/tui/test/util/locale.test.ts @@ -18,3 +18,15 @@ test("rolls the compact form over at the rounding boundary", () => { expect(Locale.number(999950)).toBe("1.0M") expect(Locale.number(1000000)).toBe("1.0M") }) + +test("truncates text in the middle without overflowing small widths", () => { + expect(Locale.truncateMiddle("abcdefghij", 0)).toBe("") + expect(Locale.truncateMiddle("abcdefghij", 1)).toBe("…") + expect(Locale.truncateMiddle("abcdefghij", 2)).toBe("a…") + expect(Locale.truncateMiddle("abcdefghij", 3)).toBe("a…j") + expect(Locale.truncateMiddle("abcdefghij", 4)).toBe("ab…j") + expect(Locale.truncateMiddle("abcdefghij", 9)).toBe("abcd…ghij") + expect(Locale.truncateMiddle("abcd", 4)).toBe("abcd") + expect(Locale.truncateMiddle("abcd", 10)).toBe("abcd") + expect(Locale.truncateMiddle("a", 1)).toBe("a") +}) diff --git a/packages/util/src/path.test.ts b/packages/util/src/path.test.ts index 33c37f52b93..36f073016b6 100644 --- a/packages/util/src/path.test.ts +++ b/packages/util/src/path.test.ts @@ -37,6 +37,17 @@ describe("client paths", () => { expect(getFilenameTruncated("/repo/long-component-name.tsx", 16)).toBe("long-compon….tsx") expect(truncateMiddle("abcdefghijklmnop", 9)).toBe("abcd…mnop") }) + + test("truncates in the middle without overflowing small widths", () => { + expect(truncateMiddle("abcdefghij", 0)).toBe("") + expect(truncateMiddle("abcdefghij", 1)).toBe("…") + expect(truncateMiddle("abcdefghij", 2)).toBe("a…") + expect(truncateMiddle("abcdefghij", 3)).toBe("a…j") + expect(truncateMiddle("abcdefghij", 4)).toBe("ab…j") + expect(truncateMiddle("abcd", 4)).toBe("abcd") + expect(truncateMiddle("abcd", 10)).toBe("abcd") + expect(truncateMiddle("a", 1)).toBe("a") + }) }) describe("encodeFilePath", () => { diff --git a/packages/util/src/path.ts b/packages/util/src/path.ts index 1af29328a84..761c4708d99 100644 --- a/packages/util/src/path.ts +++ b/packages/util/src/path.ts @@ -23,10 +23,12 @@ export function getFilenameTruncated(path: string | undefined, maxLength = 20) { } export function truncateMiddle(text: string, maxLength = 20) { + if (maxLength <= 0) return "" if (text.length <= maxLength) return text const available = maxLength - 1 const start = Math.ceil(available / 2) const end = Math.floor(available / 2) + if (end <= 0) return text.slice(0, start) + "…" return text.slice(0, start) + "…" + text.slice(-end) }