{"version":3,"file":"clipboard.d.ts","sourceRoot":"","sources":["../../src/utils/clipboard.ts"],"names":[],"mappings":"AAqBA,iDAAiD;AACjD,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAqBhE;AAED;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,6JACkH,CAAC;AAqC5J,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAyDjE","sourcesContent":["import { platform } from \"os\";\nimport { runClipboardCommand } from \"./clipboard-command.ts\";\nimport { isWaylandSession } from \"./clipboard-image.ts\";\nimport { clipboard } from \"./clipboard-native.ts\";\n\nconst MAX_OSC52_ENCODED_LENGTH = 100_000;\nconst CLIPBOARD_COMMAND_TIMEOUT_MS = 5000;\n\nfunction isRemoteSession(env: NodeJS.ProcessEnv = process.env): boolean {\n\treturn Boolean(env.SSH_CONNECTION || env.SSH_CLIENT || env.MOSH_CONNECTION);\n}\n\nfunction emitOsc52(text: string): boolean {\n\tconst encoded = Buffer.from(text).toString(\"base64\");\n\tif (encoded.length > MAX_OSC52_ENCODED_LENGTH) {\n\t\treturn false;\n\t}\n\tprocess.stdout.write(`\\x1b]52;c;${encoded}\\x07`);\n\treturn true;\n}\n\n/** Read plain text from the system clipboard. */\nexport async function readClipboardText(): Promise<string | null> {\n\tif (platform() === \"linux\" && isWaylandSession() && process.env.WAYLAND_DISPLAY) {\n\t\t// Asynchronous so a slow or hung wl-paste never blocks the TUI event loop.\n\t\tconst bytes = await runClipboardCommand(\"wl-paste\", [\"--no-newline\", \"--type\", \"text\"], {\n\t\t\ttimeoutMs: CLIPBOARD_COMMAND_TIMEOUT_MS,\n\t\t});\n\t\tif (bytes !== undefined) {\n\t\t\treturn bytes.toString(\"utf8\") || null;\n\t\t}\n\t}\n\n\tif (!clipboard) {\n\t\treturn null;\n\t}\n\n\ttry {\n\t\tconst text = await clipboard.getText();\n\t\treturn text || null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\n/**\n * Windows writer: `clip.exe` decodes stdin with the console code page, so UTF-8 accents\n * arrive corrupted, and UTF-16LE input either keeps its BOM in the clipboard or is\n * misdetected without one (CJK). PowerShell reads stdin as UTF-8 and calls Set-Clipboard;\n * the text never touches the command line, so nothing is interpolated or escaped.\n */\nexport const WINDOWS_SET_CLIPBOARD_SCRIPT =\n\t\"$ErrorActionPreference = 'Stop'; [Console]::InputEncoding = New-Object System.Text.UTF8Encoding $false; Set-Clipboard -Value ([Console]::In.ReadToEnd())\";\n\n/** Platform clipboard writers, in preference order. */\nfunction getClipboardWriteCommands(p: NodeJS.Platform, env: NodeJS.ProcessEnv): Array<[string, string[]]> {\n\tif (p === \"darwin\") return [[\"pbcopy\", []]];\n\tif (p === \"win32\") {\n\t\t// clip stays as the fallback: it handles ASCII and the empty string, which Set-Clipboard rejects.\n\t\treturn [\n\t\t\t[\"powershell.exe\", [\"-NoProfile\", \"-NonInteractive\", \"-Command\", WINDOWS_SET_CLIPBOARD_SCRIPT]],\n\t\t\t[\"clip\", []],\n\t\t];\n\t}\n\n\tconst commands: Array<[string, string[]]> = [];\n\tif (env.TERMUX_VERSION) commands.push([\"termux-clipboard-set\", []]);\n\tif (isWaylandSession(env) && env.WAYLAND_DISPLAY) commands.push([\"wl-copy\", []]);\n\tif (env.DISPLAY) {\n\t\tcommands.push([\"xclip\", [\"-selection\", \"clipboard\"]], [\"xsel\", [\"--clipboard\", \"--input\"]]);\n\t}\n\treturn commands;\n}\n\nfunction getClipboardFailureMessage(p: NodeJS.Platform, env: NodeJS.ProcessEnv): string {\n\tif (p === \"linux\") {\n\t\tif (env.TERMUX_VERSION) {\n\t\t\treturn \"Failed to copy to clipboard: install the Termux:API app and the `termux-api` package\";\n\t\t}\n\t\tif (env.WAYLAND_DISPLAY) {\n\t\t\treturn \"Failed to copy to clipboard: install `wl-clipboard` (`wl-copy`) or check Wayland access\";\n\t\t}\n\t\tif (env.DISPLAY) {\n\t\t\treturn \"Failed to copy to clipboard: install `xclip` or `xsel`, or check X11 access\";\n\t\t}\n\t}\n\treturn \"Failed to copy to clipboard\";\n}\n\nexport async function copyToClipboard(text: string): Promise<void> {\n\tlet copied = false;\n\n\tconst p = platform();\n\tconst env = process.env;\n\n\t// Prefer direct clipboard writes. Emitting OSC 52 first can make terminals\n\t// write the same native clipboard concurrently with the addon, and very large\n\t// OSC 52 payloads can desynchronize terminal rendering.\n\t//\n\t// On Linux, skip the native addon. The underlying `clipboard-rs` crate is\n\t// X11-only and does not retain selection ownership after `set_text`\n\t// resolves, so on Wayland-only compositors (Hyprland, Niri, ...) and even\n\t// some X11 sessions the call resolves successfully without populating the\n\t// clipboard. The platform tools below (wl-copy, xclip, xsel) properly\n\t// daemonize and keep ownership.\n\ttry {\n\t\tif (clipboard && p !== \"linux\") {\n\t\t\tawait clipboard.setText(text);\n\t\t\tcopied = true;\n\t\t}\n\t} catch {\n\t\t// Fall through to platform-specific clipboard tools.\n\t}\n\n\tconst remote = isRemoteSession(env);\n\tif (copied && !remote) {\n\t\treturn;\n\t}\n\n\tif (!copied) {\n\t\t// Spawned asynchronously (no execSync): a hung clipboard tool must not freeze the TUI.\n\t\tfor (const [command, args] of getClipboardWriteCommands(p, env)) {\n\t\t\tconst result = await runClipboardCommand(command, args, {\n\t\t\t\tinput: text,\n\t\t\t\ttimeoutMs: CLIPBOARD_COMMAND_TIMEOUT_MS,\n\t\t\t});\n\t\t\tif (result !== undefined) {\n\t\t\t\tcopied = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\t// OSC 52 cannot be verified, so a local desktop session reports the failure instead of\n\t// a false success (#9618). Remote sessions always emit it to reach the client clipboard,\n\t// and display-less Linux (containers, WSL without WSLg) has no other clipboard route.\n\tconst headless = p === \"linux\" && !env.DISPLAY && !env.WAYLAND_DISPLAY && !env.TERMUX_VERSION;\n\tlet oversized = false;\n\tif (remote || (!copied && headless)) {\n\t\tif (emitOsc52(text)) copied = true;\n\t\telse oversized = true;\n\t}\n\n\tif (copied) return;\n\tif (oversized) throw new Error(\"Failed to copy to clipboard: text exceeds the OSC 52 size limit\");\n\tthrow new Error(getClipboardFailureMessage(p, env));\n}\n"]}