Open History to see the last commit
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;
36}
37
38/** Use the device clipboard for host paste chords; forwarding Ctrl+V to PowerShell pastes the host clipboard. */
39export function resolveModifiedTerminalInput(input: {
40 readonly data: string;
41 readonly modifier: PendingModifier;
42 readonly hostPlatform: HostPlatform;
43}): ModifiedTerminalInput {
44 const pasteModifier: PendingModifier = input.hostPlatform === "mac" ? "meta" : "ctrl";
45 if (input.modifier === pasteModifier && input.data.toLowerCase() === "v") {
46 return { kind: "paste" };
47 }
48
49 return {
50 kind: "write",
51 data: input.modifier === "ctrl" ? applyCtrlModifier(input.data) : `\u001b${input.data}`,
52 };
53}
54
55/** No DECSET 2004 state, so omit bracket markers. Strip unsafe controls and use CR; raw-mode TUIs read LF as Ctrl+J. */
56export function encodeTerminalPaste(text: string): string {
57 return text.replace(UNSAFE_PASTE_BYTES, " ").replace(/\r\n|\n/g, "\r");
58}
59
60export function chunkTerminalWrite(data: string): ReadonlyArray<string> {
61 const chunks: string[] = [];
62 let start = 0;
63 while (start < data.length) {
64 let end = Math.min(start + TERMINAL_WRITE_MAX_LENGTH, data.length);
65 const last = data.charCodeAt(end - 1);
66 if (end < data.length && last >= 0xd800 && last <= 0xdbff) {
67 end -= 1;
68 }
69 chunks.push(data.slice(start, end));
70 start = end;
71 }
72 return chunks;
73}
74
75/** Returns null for unknown OSes so callers can try weaker signals. */
76export function hostPlatformFromOs(os: ExecutionEnvironmentPlatformOs | null): HostPlatform | null {
77 if (os === "darwin") return "mac";
78 if (os === "linux") return "linux";
79 if (os === "windows") return "windows";
80 return null;
81}