Skip to content

Commit 3f16be4

Browse files
committed
feat(mobile): add terminal toolbar paste button
Read clipboard on press, sanitize paste-end injection, and encode newlines for PTY input without unconditional bracketed-paste wrapping.
1 parent fff33f9 commit 3f16be4

3 files changed

Lines changed: 98 additions & 1 deletion

File tree

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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";
910
import {
1011
KeyboardController,
1112
KeyboardEvents,
@@ -66,6 +67,7 @@ import {
6667
type TerminalMenuSession,
6768
} from "./terminalMenu";
6869
import { cacheTerminalGridSize, getCachedTerminalGridSize } from "./terminalUiState";
70+
import { encodeTerminalPaste } from "./encodeTerminalPaste";
6971

7072
const DEFAULT_TERMINAL_COLS = 80;
7173
const DEFAULT_TERMINAL_ROWS = 24;
@@ -78,6 +80,7 @@ type HostPlatform = "mac" | "linux" | "windows" | "unknown";
7880
type TerminalToolbarAction =
7981
| { readonly kind: "send"; readonly key: string; readonly label: string; readonly data: string }
8082
| { readonly kind: "clear"; readonly key: string; readonly label: string }
83+
| { readonly kind: "paste"; readonly key: string; readonly label: string }
8184
| {
8285
readonly kind: "modifier";
8386
readonly key: string;
@@ -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" },
@@ -1003,6 +1007,29 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
10031007
});
10041008
}, [clearTerminal, selectedThread, terminalId]);
10051009

1010+
const handlePasteFromClipboard = useCallback(() => {
1011+
setPendingModifierState({ terminalId, value: null });
1012+
void (async () => {
1013+
let text: string;
1014+
try {
1015+
text = await Clipboard.getStringAsync();
1016+
} catch {
1017+
return;
1018+
}
1019+
if (text.length === 0) {
1020+
return;
1021+
}
1022+
// Bracketed paste (DECSET 2004) is not exposed from the mobile native
1023+
// surface yet, so encode like Ghostty with bracketed=false: sanitize and
1024+
// map newlines to CR without wrapping ESC[200~/ESC[201~.
1025+
const encoded = encodeTerminalPaste(text);
1026+
if (encoded.length === 0) {
1027+
return;
1028+
}
1029+
writeInput(encoded);
1030+
})();
1031+
}, [terminalId, writeInput]);
1032+
10061033
const handleToolbarActionPress = useCallback(
10071034
(action: TerminalToolbarAction) => {
10081035
if (action.kind === "modifier") {
@@ -1021,6 +1048,11 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
10211048
return;
10221049
}
10231050

1051+
if (action.kind === "paste") {
1052+
handlePasteFromClipboard();
1053+
return;
1054+
}
1055+
10241056
setPendingModifierState({ terminalId, value: null });
10251057
if (pendingModifier === "ctrl") {
10261058
writeInput(applyCtrlModifier(action.data));
@@ -1030,7 +1062,7 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
10301062
writeInput(action.data);
10311063
}
10321064
},
1033-
[handleClearTerminal, pendingModifier, terminalId, writeInput],
1065+
[handleClearTerminal, handlePasteFromClipboard, pendingModifier, terminalId, writeInput],
10341066
);
10351067

10361068
const handleDismissKeyboard = useCallback(() => {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { encodeTerminalPaste } from "./encodeTerminalPaste";
4+
5+
describe("encodeTerminalPaste", () => {
6+
it("returns empty for empty input", () => {
7+
expect(encodeTerminalPaste("")).toBe("");
8+
});
9+
10+
it("passes through plain text when not bracketed", () => {
11+
expect(encodeTerminalPaste("hello")).toBe("hello");
12+
});
13+
14+
it("replaces newlines with carriage returns when not bracketed", () => {
15+
expect(encodeTerminalPaste("one\ntwo\r\nthree")).toBe("one\rtwo\rthree");
16+
});
17+
18+
it("strips bracketed-paste end sequences to block injection", () => {
19+
expect(encodeTerminalPaste("safe\u001b[201~; rm -rf /\n")).toBe("safe; rm -rf /\r");
20+
expect(encodeTerminalPaste("\u001b[201~\u001b[201~")).toBe("");
21+
});
22+
23+
it("wraps only when bracketed is explicitly enabled", () => {
24+
expect(encodeTerminalPaste("a\nb", { bracketed: true })).toBe("\u001b[200~a\nb\u001b[201~");
25+
});
26+
27+
it("still strips embedded end sequences before wrapping", () => {
28+
expect(encodeTerminalPaste("x\u001b[201~y", { bracketed: true })).toBe(
29+
"\u001b[200~xy\u001b[201~",
30+
);
31+
});
32+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Encode clipboard text for PTY write, matching web Ghostty `encodePaste`
3+
* semantics as closely as we can without a native mode query.
4+
*
5+
* Web reads DECSET 2004 (bracketed paste) from libghostty and only wraps when
6+
* active. Mobile's native surface does not expose that mode yet, so callers
7+
* default to `bracketed: false` — never emit literal ESC[200~/ESC[201~ into
8+
* shells that have not enabled the mode.
9+
*
10+
* Always strips ESC[201~ from the payload to block paste-end injection.
11+
*/
12+
13+
const BRACKETED_PASTE_START = "\u001b[200~";
14+
const BRACKETED_PASTE_END = "\u001b[201~";
15+
16+
export function encodeTerminalPaste(
17+
text: string,
18+
options: { readonly bracketed?: boolean } = {},
19+
): string {
20+
// Split/join so every occurrence is removed, including adjacent repeats.
21+
const sanitized = text.split(BRACKETED_PASTE_END).join("");
22+
if (sanitized.length === 0) {
23+
return "";
24+
}
25+
26+
if (options.bracketed === true) {
27+
return `${BRACKETED_PASTE_START}${sanitized}${BRACKETED_PASTE_END}`;
28+
}
29+
30+
// Ghostty replaces newlines with CR when bracketed paste is off so each
31+
// line is a carriage return rather than a bare LF (Ctrl+J in raw mode).
32+
return sanitized.replace(/\r\n/g, "\r").replace(/\n/g, "\r");
33+
}

0 commit comments

Comments
 (0)