@@ -83,17 +83,17 @@ export function createHighlighter(options?: Options): Highlighter {
8383 return result;
8484 }
8585
86- function parseLine(line: string, _isLastLine: boolean): string {
86+ function parseLine(line: string, isPartial: boolean): string {
8787 if (inCodeBlock) {
8888 if (line.trim().startsWith("```")) {
89- inCodeBlock = false;
89+ if (!isPartial) inCodeBlock = false;
9090 return theme.codeBlock(line);
9191 }
9292 return theme.codeBlock(line);
9393 }
9494
9595 if (line.trim().startsWith("```")) {
96- inCodeBlock = true;
96+ if (!isPartial) inCodeBlock = true;
9797 return theme.codeBlock(line);
9898 }
9999
@@ -127,6 +127,8 @@ export function createHighlighter(options?: Options): Highlighter {
127127 return parseInline(line);
128128 }
129129
130+ let hasPartial = false;
131+
130132 return {
131133 write(chunk: string | Uint8Array): string {
132134 const text =
@@ -136,16 +138,29 @@ export function createHighlighter(options?: Options): Highlighter {
136138 const lines = buffer.split("\n");
137139 buffer = lines.pop() ?? "";
138140
139- return (
140- lines.map((line) => parseLine(line, false)).join("\n") +
141- (lines.length > 0 ? "\n" : "")
142- );
141+ let out = "";
142+ if (hasPartial) {
143+ out += "\x1b[2K\r";
144+ }
145+
146+ if (lines.length > 0) {
147+ out += `${lines.map((line) => parseLine(line, false)).join("\n")}\n`;
148+ }
149+
150+ if (buffer.length > 0) {
151+ out += parseLine(buffer, true);
152+ hasPartial = true;
153+ } else {
154+ hasPartial = false;
155+ }
156+
157+ return out;
143158 },
144159 end(): string {
145160 if (buffer.length > 0) {
146- const out = parseLine(buffer, true);
161+ const out = parseLine(buffer, false);
147162 buffer = "";
148- return out;
163+ return (hasPartial ? "\x1b[2K\r" : "") + out;
149164 }
150165 return "";
151166 },