Skip to content

Commit 0a549c8

Browse files
perf(shared): skip parsing cached terminal styles
1 parent 37fbefd commit 0a549c8

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
@@ -103,10 +103,15 @@ describe("appendTerminalOutput edge cases from review", () => {
103103

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

‎packages/shared/src/terminalOutput.ts‎

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

390390
csi(params: string, final: string): void {
391391
if (/^[?>=]/u.test(params)) return; // Mode switches (cursor visibility, bracketed paste).
392+
if (final === "m") {
393+
const key = `${this.pen}|${params}`;
394+
let pen = penTransitions.get(key);
395+
if (pen === undefined) {
396+
const attributes = parsePen(this.pen);
397+
applySgr(attributes, params === "" ? [] : csiParams(params));
398+
pen = formatPen(attributes);
399+
if (penTransitions.size >= 512) penTransitions.clear();
400+
penTransitions.set(key, pen);
401+
}
402+
this.pen = pen;
403+
return;
404+
}
392405
const numbers = params === "" ? [] : csiParams(params);
393406
const count = Math.max(1, numbers[0] || 1);
394407
switch (final) {
395-
case "m": {
396-
const key = `${this.pen}|${params}`;
397-
let pen = penTransitions.get(key);
398-
if (pen === undefined) {
399-
const attributes = parsePen(this.pen);
400-
applySgr(attributes, numbers);
401-
pen = formatPen(attributes);
402-
if (penTransitions.size >= 512) penTransitions.clear();
403-
penTransitions.set(key, pen);
404-
}
405-
this.pen = pen;
406-
return;
407-
}
408408
case "A":
409409
return this.moveTo(this.row - count, this.col);
410410
case "B":

0 commit comments

Comments
 (0)