GeezerVison is showing github.com — fix(terminal): keep line breaks when joining a running terminal (#42927) · patroza/t3code@867a9f0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/contracts/src/terminal.test.ts +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,16 +4,41 @@ import { describe, expect, it } from "vite-plus/test"; | 4 | 4 | import { | 5 | 5 | chunkTerminalWrite, | 6 | 6 | DEFAULT_TERMINAL_ID, | | 7 | + retainTerminalSnapshotHistory, | | 8 | + skipTerminalSnapshotContinuation, | 7 | 9 | TERMINAL_WRITE_MAX_LENGTH, | 8 | 10 | TerminalError, | 9 | 11 | TerminalOpenInput, | 10 | 12 | TerminalProviderEnvironmentError, | | 13 | + terminalSequenceStateAfter, | 11 | 14 | } from "./terminal.ts"; | 12 | 15 | import { ProviderInstanceId } from "./providerInstance.ts"; | 13 | 16 |
| 14 | 17 | const encodeTerminalError = Schema.encodeUnknownSync(TerminalError); | 15 | 18 | const decodeTerminalError = Schema.decodeUnknownSync(TerminalError); | 16 | 19 |
| | 20 | +describe("terminal snapshot replay after an omitted DCS", () => { | | 21 | + const pendingDcs = "\u001bPz"; | | 22 | + const state = terminalSequenceStateAfter(0, pendingDcs); | | 23 | + | | 24 | + it.each([ | | 25 | + ["a next-line control", "\u0085ready", "\u001bEready"], | | 26 | + ["a tab stop before carriage return and tab", "\u0088\r\tready", "\u001bH\r\tready"], | | 27 | + ["a byte after a string terminator", "\u1700ready", "\ufffdready"], | | 28 | + ])("preserves %s in a continuation", (_name, data, replay) => { | | 29 | + expect(skipTerminalSnapshotContinuation(state, data)).toEqual({ state: 0, data: replay }); | | 30 | + }); | | 31 | + | | 32 | + it.each([ | | 33 | + [8, "\u0085ready", "\u001bEready"], | | 34 | + [8, "\u2140ready", "\u001bEready"], | | 35 | + [10, "\u2140ready", "\u001bE\ufffdready"], | | 36 | + ])("keeps the line break within a %i-byte history budget", (budget, rest, replay) => { | | 37 | + const history = pendingDcs + "z".repeat(50) + rest; | | 38 | + expect(retainTerminalSnapshotHistory(history, budget)).toEqual({ history: replay }); | | 39 | + }); | | 40 | +}); | | 41 | + | 17 | 42 | function decodeSync<S extends Schema.Top>(schema: S, input: unknown): Schema.Schema.Type<S> { | 18 | 43 | return Schema.decodeUnknownSync(schema as never)(input) as Schema.Schema.Type<S>; | 19 | 44 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/contracts/src/terminal.ts +45-10Lines changed: 45 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,11 @@ const SEQUENCE_SOS_PM_APC = 5; | 19 | 19 | const SEQUENCE_OSC = 6; | 20 | 20 |
| 21 | 21 | // Bits above the low three hold a high surrogate until its other half arrives. | 22 | | -function nextTerminalSequenceState(state: number, unit: number): number { | | 22 | +function nextTerminalSequenceState( | | 23 | + state: number, | | 24 | + unit: number, | | 25 | + onC1ReturnToGround?: (byte: number, remainingBytes: number) => void, | | 26 | +): number { | 23 | 27 | if (unit === 0x1b) return SEQUENCE_ESCAPE; | 24 | 28 | if (state === TERMINAL_SEQUENCE_GROUND || unit === 0x18 || unit === 0x1a) { | 25 | 29 | return TERMINAL_SEQUENCE_GROUND; | @@ -62,7 +66,10 @@ function nextTerminalSequenceState(state: number, unit: number): number { | 62 | 66 | if (byte === 0x90) mode = SEQUENCE_DCS; | 63 | 67 | else if (byte === 0x9b) mode = SEQUENCE_CSI; | 64 | 68 | else if (byte === 0x98 || byte >= 0x9e) mode = SEQUENCE_SOS_PM_APC; | 65 | | - else return TERMINAL_SEQUENCE_GROUND; | | 69 | + else { | | 70 | + onC1ReturnToGround?.(byte, shift / 6); | | 71 | + return TERMINAL_SEQUENCE_GROUND; | | 72 | + } | 66 | 73 | } | 67 | 74 | return mode; | 68 | 75 | } | @@ -97,6 +104,25 @@ export function terminalSequenceEnd(state: number, text: string): number { | 97 | 104 | return index; | 98 | 105 | } | 99 | 106 |
| | 107 | +function scanSnapshotContinuation(state: number, data: string, limit = data.length) { | | 108 | + let control = ""; | | 109 | + let replacementCount = 0; | | 110 | + const replayC1 = (byte: number, remainingBytes: number) => { | | 111 | + // Replay IND, NEL, HTS and RI from ground. Any bytes parsed after the control | | 112 | + // are now stray UTF-8 continuation bytes. | | 113 | + if (byte === 0x84 || byte === 0x85 || byte === 0x88 || byte === 0x8d) { | | 114 | + control = "\u001b" + String.fromCharCode(byte - 0x40); | | 115 | + } | | 116 | + replacementCount = remainingBytes; | | 117 | + }; | | 118 | + let end = 0; | | 119 | + while (state !== TERMINAL_SEQUENCE_GROUND && end < limit) { | | 120 | + state = nextTerminalSequenceState(state, data.charCodeAt(end), replayC1); | | 121 | + end += 1; | | 122 | + } | | 123 | + return { state, end, control, replacementCount }; | | 124 | +} | | 125 | + | 100 | 126 | /** Skip the continuation of an omitted snapshot prefix, retaining any new escape sequence. */ | 101 | 127 | export function skipTerminalSnapshotContinuation(state: number, data: string) { | 102 | 128 | if (data.length === 0) return { state, data }; | @@ -108,17 +134,20 @@ export function skipTerminalSnapshotContinuation(state: number, data: string) { | 108 | 134 | } | 109 | 135 |
| 110 | 136 | const escape = data.indexOf("\u001b"); | 111 | | - const prefix = escape < 0 ? data : data.slice(0, escape); | 112 | | - const end = terminalSequenceEnd(state, prefix); | 113 | | - const next = terminalSequenceStateAfter(state, prefix, 0, end); | 114 | | - if (next === TERMINAL_SEQUENCE_GROUND || escape < 0) { | 115 | | - return { state: next, data: data.slice(end) }; | | 137 | + const { | | 138 | + state: next, | | 139 | + end, | | 140 | + control, | | 141 | + replacementCount, | | 142 | + } = scanSnapshotContinuation(state, data, escape < 0 ? data.length : escape); | | 143 | + if (next === TERMINAL_SEQUENCE_GROUND || end === data.length) { | | 144 | + return { state: next, data: control + "\ufffd".repeat(replacementCount) + data.slice(end) }; | 116 | 145 | } | 117 | 146 | // Hold a final ESC until we know whether it starts a new sequence or completes ST. | 118 | | - if (escape + 1 === data.length) return { state: SEQUENCE_ESCAPE, data: "" }; | | 147 | + if (end + 1 === data.length) return { state: SEQUENCE_ESCAPE, data: "" }; | 119 | 148 | return { | 120 | 149 | state: TERMINAL_SEQUENCE_GROUND, | 121 | | - data: data.slice(data[escape + 1] === "\\" ? escape + 2 : escape), | | 150 | + data: data.slice(data[end + 1] === "\\" ? end + 2 : end), | 122 | 151 | }; | 123 | 152 | } | 124 | 153 |
| @@ -152,7 +181,13 @@ export function retainedTerminalHistory(history: string, maxBytes: number): stri | 152 | 181 | 0, | 153 | 182 | history.length - retained.length, | 154 | 183 | ); | 155 | | - return retained.slice(terminalSequenceEnd(state, retained)); | | 184 | + const { end, control, replacementCount } = scanSnapshotContinuation(state, retained); | | 185 | + const tail = retained.slice(end); | | 186 | + if (replacementCount === 0) return control + tail; | | 187 | + // The two-byte ESC form fits where the consumed code point did. Each replacement | | 188 | + // glyph takes three bytes, so keep only as many as fit alongside the control and tail. | | 189 | + const available = maxBytes - historyEncoder.encode(tail).byteLength - control.length; | | 190 | + return control + "\ufffd".repeat(Math.min(replacementCount, Math.floor(available / 3))) + tail; | 156 | 191 | } | 157 | 192 |
| 158 | 193 | /** Keep a whole unfinished prefix when possible, or skip its continuation during replay. */ | |
|