Skip to content
TypeScript

fix(shared): command output keeps text after an unterminated OSC (#30479)

spoukyiie4ff8433ee8d
diff --git a/packages/shared/src/terminalOutput.test.ts b/packages/shared/src/terminalOutput.test.ts
index 578db819614..adb4ae061bc 100644
--- a/packages/shared/src/terminalOutput.test.ts
+++ b/packages/shared/src/terminalOutput.test.ts
@@ -83,6 +83,9 @@ describe("appendTerminalOutput", () => {
it("drops an escape sequence that never terminates and resumes after it", () => {
expect(feed(["ok\u001b]", "x".repeat(200), "\nafter"]).text).toBe("ok\nafter");
+ expect(feed(["ok\u001b]0;title\u001b", "\u001b[31mred"]).text).toBe(
+ "ok\u001b[0;31mred\u001b[0m",
+ );
});
});
diff --git a/packages/shared/src/terminalOutput.ts b/packages/shared/src/terminalOutput.ts
index afeee8fdf34..5edeb250971 100644
--- a/packages/shared/src/terminalOutput.ts
+++ b/packages/shared/src/terminalOutput.ts
@@ -246,6 +246,8 @@ const STRING_SEQUENCE_INTRODUCERS = new Set(["]", "P", "X", "_", "^"]);
function escapeLength(input: string, index: number): number {
const next = input[index + 1];
if (next === undefined) return -1;
+ // A second ESC restarts the sequence.
+ if (next === ESC) return 1;
if (next === "[") {
for (let cursor = index + 2; cursor < input.length; cursor += 1) {
const code = input.charCodeAt(cursor);
@@ -258,9 +260,10 @@ function escapeLength(input: string, index: number): number {
for (let cursor = index + 2; cursor < input.length; cursor += 1) {
if (input[cursor] === "\u0007") return cursor - index + 1;
if (input[cursor] === "\n") return cursor - index;
+ // Like xterm.js, an ESC that does not start ST ends the string and begins the next sequence.
if (input[cursor] === ESC) {
if (cursor + 1 >= input.length) return -1;
- if (input[cursor + 1] === "\\") return cursor - index + 2;
+ return input[cursor + 1] === "\\" ? cursor - index + 2 : cursor - index;
}
}
return -1;