Skip to content

Commit e8b0856

Browse files
perf(shared): skip parsing cached terminal styles (#27041)
1 parent 88f6113 commit e8b0856

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,15 @@ describe("appendTerminalOutput edge cases from review", () => {
106106

107107
describe("appendTerminalOutput under adversarial output", () => {
108108
it("gives the same result however the output is split into chunks", () => {
109-
const input = "abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n";
110-
const whole = feed([input], 3).text;
111-
for (let cut = 1; cut < input.length; cut += 1) {
112-
expect(feed([input.slice(0, cut), input.slice(cut)], 3).text).toBe(whole);
109+
const input =
110+
"abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" +
111+
"\u001b[1;31mbold red\u001b[mplain\u001b[38:2::12:34:56mrgb\u001b[0m\n" +
112+
"\u001b[1;31mbold red again\u001b[mplain again\u001b[38:2::12:34:56mrgb again\u001b[0m";
113+
for (const maxChars of [3, 1_024]) {
114+
const whole = feed([input], maxChars).text;
115+
for (let cut = 1; cut < input.length; cut += 1) {
116+
expect(feed([input.slice(0, cut), input.slice(cut)], maxChars).text).toBe(whole);
117+
}
113118
}
114119
expect(feed(["abcdef", "\rX"], 3).text).toBe(feed(["abcdef\rX"], 3).text);
115120
});

‎packages/shared/src/terminalOutput.ts‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -394,22 +394,22 @@ class Terminal {
394394

395395
csi(params: string, final: string): void {
396396
if (/^[?>=]/u.test(params)) return; // Mode switches (cursor visibility, bracketed paste).
397+
if (final === "m") {
398+
const key = `${this.pen}|${params}`;
399+
let pen = penTransitions.get(key);
400+
if (pen === undefined) {
401+
const attributes = parsePen(this.pen);
402+
applySgr(attributes, params === "" ? [] : csiParams(params));
403+
pen = formatPen(attributes);
404+
if (penTransitions.size >= 512) penTransitions.clear();
405+
penTransitions.set(key, pen);
406+
}
407+
this.pen = pen;
408+
return;
409+
}
397410
const numbers = params === "" ? [] : csiParams(params);
398411
const count = Math.max(1, numbers[0] || 1);
399412
switch (final) {
400-
case "m": {
401-
const key = `${this.pen}|${params}`;
402-
let pen = penTransitions.get(key);
403-
if (pen === undefined) {
404-
const attributes = parsePen(this.pen);
405-
applySgr(attributes, numbers);
406-
pen = formatPen(attributes);
407-
if (penTransitions.size >= 512) penTransitions.clear();
408-
penTransitions.set(key, pen);
409-
}
410-
this.pen = pen;
411-
return;
412-
}
413413
case "A":
414414
return this.moveTo(this.row - count, this.col);
415415
case "B":

0 commit comments

Comments
 (0)