Skip to content

Commit d5b9410

Browse files
lnieuwenhuisjakeleventhalshivamhwp
authored
feat(mobile): paste the phone clipboard into the terminal (pingdotgg#9199)
Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: shivam <[email protected]>
1 parent d487dfb commit d5b9410

8 files changed

Lines changed: 520 additions & 48 deletions

File tree

‎apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx‎

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/Stac
66
import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
77
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
88
import { Platform, Pressable, View } from "react-native";
9+
import * as Clipboard from "expo-clipboard";
10+
import * as Schema from "effect/Schema";
911
import {
1012
KeyboardController,
1113
KeyboardEvents,
@@ -27,6 +29,7 @@ import { environmentCatalog } from "../../connection/catalog";
2729
import { useEnvironmentPresentation } from "../../state/presentation";
2830
import { terminalEnvironment } from "../../state/terminal";
2931
import { useAtomCommand } from "../../state/use-atom-command";
32+
import { useServerConfigs } from "../../state/entities";
3033
import { useWorkspaceState } from "../../state/workspace";
3134
import {
3235
MAX_TERMINAL_FONT_SIZE,
@@ -65,19 +68,33 @@ import {
6568
resolveTerminalSessionLabel,
6669
type TerminalMenuSession,
6770
} from "./terminalMenu";
71+
import {
72+
hostPlatformFromOs,
73+
resolveModifiedTerminalInput,
74+
type HostPlatform,
75+
type PendingModifier,
76+
} from "./terminalInput";
77+
import { createTerminalPasteSession } from "./terminalPaste";
6878
import { cacheTerminalGridSize, getCachedTerminalGridSize } from "./terminalUiState";
6979

7080
const DEFAULT_TERMINAL_COLS = 80;
7181
const DEFAULT_TERMINAL_ROWS = 24;
7282
const TERMINAL_ACCESSORY_HEIGHT = 52;
7383
const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
7484

75-
type PendingModifier = "ctrl" | "meta";
76-
type HostPlatform = "mac" | "linux" | "windows" | "unknown";
85+
class TerminalClipboardReadError extends Schema.TaggedErrorClass<TerminalClipboardReadError>()(
86+
"TerminalClipboardReadError",
87+
{ terminalId: Schema.String, cause: Schema.Defect() },
88+
) {
89+
override get message(): string {
90+
return `Failed to read the clipboard for a paste into terminal ${this.terminalId}.`;
91+
}
92+
}
7793

7894
type TerminalToolbarAction =
7995
| { readonly kind: "send"; readonly key: string; readonly label: string; readonly data: string }
8096
| { readonly kind: "clear"; readonly key: string; readonly label: string }
97+
| { readonly kind: "paste"; readonly key: string; readonly label: string }
8198
| {
8299
readonly kind: "modifier";
83100
readonly key: string;
@@ -114,28 +131,6 @@ function inferHostPlatform(environmentLabel: string | null): HostPlatform {
114131
return "unknown";
115132
}
116133

117-
function applyCtrlModifier(input: string): string {
118-
const firstCharacter = input[0];
119-
if (!firstCharacter) {
120-
return input;
121-
}
122-
123-
const lowerCharacter = firstCharacter.toLowerCase();
124-
if (lowerCharacter >= "a" && lowerCharacter <= "z") {
125-
return String.fromCharCode(lowerCharacter.charCodeAt(0) - 96);
126-
}
127-
128-
if (firstCharacter === "@") return "\u0000";
129-
if (firstCharacter === "[") return "\u001b";
130-
if (firstCharacter === "\\") return "\u001c";
131-
if (firstCharacter === "]") return "\u001d";
132-
if (firstCharacter === "^") return "\u001e";
133-
if (firstCharacter === "_") return "\u001f";
134-
if (firstCharacter === "?") return "\u007f";
135-
136-
return input;
137-
}
138-
139134
function pickRunningTerminalSessionForBootstrap(
140135
sessions: ReadonlyArray<KnownTerminalSession>,
141136
): KnownTerminalSession | null {
@@ -462,9 +457,17 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
462457
});
463458
}, [terminal.buffer, terminal.buffer.length, terminalKey]);
464459
const cwd = terminal.summary?.cwd ?? selectedThreadProject?.workspaceRoot ?? null;
460+
const serverConfigs = useServerConfigs();
461+
const hostOs =
462+
routeEnvironmentId === null
463+
? null
464+
: (serverConfigs.get(routeEnvironmentId)?.environment.platform.os ?? null);
465+
// The descriptor is authoritative; the label is only a hint until it arrives.
465466
const hostPlatform = useMemo(
466-
() => inferHostPlatform(selectedEnvironmentConnection?.environmentLabel ?? null),
467-
[selectedEnvironmentConnection?.environmentLabel],
467+
() =>
468+
hostPlatformFromOs(hostOs) ??
469+
inferHostPlatform(selectedEnvironmentConnection?.environmentLabel ?? null),
470+
[hostOs, selectedEnvironmentConnection?.environmentLabel],
468471
);
469472

470473
const terminalTheme = getMobileTerminalTheme(themeId, appearanceScheme);
@@ -488,6 +491,7 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
488491
{ kind: "send", key: "esc", label: "esc", data: "\u001b" },
489492
...modifierActions,
490493
{ kind: "send", key: "tab", label: "tab", data: "\t" },
494+
{ kind: "paste", key: "paste", label: "paste" },
491495
{ kind: "clear", key: "clear", label: "clear" },
492496
{ kind: "send", key: "up", label: "↑", data: "\u001b[A" },
493497
{ kind: "send", key: "down", label: "↓", data: "\u001b[B" },
@@ -693,41 +697,82 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
693697
setHasMeasuredSurface(true);
694698
}, [routeEnvironmentId, routeThreadId, terminalId]);
695699

