xonecas.io
6533
Yo

yoctomarkdown / commit e6d8636

fix: use cursor-back + erase-forward to preserve inline prefix text

Replace \x1b[2K (erase entire line) + \x1b[G (column 1) with
\x1b[<n>D (cursor back n) + \x1b[0J (erase from cursor to end).

The old eraseLines approach destroyed any text that was already on the
terminal line before the highlighter output (e.g. a prompt prefix like
'AI: '), because \x1b[2K erases the whole line unconditionally.

The new erasePreviousPartial approach tracks the visible character
length of the previous partial render and moves the cursor back by
exactly that amount, then erases only from cursor forward. This
preserves pre-existing content on the same terminal line.

For wrapped partial lines, cursor-up is still used for extra visual
lines, then cursor-back for the first line's width.

xonecas2026-03-16 e6d8636204eccd53ec404b511196503b14dbd2f7 parent 90bb466

2 files changed

src/index.ts+36−31
@@ -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 },
tests/index.test.ts+17−3
@@ -193,10 +193,24 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
193193 expectStreamMatch(res, markdown);
194194 });
195195
196+ test("should preserve text before streamed output (inline use)", () => {
197+ // When a highlighter is used inline (e.g. "AI: " prefix already on
198+ // the terminal line), the erase/redraw must not destroy the prefix.
199+ const markdown = "Hello **world**";
200+ const highlighter = createHighlighter({ theme: "none" });
201+ let res = "AI: "; // simulates text already on terminal line
202+ for (const ch of markdown) {
203+ res += highlighter.write(ch);
204+ }
205+ res += highlighter.end();
206+
207+ expect(renderTerminalOutput(res)).toBe("AI: Hello **world**");
208+ });
209+
196210 test("should erase and redraw partial lines that wrap in terminal", () => {
197- // eraseLines uses \x1b[2K (erase whole line) + \x1b[1A (cursor up) +
198- // \x1b[G (cursor to col 1) — the standard log-update / ansi-escapes
199- // pattern. This is scroll-safe (all relative) unlike DECSC/DECRC.
211+ // erasePreviousPartial uses cursor-back + \x1b[0J (erase from cursor
212+ // to end of screen). This preserves any text before our output on the
213+ // same terminal line and is scroll-safe (all relative).
200214 const markdown =
201215 "No `\\x1b[2K` or `\\r` sequences in the streamed text. The ANSI colour codes remain (intentional — those are static style codes from yoctocolors, not cursor-control sequences).\n";
202216 const splitAt = 134;