Skip to content

Commit eb8f5e9

Browse files
committed
fix: clamp ANSI-aware padding in buildColored to prevent RangeError
String.repeat() throws when given a negative count. The pad helper was computing innerWidth - 2 - s.length on strings that already contained ANSI escape codes, so the invisible bytes inflated s.length past the box width. Strip ANSI codes via a regex before measuring visible length and add Math.max(0, …) as a safety guard.
1 parent 83da9db commit eb8f5e9

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/ui/logo.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ function buildPlain(): string {
5959

6060
// ── Build coloured box (synchronous ANSI, no chalk needed) ──────────────────
6161

62+
// Strip ANSI escape codes to get the visible (printable) length of a string.
63+
const visibleLength = (s: string): number =>
64+
s.replace(/\x1b\[[0-9;]*m/g, '').length;
65+
6266
function buildColored(): string {
63-
const pad = (s: string) => s + ' '.repeat(innerWidth - 2 - s.length);
67+
const pad = (s: string) => s + ' '.repeat(Math.max(0, innerWidth - 2 - visibleLength(s)));
6468
const blank = ansi(A.dim, `│${' '.repeat(innerWidth)}│`);
6569

6670
const boxLine = (middle: string) =>

0 commit comments

Comments
 (0)