700+
/** Resolves true once the pty accepted the write, false if it was skipped or rejected. */
696701
const writeInput = useCallback(
697-
(data: string) => {
702+
async (data: string): Promise<boolean> => {
698703
if (!selectedThread || !isRunning) {
699-
return;
704+
return false;
700705
}
701706

702-
void writeTerminal({
707+
const result = await writeTerminal({
703708
environmentId: selectedThread.environmentId,
704709
input: {
705710
threadId: selectedThread.id,
706711
terminalId,
707712
data,
708713
},
709714
});
715+
return result._tag === "Success";
710716
},
711717
[isRunning, selectedThread, terminalId, writeTerminal],
712718
);
713719

720+
const pasteSessionRef = useRef<ReturnType<typeof createTerminalPasteSession> | null>(null);
721+
if (pasteSessionRef.current === null) {
722+
pasteSessionRef.current = createTerminalPasteSession();
723+
}
724+
const pasteSession = pasteSessionRef.current;
725+
726+
// Drop delayed clipboard reads whenever the route or attached pty changes.
727+
useEffect(() => {
728+
pasteSession.reset(isRunning);
729+
return () => {
730+
pasteSession.reset(false);
731+
};
732+
}, [isRunning, pasteSession, terminal.lifecycleVersion, terminalKey]);
733+
734+
const pasteFromClipboard = useCallback(async () => {
735+
await pasteSession.paste({
736+
readText: Clipboard.getStringAsync,
737+
write: writeInput,
738+
onReadError: (cause) => {
739+
console.error(new TerminalClipboardReadError({ terminalId, cause }));
740+
},
741+
});
742+
}, [pasteSession, terminalId, writeInput]);
743+
744+
/** Sends a key through the armed toolbar modifier, if any, and disarms it. */
745+
const writeModifiedInput = useCallback(
746+
(data: string) => {
747+
if (pendingModifier === null) {
748+
void writeInput(data);
749+
return;
750+
}
751+
752+
setPendingModifierState({ terminalId, value: null });
753+
const resolved = resolveModifiedTerminalInput({
754+
data,
755+
modifier: pendingModifier,
756+
hostPlatform,
757+
});
758+
if (resolved.kind === "paste") {
759+
void pasteFromClipboard();
760+
return;
761+
}
762+
void writeInput(resolved.data);
763+
},
764+
[hostPlatform, pasteFromClipboard, pendingModifier, terminalId, writeInput],
765+
);
766+
714767
const handleInput = useCallback(
715768
(data: string) => {
716769
if (data.length === 0) {
717770
return;
718771
}
719772

720-
if (pendingModifier === "ctrl") {
721-
setPendingModifierState({ terminalId, value: null });
722-
writeInput(applyCtrlModifier(data));
723-
} else if (pendingModifier === "meta") {
724-
setPendingModifierState({ terminalId, value: null });
725-
writeInput(`\u001b${data}`);
726-
} else {
727-
writeInput(data);
728-
}
773+
writeModifiedInput(data);
729774
},
730-
[pendingModifier, terminalId, writeInput],
775+
[writeModifiedInput],
731776
);
732777

733778
const handleResize = useCallback(
@@ -1021,16 +1066,15 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
10211066
return;
10221067
}
10231068

1024-
setPendingModifierState({ terminalId, value: null });
1025-
if (pendingModifier === "ctrl") {
1026-
writeInput(applyCtrlModifier(action.data));
1027-
} else if (pendingModifier === "meta") {
1028-
writeInput(`\u001b${action.data}`);
1029-
} else {
1030-
writeInput(action.data);
1069+
if (action.kind === "paste") {
1070+
setPendingModifierState({ terminalId, value: null });
1071+
void pasteFromClipboard();
1072+
return;
10311073
}
1074+
1075+
writeModifiedInput(action.data);
10321076
},
1033-
[handleClearTerminal, pendingModifier, terminalId, writeInput],
1077+
[handleClearTerminal, pasteFromClipboard, terminalId, writeModifiedInput],
10341078
);
10351079

10361080
const handleDismissKeyboard = useCallback(() => {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import {
4+
applyCtrlModifier,
5+
chunkTerminalWrite,
6+
encodeTerminalPaste,
7+
hostPlatformFromOs,
8+
resolveModifiedTerminalInput,
9+
TERMINAL_WRITE_MAX_LENGTH,
10+
} from "./terminalInput";
11+
12+
const byte = (code: number) => String.fromCharCode(code);
13+
const ESC = byte(0x1b);
14+
const CTRL_C = byte(0x03);
15+
const CTRL_V = byte(0x16);
16+
17+
describe("applyCtrlModifier", () => {
18+
it("maps letters to control bytes regardless of case", () => {
19+
expect(applyCtrlModifier("c")).toBe(CTRL_C);
20+
expect(applyCtrlModifier("C")).toBe(CTRL_C);
21+
expect(applyCtrlModifier("z")).toBe(byte(0x1a));
22+
});
23+
24+
it("maps the punctuation control keys and leaves the rest untouched", () => {
25+
expect(applyCtrlModifier("[")).toBe(ESC);
26+
expect(applyCtrlModifier("?")).toBe(byte(0x7f));
27+
expect(applyCtrlModifier("1")).toBe("1");
28+
expect(applyCtrlModifier("")).toBe("");
29+
});
30+
});
31+
32+
describe("resolveModifiedTerminalInput", () => {
33+
it("pastes on ctrl+v for windows, linux, and unknown hosts", () => {
34+
for (const hostPlatform of ["windows", "linux", "unknown"] as const) {
35+
expect(resolveModifiedTerminalInput({ data: "v", modifier: "ctrl", hostPlatform })).toEqual({
36+
kind: "paste",
37+
});
38+
expect(resolveModifiedTerminalInput({ data: "V", modifier: "ctrl", hostPlatform })).toEqual({
39+
kind: "paste",
40+
});
41+
}
42+
});
43+
44+
it("keeps alt+v as a meta chord on non-mac hosts", () => {
45+
expect(
46+
resolveModifiedTerminalInput({ data: "v", modifier: "meta", hostPlatform: "windows" }),
47+
).toEqual({ kind: "write", data: `${ESC}v` });
48+
});
49+
50+
it("pastes on cmd+v and forwards raw ctrl+v on mac hosts", () => {
51+
expect(
52+
resolveModifiedTerminalInput({ data: "v", modifier: "meta", hostPlatform: "mac" }),
53+
).toEqual({ kind: "paste" });
54+
expect(
55+
resolveModifiedTerminalInput({ data: "v", modifier: "ctrl", hostPlatform: "mac" }),
56+
).toEqual({ kind: "write", data: CTRL_V });
57+
});
58+
59+
it("still encodes every other modified key", () => {
60+
expect(
61+
resolveModifiedTerminalInput({ data: "c", modifier: "ctrl", hostPlatform: "windows" }),
62+
).toEqual({ kind: "write", data: CTRL_C });
63+
expect(
64+
resolveModifiedTerminalInput({ data: "[A", modifier: "meta", hostPlatform: "linux" }),
65+
).toEqual({ kind: "write", data: `${ESC}[A` });
66+
});
67+
});
68+
69+
describe("encodeTerminalPaste", () => {
70+
it("passes single-line text through unchanged", () => {
71+
expect(encodeTerminalPaste("git switch -c fix/paste")).toBe("git switch -c fix/paste");
72+
expect(encodeTerminalPaste("")).toBe("");
73+
});
74+
75+
it("turns LF and CRLF line breaks into a single carriage return each", () => {
76+
expect(encodeTerminalPaste("one\ntwo\r\nthree\n")).toBe("one\rtwo\rthree\r");
77+
});
78+
79+
it("replaces unsafe control bytes with spaces but keeps tabs", () => {
80+
expect(encodeTerminalPaste(`a${byte(0)}b${ESC}c${byte(0x7f)}d\te`)).toBe("a b c d\te");
81+
});
82+
83+
it("never lets a bracketed-paste end marker reach the shell", () => {
84+
expect(encodeTerminalPaste(`safe${ESC}[201~; rm -rf /\n`)).toBe("safe [201~; rm -rf /\r");
85+
});
86+
});
87+
88+
describe("chunkTerminalWrite", () => {
89+
it("leaves writes within the wire limit whole", () => {
90+
expect(chunkTerminalWrite("")).toEqual([]);
91+
expect(chunkTerminalWrite("ls")).toEqual(["ls"]);
92+
expect(chunkTerminalWrite("x".repeat(TERMINAL_WRITE_MAX_LENGTH))).toHaveLength(1);
93+
});
94+
95+
it("splits oversized writes so every chunk fits the contract", () => {
96+
const chunks = chunkTerminalWrite("y".repeat(TERMINAL_WRITE_MAX_LENGTH * 2 + 5));
97+
expect(chunks.map((chunk) => chunk.length)).toEqual([
98+
TERMINAL_WRITE_MAX_LENGTH,
99+
TERMINAL_WRITE_MAX_LENGTH,
100+
5,
101+
]);
102+
expect(chunks.join("")).toHaveLength(TERMINAL_WRITE_MAX_LENGTH * 2 + 5);
103+
});
104+
105+
it("does not cut a surrogate pair in half at the boundary", () => {
106+
const data = `${"z".repeat(TERMINAL_WRITE_MAX_LENGTH - 1)}😀tail`;
107+
const chunks = chunkTerminalWrite(data);
108+
expect(chunks[0]).toHaveLength(TERMINAL_WRITE_MAX_LENGTH - 1);
109+
expect(chunks[1]).toBe("😀tail");
110+
expect(chunks.join("")).toBe(data);
111+
});
112+
});
113+
114+
describe("hostPlatformFromOs", () => {
115+
it("maps the descriptor os onto the toolbar layout", () => {
116+
expect(hostPlatformFromOs("darwin")).toBe("mac");
117+
expect(hostPlatformFromOs("windows")).toBe("windows");
118+
expect(hostPlatformFromOs("linux")).toBe("linux");
119+
});
120+
121+
it("defers to the caller when the os is unknown or not loaded yet", () => {
122+
expect(hostPlatformFromOs("unknown")).toBeNull();
123+
expect(hostPlatformFromOs(null)).toBeNull();
124+
});
125+
});

0 commit comments

Comments
 (0)