@@ -142,8 +142,7 @@ export function createHighlighter(options?: Options): Highlighter {
142142 return parseInline(line);
143143 }
144144
145- let previousPartialVisibleLen = 0;
146- let previousPartialWrappedLines = 0;
145+ let previousPartialRendered = "";
147146
148147 // Strip ANSI escapes to measure visible character width
149148 function visibleLength(s: string): number {
@@ -156,21 +155,18 @@ export function createHighlighter(options?: Options): Highlighter {
156155 // This preserves any text before our output on the same terminal line
157156 // (e.g. a prompt prefix), unlike \x1b[2K which erases the whole line.
158157 function erasePreviousPartial(): string {
159- if (previousPartialVisibleLen <= 0 && previousPartialWrappedLines <= 0) {
158+ if (previousPartialRendered.length === 0) {
160159 return "";
161160 }
161+ const vLen = visibleLength(previousPartialRendered);
162+ const wrappedLines = vLen === 0 ? 0 : Math.ceil(vLen / terminalColumns);
162163 let seq = "";
163164 // Move cursor up for any extra wrapped lines beyond the first
164- if (previousPartialWrappedLines > 1) {
165- seq += `\x1b[${previousPartialWrappedLines - 1}A`;
165+ if (wrappedLines > 1) {
166+ seq += `\x1b[${wrappedLines - 1}A`;
166167 }
167168 // 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;
169+ const firstLineLen = wrappedLines > 1 ? terminalColumns : vLen;
174170 if (firstLineLen > 0) {
175171 seq += `\x1b[${firstLineLen}D`;
176172 }
@@ -188,34 +184,56 @@ export function createHighlighter(options?: Options): Highlighter {
188184 const lines = buffer.split("\n");
189185 buffer = lines.pop() ?? "";
190186
191- let out = erasePreviousPartial();
187+ let out = "";
192188
189+ // If we have completed lines, we must erase the previous partial
190+ // (which was the in-progress version of the first completed line)
191+ // and re-render all completed lines plus the new partial.
193192 if (lines.length > 0) {
193+ out += erasePreviousPartial();
194194 out += `${lines.map((line) => parseLine(line, false)).join("\n")}\n`;
195+ previousPartialRendered = "";
195196 }
196197
197198 if (buffer.length > 0) {
198199 const rendered = parseLine(buffer, true);
199- out += rendered;
200- const vLen = visibleLength(rendered);
201- previousPartialVisibleLen = vLen;
202- previousPartialWrappedLines =
203- vLen === 0 ? 0 : Math.ceil(vLen / terminalColumns);
200+ // Append-only optimisation: if the new render starts with the
201+ // previous render, just emit the new suffix — no erase needed.
202+ // This avoids flicker and sidesteps character-width measurement
203+ // for the common case of appending plain text.
204+ if (
205+ lines.length === 0 &&
206+ previousPartialRendered.length > 0 &&
207+ rendered.startsWith(previousPartialRendered)
208+ ) {
209+ out += rendered.slice(previousPartialRendered.length);
210+ } else {
211+ // Render changed (e.g. inline formatting closed) — full redraw
212+ if (lines.length === 0) {
213+ out += erasePreviousPartial();
214+ }
215+ out += rendered;
216+ }
217+ previousPartialRendered = rendered;
204218 } else {
205- previousPartialVisibleLen = 0;
206- previousPartialWrappedLines = 0;
219+ previousPartialRendered = "";
207220 }
208221
209222 return out;
210223 },
211224 end(): string {
212225 if (buffer.length > 0) {
213- const out = parseLine(buffer, false);
226+ const rendered = parseLine(buffer, false);
214227 buffer = "";
215- const prefix = erasePreviousPartial();
216- previousPartialVisibleLen = 0;
217- previousPartialWrappedLines = 0;
218- return prefix + out;
228+ // If the final render matches the partial, nothing to redo.
229+ // If it differs (e.g. unclosed bold emitted as plain), erase and redraw.
230+ let out = "";
231+ if (rendered !== previousPartialRendered) {
232+ out += erasePreviousPartial();
233+ out += rendered;
234+ }
235+ previousPartialRendered = "";
236+ return out;
219237 }
220238 return "";
221239 },