@@ -142,7 +142,8 @@ export function createHighlighter(options?: Options): Highlighter {
142142 return parseInline(line);
143143 }
144144
145- let previousPartialLineCount = 0;
145+ let previousPartialVisibleLen = 0;
146+ let previousPartialWrappedLines = 0;
146147
147148 // Strip ANSI escapes to measure visible character width
148149 function visibleLength(s: string): number {
@@ -150,29 +151,31 @@ export function createHighlighter(options?: Options): Highlighter {
150151 return s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, "").length;
151152 }
152153
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 "";
154+ // Erase the previous partial render by moving back over exactly the
155+ // characters we wrote and clearing from cursor to end of screen.
156+ // This preserves any text before our output on the same terminal line
157+ // (e.g. a prompt prefix), unlike \x1b[2K which erases the whole line.
158+ function erasePreviousPartial(): string {
159+ if (previousPartialVisibleLen <= 0 && previousPartialWrappedLines <= 0) {
160+ return "";
161+ }
170162 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)
163+ // Move cursor up for any extra wrapped lines beyond the first
164+ if (previousPartialWrappedLines > 1) {
165+ seq += `\x1b[${previousPartialWrappedLines - 1}A`;
166+ }
167+ // Move cursor back to the start of our partial output.
168+ // On the first visual line, the column offset is where our text began.
169+ // We compute how many columns are on the first line of our partial.
170+ const firstLineLen =
171+ previousPartialWrappedLines > 1
172+ ? terminalColumns
173+ : previousPartialVisibleLen;
174+ if (firstLineLen > 0) {
175+ seq += `\x1b[${firstLineLen}D`;
174176 }
175- seq += "\x1b[G"; // move cursor to column 1
177+ // Erase from cursor to end of screen
178+ seq += "\x1b[0J";
176179 return seq;
177180 }
178181
@@ -185,7 +188,7 @@ export function createHighlighter(options?: Options): Highlighter {
185188 const lines = buffer.split("\n");
186189 buffer = lines.pop() ?? "";
187190
188- let out = eraseLines(previousPartialLineCount);
191+ let out = erasePreviousPartial();
189192
190193 if (lines.length > 0) {
191194 out += `${lines.map((line) => parseLine(line, false)).join("\n")}\n`;
@@ -194,9 +197,13 @@ export function createHighlighter(options?: Options): Highlighter {
194197 if (buffer.length > 0) {
195198 const rendered = parseLine(buffer, true);
196199 out += rendered;
197- previousPartialLineCount = countVisualLines(rendered);
200+ const vLen = visibleLength(rendered);
201+ previousPartialVisibleLen = vLen;
202+ previousPartialWrappedLines =
203+ vLen === 0 ? 0 : Math.ceil(vLen / terminalColumns);
198204 } else {
199- previousPartialLineCount = 0;
205+ previousPartialVisibleLen = 0;
206+ previousPartialWrappedLines = 0;
200207 }
201208
202209 return out;
@@ -205,12 +212,10 @@ export function createHighlighter(options?: Options): Highlighter {
205212 if (buffer.length > 0) {
206213 const out = parseLine(buffer, false);
207214 buffer = "";
208- if (previousPartialLineCount > 0) {
209- const prefix = eraseLines(previousPartialLineCount);
210- previousPartialLineCount = 0;
211- return prefix + out;
212- }
213- return out;
215+ const prefix = erasePreviousPartial();
216+ previousPartialVisibleLen = 0;
217+ previousPartialWrappedLines = 0;
218+ return prefix + out;
214219 }
215220 return "";
216221 },