GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(terminal): keep line breaks when joining a running terminal (#42927) · patroza/t3code@867a9f0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/contracts/src
    • terminal.test.ts
    • terminal.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/contracts/src/terminal.test.ts‎

Copy file name to clipboardExpand all lines: packages/contracts/src/terminal.test.ts
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,41 @@ import { describe, expect, it } from "vite-plus/test";
44
import {
55
chunkTerminalWrite,
66
DEFAULT_TERMINAL_ID,
7+
retainTerminalSnapshotHistory,
8+
skipTerminalSnapshotContinuation,
79
TERMINAL_WRITE_MAX_LENGTH,
810
TerminalError,
911
TerminalOpenInput,
1012
TerminalProviderEnvironmentError,
13+
terminalSequenceStateAfter,
1114
} from "./terminal.ts";
1215
import { ProviderInstanceId } from "./providerInstance.ts";
1316

1417
const encodeTerminalError = Schema.encodeUnknownSync(TerminalError);
1518
const decodeTerminalError = Schema.decodeUnknownSync(TerminalError);
1619

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+
1742
function decodeSync<S extends Schema.Top>(schema: S, input: unknown): Schema.Schema.Type<S> {
1843
return Schema.decodeUnknownSync(schema as never)(input) as Schema.Schema.Type<S>;
1944
}
Collapse file

‎packages/contracts/src/terminal.ts‎

Copy file name to clipboardExpand all lines: packages/contracts/src/terminal.ts
+45-10Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const SEQUENCE_SOS_PM_APC = 5;
1919
const SEQUENCE_OSC = 6;
2020

2121
// 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 {
2327
if (unit === 0x1b) return SEQUENCE_ESCAPE;
2428
if (state === TERMINAL_SEQUENCE_GROUND || unit === 0x18 || unit === 0x1a) {
2529
return TERMINAL_SEQUENCE_GROUND;
@@ -62,7 +66,10 @@ function nextTerminalSequenceState(state: number, unit: number): number {
6266
if (byte === 0x90) mode = SEQUENCE_DCS;
6367
else if (byte === 0x9b) mode = SEQUENCE_CSI;
6468
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+
}
6673
}
6774
return mode;
6875
}
@@ -97,6 +104,25 @@ export function terminalSequenceEnd(state: number, text: string): number {
97104
return index;
98105
}
99106

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+
100126
/** Skip the continuation of an omitted snapshot prefix, retaining any new escape sequence. */
101127
export function skipTerminalSnapshotContinuation(state: number, data: string) {
102128
if (data.length === 0) return { state, data };
@@ -108,17 +134,20 @@ export function skipTerminalSnapshotContinuation(state: number, data: string) {
108134
}
109135

110136
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) };
116145
}
117146
// 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: "" };
119148
return {
120149
state: TERMINAL_SEQUENCE_GROUND,
121-
data: data.slice(data[escape + 1] === "\\" ? escape + 2 : escape),
150+
data: data.slice(data[end + 1] === "\\" ? end + 2 : end),
122151
};
123152
}
124153

@@ -152,7 +181,13 @@ export function retainedTerminalHistory(history: string, maxBytes: number): stri
152181
0,
153182
history.length - retained.length,
154183
);
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;
156191
}
157192

158193
/** Keep a whole unfinished prefix when possible, or skip its continuation during replay. */

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.