Skip to content

Commit cd890ba

Browse files
geezoriageezoria1337claude
authored
fix(shared): command output no longer prints the final byte of escape sequences with intermediates (pingdotgg#30724)
Co-authored-by: Geezy <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b60a89f commit cd890ba

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎packages/shared/src/terminalOutput.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ describe("appendTerminalOutput under adversarial output", () => {
131131
}
132132
});
133133

134+
it("drops escape sequences with intermediate bytes without printing their final byte", () => {
135+
for (const sequence of ["#8", "#6", "%G", " F", "-A", ".A", "(%5", "(B"]) {
136+
const input = `a\u001b${sequence}b`;
137+
expect(feed([input]).text).toBe("ab");
138+
for (let cut = 1; cut < input.length; cut += 1) {
139+
expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("ab");
140+
}
141+
}
142+
for (const [interrupted, expected] of [
143+
["a\u001b#\u001b[31mb", feed(["a\u001b[31mb"]).text],
144+
["a\u001b#\n8b", "a\n8b"],
145+
] as const) {
146+
for (let cut = 0; cut < interrupted.length; cut += 1) {
147+
expect(feed([interrupted.slice(0, cut), interrupted.slice(cut)]).text).toBe(expected);
148+
}
149+
}
150+
});
151+
134152
it("gives the same result however the output is split into chunks", () => {
135153
const input =
136154
"abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" +

‎packages/shared/src/terminalOutput.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,20 @@ function escapeLength(input: string, index: number): number {
268268
}
269269
return -1;
270270
}
271-
// Charset designations take three bytes; other sequences take two.
272-
if (next === "(" || next === ")" || next === "*" || next === "+") {
273-
return index + 2 < input.length ? 3 : -1;
271+
// ECMA-35: any intermediate bytes (0x20-0x2F, e.g. charset designations, DECALN) precede one final byte.
272+
let cursor = index + 1;
273+
while (
274+
cursor < input.length &&
275+
input.charCodeAt(cursor) >= 0x20 &&
276+
input.charCodeAt(cursor) <= 0x2f
277+
) {
278+
cursor += 1;
274279
}
275-
return 2;
280+
if (cursor >= input.length) return -1;
281+
// After intermediates, a control or ESC is not a final byte; end before it so it is still processed.
282+
const final = input.charCodeAt(cursor);
283+
if (cursor > index + 1 && (final < 0x30 || final > 0x7e)) return cursor - index;
284+
return cursor - index + 1;
276285
}
277286

278287
const penTransitions = new Map<string, string>();

0 commit comments

Comments
 (0)