@@ -128,6 +128,7 @@ export function createHighlighter(options?: Options): Highlighter {
128128 }
129129
130130 let hasPartial = false;
131+ let partialLength = 0;
131132
132133 return {
133134 write(chunk: string | Uint8Array): string {
@@ -140,7 +141,15 @@ export function createHighlighter(options?: Options): Highlighter {
140141
141142 let out = "";
142143 if (hasPartial) {
144+ const columns =
145+ typeof process !== "undefined" && process.stdout?.columns
146+ ? process.stdout.columns
147+ : 80;
148+ const linesToClear = Math.ceil(partialLength / columns) || 1;
143149 out += "\x1b[2K\r";
150+ if (linesToClear > 1) {
151+ out += "\x1b[1A\x1b[2K".repeat(linesToClear - 1);
152+ }
144153 }
145154
146155 if (lines.length > 0) {
@@ -150,8 +159,10 @@ export function createHighlighter(options?: Options): Highlighter {
150159 if (buffer.length > 0) {
151160 out += parseLine(buffer, true);
152161 hasPartial = true;
162+ partialLength = buffer.length;
153163 } else {
154164 hasPartial = false;
165+ partialLength = 0;
155166 }
156167
157168 return out;
@@ -160,7 +171,19 @@ export function createHighlighter(options?: Options): Highlighter {
160171 if (buffer.length > 0) {
161172 const out = parseLine(buffer, false);
162173 buffer = "";
163- return (hasPartial ? "\x1b[2K\r" : "") + out;
174+ if (hasPartial) {
175+ const columns =
176+ typeof process !== "undefined" && process.stdout?.columns
177+ ? process.stdout.columns
178+ : 80;
179+ const linesToClear = Math.ceil(partialLength / columns) || 1;
180+ let clearSeq = "\x1b[2K\r";
181+ if (linesToClear > 1) {
182+ clearSeq += "\x1b[1A\x1b[2K".repeat(linesToClear - 1);
183+ }
184+ return clearSeq + out;
185+ }
186+ return out;
164187 }
165188 return "";
166189 },