Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalInput.ts

97 lines3.5 KB

1import type { ExecutionEnvironmentPlatformOs } from "@t3tools/contracts";
2​
3export type PendingModifier = "ctrl" | "meta";
4export type HostPlatform = "mac" | "linux" | "windows" | "unknown";
5​
6/** Upper bound of `TerminalWriteInput.data`; longer writes are rejected by the server. */
7export const TERMINAL_WRITE_MAX_LENGTH = 65_536;
8​
9export type ModifiedTerminalInput =
10 | { readonly kind: "write"; readonly data: string }
11 | { readonly kind: "paste" };
12​
13// eslint-disable-next-line no-control-regex -- Pasted text must not carry raw terminal controls.
14const UNSAFE_PASTE_BYTES = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g;
15​
16export function applyCtrlModifier(input: string): string {
17 const firstCharacter = input[0];
18 if (!firstCharacter) {
19 return input;
20 }
21​
22 const lowerCharacter = firstCharacter.toLowerCase();
23 if (lowerCharacter >= "a" && lowerCharacter <= "z") {
24 return String.fromCharCode(lowerCharacter.charCodeAt(0) - 96);
25 }
26​
27 if (firstCharacter === "@") return "\u0000";
28 if (firstCharacter === "[") return "\u001b";
29 if (firstCharacter === "\\") return "\u001c";
30 if (firstCharacter === "]") return "\u001d";
31 if (firstCharacter === "^") return "\u001e";
32 if (firstCharacter === "_") return "\u001f";
33 if (firstCharacter === "?") return "\u007f";
34​
35 return input;
37​
38/**
39 * The host's paste chord (cmd+v on a macOS host, ctrl+v elsewhere) pastes the
40 * device clipboard instead of reaching the remote shell as a raw control byte,
41 * which matches what the web terminal does with the same chord. Forwarding the
42 * byte is never what a phone user means: PowerShell binds ctrl+v to paste from
43 * the host machine's clipboard, so the shell inserts whatever the desktop last
44 * copied rather than the text on the phone.
45 */
46export function resolveModifiedTerminalInput(input: {
47 readonly data: string;
48 readonly modifier: PendingModifier;
49 readonly hostPlatform: HostPlatform;
50}): ModifiedTerminalInput {
51 const pasteModifier: PendingModifier = input.hostPlatform === "mac" ? "meta" : "ctrl";
52 if (input.modifier === pasteModifier && input.data.toLowerCase() === "v") {
53 return { kind: "paste" };
54 }
55​
56 return {
57 kind: "write",
58 data: input.modifier === "ctrl" ? applyCtrlModifier(input.data) : `\u001b${input.data}`,
59 };
61​
62/**
63 * Encodes clipboard text for the remote pty the way the web terminal does when
64 * bracketed paste is off: unsafe control bytes become spaces (which also
65 * defuses an embedded bracketed-paste end marker, since its ESC goes too) and
66 * line breaks become carriage returns, since a bare LF is Ctrl+J to a raw-mode
67 * TUI. The native mobile surface does not expose DECSET 2004, so mobile never
68 * wraps a paste in bracketed-paste markers.
69 */
70export function encodeTerminalPaste(text: string): string {
71 return text.replace(UNSAFE_PASTE_BYTES, " ").replace(/\r\n|\n/g, "\r");
73​
74export function chunkTerminalWrite(data: string): ReadonlyArray<string> {
75 const chunks: string[] = [];
76 let start = 0;
77 while (start < data.length) {
78 let end = Math.min(start + TERMINAL_WRITE_MAX_LENGTH, data.length);
79 const last = data.charCodeAt(end - 1);
80 if (end < data.length && last >= 0xd800 && last <= 0xdbff) {
81 end -= 1;
82 }
83 chunks.push(data.slice(start, end));
84 start = end;
85 }
86 return chunks;
88​
89/**
90 * Returns null for "unknown" so callers can fall back to a weaker signal.
91 */
92export function hostPlatformFromOs(os: ExecutionEnvironmentPlatformOs | null): HostPlatform | null {
93 if (os === "darwin") return "mac";
94 if (os === "linux") return "linux";
95 if (os === "windows") return "windows";
96 return null;