Skip to content

Commit a7f6c68

Browse files
MarcoDotIOclaude
andauthored
fix(shared): underline styles and colours no longer scramble command output colours (#21542)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent fe199e3 commit a7f6c68

2 files changed

Lines changed: 82 additions & 7 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,50 @@ describe("appendTerminalOutput", () => {
9090
});
9191

9292
describe("appendTerminalOutput edge cases from review", () => {
93+
const boldRed = { color: TERMINAL_PALETTES.dark[1], fontWeight: "600" } as const;
94+
it.each([
95+
[
96+
"a curly underline",
97+
"\u001b[1;31m\u001b[4:3mX",
98+
{ ...boldRed, textDecorationLine: "underline" },
99+
],
100+
["underline off in colon form", "\u001b[1;31m\u001b[4:0mX", boldRed],
101+
["a true-colour underline colour", "\u001b[1;31m\u001b[58;2;255;0;0mX", boldRed],
102+
["a 256-colour underline colour", "\u001b[58;5;31mX", null],
103+
[
104+
"a colon-form underline colour",
105+
"\u001b[4m\u001b[58:2::255:0:0mX",
106+
{ textDecorationLine: "underline" },
107+
],
108+
["extra fields in a colon-form indexed underline colour", "\u001b[58:5:0:32mX", null],
109+
[
110+
"extra fields in a colon-form indexed colour",
111+
"\u001b[38:5:1:4mX",
112+
{ color: TERMINAL_PALETTES.dark[1] },
113+
],
114+
[
115+
"a true colour with its fields after colons",
116+
"\u001b[38;2:255:0:0;1mX",
117+
{ color: "rgb(255,0,0)", fontWeight: "600" },
118+
],
119+
[
120+
"an indexed colour with its index after a colon",
121+
"\u001b[38;5:1;4mX",
122+
{ color: TERMINAL_PALETTES.dark[1], textDecorationLine: "underline" },
123+
],
124+
[
125+
"a semicolon-form colour value that looks like a colour code",
126+
"\u001b[38;5;38;2:1:2:3mX",
127+
{ color: "rgb(0,175,215)", opacity: 0.65 },
128+
],
129+
])("reads %s without disturbing the other attributes", (_, input, style) => {
130+
const text = normalizeTerminalOutput(input).text;
131+
expect(terminalOutputSpans(text, TERMINAL_PALETTES.dark)).toEqual([{ text: "X", style }]);
132+
for (let cut = 1; cut < input.length; cut += 1) {
133+
expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe(text);
134+
}
135+
});
136+
93137
it("leaves the screen and cursor alone for unsupported erase modes", () => {
94138
for (const sequence of ["3K", "4J", "99K", "99J"]) {
95139
const input = `a\nb\nc\u001b[${sequence}X`;

‎packages/shared/src/terminalOutput.ts‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ function applySgr(attributes: Attributes, params: ReadonlyArray<number>): void {
230230
attributes.bg = String(code);
231231
else if (code === 39) attributes.fg = "";
232232
else if (code === 49) attributes.bg = "";
233-
else if (code === 38 || code === 48) {
233+
else if (code === 38 || code === 48 || code === 58) {
234+
// 58 (underline colour) is consumed but not kept: nothing renders it.
234235
const [color, next] = extendedColor(code, params, index);
235-
if (color !== undefined) attributes[code === 38 ? "fg" : "bg"] = color;
236+
if (color !== undefined && code !== 58) attributes[code === 38 ? "fg" : "bg"] = color;
236237
index = next;
237238
continue;
238239
}
@@ -289,14 +290,44 @@ const penTransitions = new Map<string, string>();
289290
// Colon color sub-parameters carry an optional color-space id absent from the semicolon form.
290291
function csiParams(params: string): Array<number> {
291292
const numbers: Array<number> = [];
293+
// After a semicolon-form 38/48/58 the next part is its mode, then that many
294+
// colour values; tracking them keeps a value from being read as a code.
295+
let pending: "code" | "mode" | number = "code";
292296
for (const part of params.split(";")) {
293-
if (!part.includes(":")) {
294-
numbers.push(Number(part));
297+
const sub = part.split(":").map(Number);
298+
const head = sub[0]!;
299+
if (typeof pending === "number") {
300+
numbers.push(head);
301+
pending = pending > 1 ? pending - 1 : "code";
295302
continue;
296303
}
297-
const sub = part.split(":").map(Number);
298-
if ((sub[0] === 38 || sub[0] === 48) && sub[1] === 2 && sub.length >= 6) sub.splice(2, 1);
299-
numbers.push(...sub);
304+
if (pending === "mode") {
305+
pending = "code";
306+
if (sub.length === 1) {
307+
numbers.push(head);
308+
if (head === 5 || head === 2) pending = head === 5 ? 1 : 3;
309+
continue;
310+
}
311+
// xterm also takes the mode's fields after colons (`38;2:255:0:0`).
312+
if (head === 2 && sub.length >= 5) sub.splice(1, 1);
313+
numbers.push(...sub.slice(0, head === 5 ? 2 : head === 2 ? 4 : 1));
314+
continue;
315+
}
316+
if (sub.length === 1) {
317+
numbers.push(head);
318+
if (head === 38 || head === 48 || head === 58) pending = "mode";
319+
continue;
320+
}
321+
if (head !== 38 && head !== 48 && head !== 58) {
322+
// Other sub-parameters pick a style variant (`4:3` curly underline); keep
323+
// only the head, except that `4:0` turns underline off.
324+
numbers.push(head === 4 && sub[1] === 0 ? 24 : head);
325+
continue;
326+
}
327+
if (sub[1] === 2 && sub.length >= 6) sub.splice(2, 1);
328+
// The group is one parameter, so fields past its mode's length must not
329+
// leak out as SGR codes of their own. Indexed colour has no colour-space id.
330+
numbers.push(...sub.slice(0, sub[1] === 5 ? 3 : sub[1] === 2 ? 5 : 2));
300331
}
301332
return numbers;
302333
}

0 commit comments

Comments
 (0)