export interface EditorPaintStyle { foregroundAnsi: string; backgroundAnsi: string; } /** Reapply only default colors reset by SGR, leaving explicit colors/attributes intact. */ export function paintEditorLine(line: string, style: EditorPaintStyle): string { const painted = line.replace(/\x1b\[([\d;:]*)m/g, (sequence: string, parameters: string) => { const atoms = parameters.split(";"); let resetForeground = false; let resetBackground = false; for (let index = 0; index < atoms.length; index++) { const atom = atoms[index]!; const code = atom === "" ? 0 : Number(atom.split(":")[0]); if (code === 0) { resetForeground = true; resetBackground = true; } else if (code === 39) resetForeground = true; else if (code === 49) resetBackground = true; else if ((code >= 30 && code <= 37) || (code >= 90 && code <= 97)) resetForeground = false; else if ((code >= 40 && code <= 47) || (code >= 100 && code <= 107)) resetBackground = false; else if (code === 38 || code === 48) { if (code === 38) resetForeground = false; else resetBackground = false; // Skip extended color payloads so an RGB channel of 0/39/49 is not a reset. if (!atom.includes(":")) { const mode = Number(atoms[index + 1]); if (mode === 2) index += 4; else if (mode === 5) index += 2; } } } return sequence + (resetForeground ? style.foregroundAnsi : "") + (resetBackground ? style.backgroundAnsi : ""); }); return style.foregroundAnsi + style.backgroundAnsi + painted + "\x1b[0m"; }