Skip to content

Commit e6eb930

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(web): preserve terminal selection across output updates (#1679)
## Evidence **Before: redraws erase selection.** https://github.com/user-attachments/assets/43c8acc2-0cd6-41b3-ab2c-759f7daad20b ![Terminal after the failed drag-copy attempt](https://github.com/user-attachments/assets/c686e75b-8a22-40ac-a290-55af591798d0) **After: Shift-drag, output arrives, copy/paste works.** https://github.com/user-attachments/assets/14b847a6-3efb-48f4-b106-4d51a5cefbcb ![Selection persists after OUTPUT_ARRIVED](https://github.com/user-attachments/assets/350f0c09-4842-4109-ba50-209180cee02b) ## Problem T3 clears Ghostty selection on every appended output update, so tmux redraws erase it before copying. tmux also captures ordinary drag; native Shift-drag already bypasses mouse reporting. ## Change Preserve selection on appended output; clear it on buffer rebuild. Five lines added, three removed; no new controls or keyboard handlers. ## Scope and approval The fork maintainer requested the smallest fix with zmux shortcuts preserved and approved the final patch. This repairs existing selection behavior. ## Verification ### Checks - **Head:** `da30220` - **Fork CI:** `vp run fork:ci`, exit 0 - **Types:** server/web/contracts, exit 0 - **Builds:** server/web, exit 0 - **Web:** six suites, 50 tests passed - **Aftercare:** no further copy changes Regressions cover selection surviving output and clearing on rebuild. React Doctor exited 0 with existing module-export warnings. ### Chrome - **Passed:** plain drag and zmux Shift-drag - **Passed:** Ctrl+Shift+C/V copy/paste - **Passed:** native tab/dashboard/detach keys - **Passed:** user's Ctrl+D T3 split binding - **Untested:** Electron, mobile, relay/tunnel ### Recordings - **After build:** `da30220`, trusted CDP input - **After clip:** 11.77s, terminal crop - **Cut:** raw 7–13s, after output arrived - **Trim:** raw 17.7–17.8s - **Wait:** full six-second output wait retained - **Before:** historical 12.17s clip Both clips reviewed by frames and full playback. ### Agent - **Model:** GPT-6.1-Sol - **Harness:** Codex through T3 Code ## Fork trailers Fork-Domain: zmux-estate Fork-Tier: bugfix Fork-Upstreamable: yes Co-authored-by: donjor <[email protected]>
1 parent 7c996b4 commit e6eb930

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,12 @@ function writeSystemMessage(
232232
}
233233

234234
export function writeTerminalOutputUpdate(
235-
terminal: Pick<GhosttyTerminalSurface, "resetAndWrite" | "write">,
235+
terminal: Pick<GhosttyTerminalSurface, "resetAndWrite" | "write" | "clearSelection">,
236236
update: TerminalOutputUpdate,
237237
): void {
238238
if (update.type === "reset") {
239239
terminal.resetAndWrite(update.data);
240+
terminal.clearSelection(); // fork-hook: zmux-estate/terminal-selection-reset
240241
} else if (update.type === "append") {
241242
terminal.write(update.data);
242243
}
@@ -250,7 +251,6 @@ export function synchronizeTerminalOutput(
250251
if (session.version === 0) return cursor;
251252
const update = readTerminalOutputUpdate(session.output, cursor);
252253
writeTerminalOutputUpdate(terminal, update);
253-
terminal.clearSelection();
254254
return update.cursor;
255255
}
256256

@@ -1082,6 +1082,7 @@ function TerminalSessionViewport({
10821082
if (!hasTerminalWriteAccess()) return;
10831083
const activeTerminal = terminalRef.current;
10841084
if (!activeTerminal) return;
1085+
activeTerminal.clearSelection(); // fork-hook: zmux-estate/terminal-selection-input
10851086
const result = await writeTerminal(data);
10861087
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
10871088
const error = squashAtomCommandFailure(result);
@@ -1174,6 +1175,7 @@ function TerminalSessionViewport({
11741175

11751176
function handleData(data: string): void {
11761177
if (!hasTerminalWriteAccess()) return;
1178+
terminal.clearSelection(); // fork-hook: zmux-estate/terminal-selection-input
11771179
void (async () => {
11781180
const result = await writeTerminal(data);
11791181
if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
import {
3+
INITIAL_TERMINAL_OUTPUT_CURSOR,
4+
applyTerminalAttachStreamEvent,
5+
nextTerminalAttachSeedState,
6+
} from "@t3tools/client-runtime/state/terminal";
7+
8+
import { synchronizeTerminalOutput } from "../../components/ThreadTerminalDrawer";
9+
import { GhosttyTerminalCore } from "./core";
10+
11+
vi.mock("./vendor/ghostty-vt.wasm?url", async () => ({
12+
default: (await import("./vendor/ghostty-vt.wasm?inline")).default,
13+
}));
14+
vi.mock("./vendor/ghostty-write-pty.wasm?url&no-inline", async () => ({
15+
default: (await import("./vendor/ghostty-write-pty.wasm?inline")).default,
16+
}));
17+
18+
describe("terminal output selection", () => {
19+
it("preserves selection through tmux redraws and clears it when output is replaced", async () => {
20+
const core = await GhosttyTerminalCore.create(
21+
12,
22+
3,
23+
8,
24+
16,
25+
{
26+
foreground: { r: 255, g: 255, b: 255 },
27+
background: { r: 0, g: 0, b: 0 },
28+
cursor: { r: 255, g: 255, b: 255 },
29+
},
30+
() => {},
31+
);
32+
try {
33+
const identity = { threadId: "selection-test", terminalId: "term-1" };
34+
let state = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
35+
type: "output",
36+
...identity,
37+
data: "copy this\x1b[?1002h\x1b[?1006h",
38+
});
39+
const cursor = synchronizeTerminalOutput(core, state, INITIAL_TERMINAL_OUTPUT_CURSOR);
40+
core.setSelection({ x: 0, y: 0 }, { x: 8, y: 0 });
41+
expect(core.selectionText()).toBe("copy this");
42+
43+
state = applyTerminalAttachStreamEvent(state, {
44+
type: "output",
45+
...identity,
46+
data: "\x1b7\x1b[3;1Hstatus\x1b8",
47+
});
48+
const next = synchronizeTerminalOutput(core, state, cursor);
49+
expect(core.selectionText()).toBe("copy this");
50+
51+
state = applyTerminalAttachStreamEvent(state, { type: "cleared", ...identity });
52+
synchronizeTerminalOutput(core, state, next);
53+
expect(core.selectionText()).toBe("");
54+
} finally {
55+
core.dispose();
56+
}
57+
});
58+
});

0 commit comments

Comments
 (0)