Repository navigation
fix: honor erased lines in notebook output #45184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
67a4e0e
4285276
fa6ca37
10b7181
136c682
73e28df
2f48920
e3c9284
9363f47
dcf49e8
143751b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { expect, it } from "vite-plus/test"; | ||
|
|
||
| import { notebookStreamText } from "./notebookOutput.ts"; | ||
|
|
||
| it.each([ | ||
| ["Processing…\r\u001b[2KDone", "Done"], | ||
| ["Status: waiting\r\u001b[9G\u001b[Kdone", "Status: done"], | ||
| ["Status: waiting\u001b[7D\u001b[Kdone", "Status: done"], | ||
| ["Status: waiting\u001b[1;9f\u001b[Kdone", "Status: done"], | ||
| ["\u009b31mStatus: waiting\r\u009b9G\u009bKdone", "Status: done"], | ||
| ["HELLO\u001b[s\u001b[1G\u001b[KX\u001b[u!", "X !"], | ||
| ["def\u001b[F\u001b[Kdone", "done"], | ||
| ["A\u001b[31ḿ\u001b[K", "Á"], | ||
| ["界界界\rABC\rX\u001b[KDone", "XDone"], | ||
| ["界界界\rABC\rX\u001b[1K!", " !C 界"], | ||
| ["keep\u001b]0;\u009b2K\u0007end", "keepend"], | ||
| ["keep\u001b]0;\u001b[2K\u0007end", " end"], | ||
| ["界".repeat(8_000) + "\rX".repeat(5_000) + "\r\u001b[2KDone", "Done"], | ||
| ["á".repeat(110_000) + "\u001b[K".repeat(110_000) + "\r\u001b[2KDone", "Done"], | ||
| [ | ||
| "a".repeat(1_000_000) + "\u001b[1K".repeat(1_000_000) + "\r" + "a".repeat(1_000_000), | ||
| "a".repeat(1_000_000), | ||
| ], | ||
| ["a" + "\u001b[999999C\u001b[2Kb\u001b[1G".repeat(100_000), "a" + "b".repeat(100_000)], | ||
| ])( | ||
| "applies horizontal cursor movement and line erases in a notebook stream (%#)", | ||
| (input, expected) => { | ||
| expect(notebookStreamText(input)).toBe(expected); | ||
| }, | ||
| ); | ||
|
|
||
| it("bounds cursor padding across many short notebook lines", () => { | ||
| const input = "\u001b[999999999C\u001b[Kb\n\u009b999999999G\u009bKc\n".repeat(200); | ||
| const output = notebookStreamText(input); | ||
| expect(output.length).toBeLessThanOrEqual(input.length + 400); | ||
| expect( | ||
| output | ||
| .trim() | ||
| .split("\n") | ||
| .map((line) => line.trimStart()), | ||
| ).toEqual(Array.from({ length: 200 }, () => ["b", "c"]).flat()); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1117,3 +1117,196 @@ export function terminalOutputCollapseRedraws(text: string): string { | |
| ? text.replaceAll("\r\n", "\n").split("\n").map(lastFrame).join("\n") | ||
| : text; | ||
| } | ||
|
|
||
| const SAVED_CSI = /(?:\u001b\[|\u009b)([0-?]*)([ -/]*)([@-~])/uy; | ||
| const SAVED_STRING_END = /\u001b\\?|\u009c|[\u0018\u001a]/gu; | ||
| const SAVED_OSC_END = /\u001b\\?|\u009c|[\u0007\u0018\u001a]/gu; | ||
|
|
||
| /** Replays saved horizontal edits in original columns, without a terminal scrollback limit. | ||
| * Undefined when erases would revisit too many columns, so hostile input stays linear. */ | ||
| export function terminalOutputReplayLineEdits(line: string): string | undefined { | ||
| // Zero is blank, one continues a wide glyph, and other values encode code points. | ||
| let columns = new Uint32Array(0); | ||
| const prefixes = new Map<number, string>(); | ||
| const suffixes = new Map<number, string>(); | ||
| let base: number | undefined; | ||
| let wrotePrefix = false; | ||
| let length = 0; | ||
| let col = 0; | ||
| let savedCol: number | undefined; | ||
| let plainParts: string[] | undefined = []; | ||
| let plainStart = 0; | ||
| let eraseBudget = 4 * line.length; | ||
|
|
||
| const skipControl = (start: number, end: number) => { | ||
| if (start > plainStart) plainParts?.push(line.slice(plainStart, start)); | ||
| plainStart = end; | ||
| }; | ||
|
|
||
| const moveTo = (next: number) => { | ||
| base = undefined; | ||
| wrotePrefix = false; | ||
| col = next; | ||
| }; | ||
|
|
||
| const clearBoundary = (at: number) => { | ||
| if (columns[at] !== 1) return; | ||
| suffixes.delete(at - 1); | ||
| columns[at - 1] = 0; | ||
| columns[at] = 0; | ||
| }; | ||
|
|
||
| const erase = (mode: number): boolean => { | ||
| plainParts = undefined; | ||
| base = undefined; | ||
| wrotePrefix = false; | ||
| if (mode === 2) { | ||
| eraseBudget -= length; | ||
| if (eraseBudget < 0) return false; | ||
| columns.fill(0, 0, length); | ||
| length = 0; | ||
| prefixes.clear(); | ||
| suffixes.clear(); | ||
| return true; | ||
| } | ||
| const boundary = Math.min(length, mode === 0 ? col : col + 1); | ||
| const start = mode === 0 ? boundary : 0; | ||
| const end = mode === 0 ? length : boundary; | ||
| if (mode === 0) prefixes.delete(end); | ||
| if (start === end) return true; | ||
| eraseBudget -= end - start; | ||
| if (eraseBudget < 0) return false; | ||
|
|
||
| clearBoundary(boundary); | ||
| columns.fill(0, start, end); | ||
| if (prefixes.size > 0 || suffixes.size > 0) { | ||
| for (let at = start; at < end; at += 1) { | ||
| prefixes.delete(at); | ||
| suffixes.delete(at); | ||
| } | ||
| } | ||
| if (mode === 0) length = boundary; | ||
| return true; | ||
| }; | ||
|
|
||
| for (let index = 0; index < line.length;) { | ||
| const point = line.codePointAt(index)!; | ||
| if (point === 0x1b || (point >= 0x80 && point <= 0x9f)) { | ||
| const start = index; | ||
| SAVED_CSI.lastIndex = index; | ||
| const csi = SAVED_CSI.exec(line); | ||
| if (csi) { | ||
| index = SAVED_CSI.lastIndex; | ||
| skipControl(start, index); | ||
| const [, params, intermediates, command] = csi; | ||
| if (intermediates || !/^[\d;]*$/u.test(params!)) continue; | ||
| const [first, second] = params!.split(";", 2).map(Number); | ||
| const distance = first || 1; | ||
| if (command === "K" && (first === 0 || first === 1 || first === 2)) { | ||
| if (!erase(first)) return undefined; | ||
| } else if (command === "s") savedCol = col; | ||
| else if (command === "u" && savedCol !== undefined) moveTo(savedCol); | ||
| else if (command === "E" || command === "F") moveTo(0); | ||
| else if ("CDGHf".includes(command!)) { | ||
| const next = | ||
| command === "C" | ||
| ? col + distance | ||
| : command === "D" | ||
| ? col - distance | ||
| : command === "G" | ||
| ? distance - 1 | ||
| : (second || 1) - 1; | ||
| // Keep existing columns addressable without expanding short lines far beyond their input. | ||
| moveTo(Math.max(0, Math.min(Math.max(length, line.length), next))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the logical cursor when bounding cursor-forward padding.
Cursor-forward and cursor-backward commands move relative to the current column. A forward move followed by an equal backward move must return to the original column. Bound only the materialized padding, and keep the logical column unchanged. Then the cursor-forward target can exceed the input length, and the padding cost stays bounded.
|
||
| } | ||
| continue; | ||
| } | ||
| const esc = point === 0x1b; | ||
| const introducer = line[index + 1] ?? ""; | ||
| const osc = point === 0x9d || (esc && introducer === "]"); | ||
| if ( | ||
| osc || | ||
| point === 0x90 || | ||
| point === 0x98 || | ||
| point === 0x9e || | ||
| point === 0x9f || | ||
| (esc && STRING_SEQUENCE_INTRODUCERS.has(introducer)) | ||
| ) { | ||
| const end = osc ? SAVED_OSC_END : SAVED_STRING_END; | ||
| end.lastIndex = index + (esc ? 2 : 1); | ||
| const terminator = end.exec(line); | ||
| // A bare ESC interrupts the string and begins the next command. | ||
| index = | ||
| terminator?.[0] === ESC ? terminator.index : terminator ? end.lastIndex : line.length; | ||
| } else if (esc) { | ||
| if (introducer === "7") savedCol = col; | ||
| else if (introducer === "8" && savedCol !== undefined) moveTo(savedCol); | ||
| const size = escapeLength(line, index); | ||
| index = size === -1 ? line.length : index + size; | ||
| } else index += 1; | ||
| skipControl(start, index); | ||
| continue; | ||
| } | ||
| index += point > 0xffff ? 2 : 1; | ||
| if (point < 0x20 && point !== 0x09) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle TAB as cursor movement before replaying erases. TAB bypasses this control branch and enters the printable-character branch. The declared width library returns character widths, not tab-stop positions. (raw.githubusercontent.com) With eight-column tab stops, Advance to the next tab stop without storing TAB as a printable cell. Keep any resulting padding bounded.
|
||
| base = undefined; | ||
| wrotePrefix = false; | ||
| if (point === 0x0d) col = 0; | ||
| else if (point === 0x08) col = Math.max(0, col - 1); | ||
| continue; | ||
| } | ||
| if (point === 0x7f) continue; | ||
| const character = String.fromCodePoint(point); | ||
| const width = characterWidth(character); | ||
| if (width === 0) { | ||
| if (base === undefined) { | ||
| prefixes.set(col, (prefixes.get(col) ?? "") + character); | ||
| wrotePrefix = true; | ||
| length = Math.max(length, col); | ||
| } else suffixes.set(base, (suffixes.get(base) ?? "") + character); | ||
| continue; | ||
| } | ||
| const end = col + width; | ||
| if (end > columns.length) { | ||
| const grown = new Uint32Array(Math.max(end, columns.length * 2, 32)); | ||
| grown.set(columns); | ||
| columns = grown; | ||
| } | ||
| clearBoundary(col); | ||
| if (columns[end] === 1) columns[end] = 0; | ||
| if (!wrotePrefix) prefixes.delete(col); | ||
| suffixes.delete(col); | ||
| columns[col] = point + 2; | ||
| if (width === 2) { | ||
| columns[col + 1] = 1; | ||
| prefixes.delete(col + 1); | ||
| suffixes.delete(col + 1); | ||
| } | ||
| base = col; | ||
| wrotePrefix = false; | ||
| col = end; | ||
| length = Math.max(length, end); | ||
| } | ||
|
|
||
| // An apparent erase inside a control string does not activate horizontal replay. | ||
| if (plainParts) { | ||
| plainParts.push(line.slice(plainStart)); | ||
| return plainParts.join(""); | ||
| } | ||
|
|
||
| const parts: string[] = []; | ||
| let text = ""; | ||
| for (let at = 0; at < length; at += 1) { | ||
| const cell = columns[at] ?? 0; | ||
| text += | ||
| (prefixes.get(at) ?? "") + | ||
| (cell === 0 ? " " : cell === 1 ? "" : String.fromCodePoint(cell - 2)) + | ||
| (suffixes.get(at) ?? ""); | ||
| if (text.length >= 4096) { | ||
| parts.push(text); | ||
| text = ""; | ||
| } | ||
| } | ||
| parts.push(text, prefixes.get(length) ?? ""); | ||
| return parts.join(""); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skip erase work on cells that earlier erases already cleared.
For
"x".repeat(n) + "\u001b[1K".repeat(n), eachESC[1Kclears the range from column 0 to the cursor. It leaves the cursor and the logical line length unchanged. Each later erase clears the same blank range again. Replay therefore performsn × ncell writes. Atn = 500_000, that is 250 billion writes for 2.5 million input characters.The web notebook preview can supply this input. It caps each stream record at 256,000 characters, but it joins consecutive stream records into one line before replay. The 8 MiB file limit still admits this input. The server extraction path also passes joined stream text to the same replay function.
Replay should not repeat work on cells that earlier erases already cleared. Track the occupied ranges separately from the logical line length, so repeated left erases skip ranges that are already blank. Cursor positions, rendered text, and output spacing must remain unchanged.