@@ -12,6 +12,20 @@ export interface Highlighter {
1212 end(): string;
1313 }
1414
15+function resolveColumns(options?: Options): number {
16+ const wrap = options?.wordWrap ?? "auto";
17+ if (typeof wrap === "number" && wrap > 0) return wrap;
18+ if (wrap === "auto") {
19+ try {
20+ return process.stdout.columns || 80;
21+ } catch {
22+ return 80;
23+ }
24+ }
25+ // wordWrap === 0 (disabled) – use a large value so visual-line count stays 1
26+ return Number.MAX_SAFE_INTEGER;
27+}
28+
1529 function resolveTheme(options?: Options): Theme {
1630 const t = options?.theme || "16";
1731 if (typeof t === "string") {
@@ -28,6 +42,7 @@ export function highlightSync(input: string, options?: Options): string {
2842
2943 export function createHighlighter(options?: Options): Highlighter {
3044 const theme = resolveTheme(options);
45+ const terminalColumns = resolveColumns(options);
3146 let buffer = "";
3247 let inCodeBlock = false;
3348
@@ -127,14 +142,38 @@ export function createHighlighter(options?: Options): Highlighter {
127142 return parseInline(line);
128143 }
129144
130- let hasPartial = false;
145+ let previousPartialLineCount = 0;
131146
132- function redrawPartialPrefix(): string {
133- return hasPartial ? "\x1b8\x1b[J" : "";
147+ // Strip ANSI escapes to measure visible character width
148+ function visibleLength(s: string): number {
149+ // biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape regex
150+ return s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, "").length;
134151 }
135152
136- function renderPartial(line: string, isPartial: boolean): string {
137- return `\x1b7${parseLine(line, isPartial)}`;
153+ // Count visual (wrapped) lines a rendered string occupies.
154+ function countVisualLines(rendered: string): number {
155+ const len = visibleLength(rendered);
156+ if (len === 0) return 1;
157+ return Math.ceil(len / terminalColumns);
158+ }
159+
160+ // Erase the previous partial render using the standard eraseLines
161+ // pattern (same as log-update / ansi-escapes). All movement is
162+ // relative so it is immune to terminal scroll, unlike DECSC/DECRC.
163+ //
164+ // eraseLines(n) = for each line: eraseLine + cursorUp, then cursorLeft
165+ // eraseLine = \x1b[2K (erase entire line)
166+ // cursorUp = \x1b[1A
167+ // cursorLeft = \x1b[G (move to column 1)
168+ function eraseLines(count: number): string {
169+ if (count <= 0) return "";
170+ let seq = "";
171+ for (let i = 0; i < count; i++) {
172+ seq += "\x1b[2K"; // erase entire current line
173+ if (i < count - 1) seq += "\x1b[1A"; // move up (skip on last)
174+ }
175+ seq += "\x1b[G"; // move cursor to column 1
176+ return seq;
138177 }
139178
140179 return {
@@ -146,17 +185,18 @@ export function createHighlighter(options?: Options): Highlighter {
146185 const lines = buffer.split("\n");
147186 buffer = lines.pop() ?? "";
148187
149- let out = redrawPartialPrefix();
188+ let out = eraseLines(previousPartialLineCount);
150189
151190 if (lines.length > 0) {
152191 out += `${lines.map((line) => parseLine(line, false)).join("\n")}\n`;
153192 }
154193
155194 if (buffer.length > 0) {
156- out += renderPartial(buffer, true);
157- hasPartial = true;
195+ const rendered = parseLine(buffer, true);
196+ out += rendered;
197+ previousPartialLineCount = countVisualLines(rendered);
158198 } else {
159- hasPartial = false;
199+ previousPartialLineCount = 0;
160200 }
161201
162202 return out;
@@ -165,10 +205,10 @@ export function createHighlighter(options?: Options): Highlighter {
165205 if (buffer.length > 0) {
166206 const out = parseLine(buffer, false);
167207 buffer = "";
168- if (hasPartial) {
169- const redrawPrefix = redrawPartialPrefix();
170- hasPartial = false;
171- return redrawPrefix + out;
208+ if (previousPartialLineCount > 0) {
209+ const prefix = eraseLines(previousPartialLineCount);
210+ previousPartialLineCount = 0;
211+ return prefix + out;
172212 }
173213 return out;
174214 }