@@ -32,10 +32,107 @@ describe("Programmatic Usage: highlightSync", () => {
3232 });
3333 });
3434
35+function renderTerminalOutput(
36+ output: string,
37+ columns = Number.POSITIVE_INFINITY,
38+) {
39+ const screen: string[][] = [[]];
40+ let row = 0;
41+ let column = 0;
42+ let savedCursor: { row: number; column: number } | null = null;
43+
44+ function ensureRow(targetRow: number) {
45+ while (screen.length <= targetRow) {
46+ screen.push([]);
47+ }
48+ }
49+
50+ function writeChar(char: string) {
51+ if (column >= columns) {
52+ row += 1;
53+ column = 0;
54+ }
55+
56+ ensureRow(row);
57+ if (!screen[row]) {
58+ screen[row] = [];
59+ }
60+ const currentLine = screen[row];
61+ if (!currentLine) {
62+ return;
63+ }
64+ currentLine[column] = char;
65+ column += 1;
66+ }
67+
68+ for (let i = 0; i < output.length; i += 1) {
69+ const rest = output.slice(i);
70+
71+ if (rest.startsWith("\x1b7")) {
72+ savedCursor = { row, column };
73+ i += 1;
74+ continue;
75+ }
76+
77+ if (rest.startsWith("\x1b8")) {
78+ if (savedCursor) {
79+ row = savedCursor.row;
80+ column = savedCursor.column;
81+ }
82+ i += 1;
83+ continue;
84+ }
85+
86+ if (rest.startsWith("\x1b[J")) {
87+ ensureRow(row);
88+ screen[row] = (screen[row] ?? []).slice(0, column);
89+ for (let clearRow = row + 1; clearRow < screen.length; clearRow += 1) {
90+ screen[clearRow] = [];
91+ }
92+ i += 2;
93+ continue;
94+ }
95+
96+ if (rest.startsWith("\x1b[2K")) {
97+ ensureRow(row);
98+ screen[row] = [];
99+ i += 3;
100+ continue;
101+ }
102+
103+ if (rest.startsWith("\x1b[1A")) {
104+ row = Math.max(0, row - 1);
105+ i += 3;
106+ continue;
107+ }
108+
109+ const char = output[i];
110+ if (char === "\n") {
111+ row += 1;
112+ column = 0;
113+ ensureRow(row);
114+ continue;
115+ }
116+
117+ if (char === "\r") {
118+ column = 0;
119+ continue;
120+ }
121+
122+ if (char !== undefined && char !== "\x1b") {
123+ writeChar(char);
124+ }
125+ }
126+
127+ return screen
128+ .map((line) => line.join("").replace(/\s+$/u, ""))
129+ .join("\n")
130+ .replace(/\n+$/u, "");
131+}
132+
35133 function expectStreamMatch(res: string, markdown: string) {
36134 const expected = highlightSync(markdown, { theme: "none" });
37- // biome-ignore lint/suspicious/noControlCharactersInRegex: clear line ansi escape
38- expect(res.replace(/[^\n]*\x1b\[2K\r/g, "")).toBe(expected);
135+ expect(renderTerminalOutput(res)).toBe(renderTerminalOutput(expected));
39136 }
40137
41138 describe("Programmatic Usage: createHighlighter (Streaming)", () => {
@@ -82,33 +179,20 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
82179 expectStreamMatch(res, markdown);
83180 });
84181
85- test("should clear multiple lines when wrapped", () => {
86- // Mock columns
87- const originalColumns = process.stdout.columns;
88- Object.defineProperty(process.stdout, "columns", {
89- value: 10,
90- configurable: true,
91- });
182+ test("should redraw wrapped partial lines after an external prefix", () => {
183+ const prefix = "◆ ";
184+ const markdown =
185+ "No `\\x1b[2K` or `\\r` sequences in the streamed text. The ANSI colour codes remain (intentional — those are static style codes from yoctocolors, not cursor-control sequences).\n";
186+ const splitAt = 134;
92187
93188 const highlighter = createHighlighter({ theme: "none" });
94- // 25 chars, columns = 10, so it takes 3 visual lines
95- const chunk1 = "1234567890123456789012345";
96- const chunk2 = "67890\n";
97-
98- let res = highlighter.write(chunk1);
99- res += highlighter.write(chunk2);
189+ let res = prefix;
190+ res += highlighter.write(markdown.slice(0, splitAt));
191+ res += highlighter.write(markdown.slice(splitAt));
100192 res += highlighter.end();
101193
102- // The first chunk is written, then cleared. Since it takes 3 lines,
103- // clear sequence should have \x1b[1A\x1b[2K repeated twice.
104- expect(res).toContain("\x1b[1A\x1b[2K\x1b[1A\x1b[2K");
105-
106- // Restore
107- if (originalColumns !== undefined) {
108- Object.defineProperty(process.stdout, "columns", {
109- value: originalColumns,
110- configurable: true,
111- });
112- }
194+ expect(renderTerminalOutput(res, 120)).toBe(
195+ renderTerminalOutput(`${prefix}${markdown}`, 120),
196+ );
113197 });
114198 });