|
1 | 1 | import { $ } from "bun" |
2 | | -import type { CliRenderer } from "@opentui/core" |
3 | 2 | import { platform, release } from "os" |
4 | 3 | import clipboardy from "clipboardy" |
5 | 4 | import { lazy } from "../../../../util/lazy.js" |
6 | 5 | import { tmpdir } from "os" |
7 | 6 | import path from "path" |
8 | 7 |
|
9 | | -const rendererRef = { current: undefined as CliRenderer | undefined } |
| 8 | +/** |
| 9 | + * Writes text to clipboard via OSC 52 escape sequence. |
| 10 | + * This allows clipboard operations to work over SSH by having |
| 11 | + * the terminal emulator handle the clipboard locally. |
| 12 | + */ |
| 13 | +function writeOsc52(text: string): void { |
| 14 | + if (!process.stdout.isTTY) return |
| 15 | + const base64 = Buffer.from(text).toString("base64") |
| 16 | + const osc52 = `\x1b]52;c;${base64}\x07` |
| 17 | + const passthrough = process.env["TMUX"] || process.env["STY"] |
| 18 | + const sequence = passthrough ? `\x1bPtmux;\x1b${osc52}\x1b\\` : osc52 |
| 19 | + process.stdout.write(sequence) |
| 20 | +} |
10 | 21 |
|
11 | 22 | export namespace Clipboard { |
12 | 23 | export interface Content { |
13 | 24 | data: string |
14 | 25 | mime: string |
15 | 26 | } |
16 | 27 |
|
17 | | - export function setRenderer(renderer: CliRenderer | undefined): void { |
18 | | - rendererRef.current = renderer |
19 | | - } |
20 | | - |
21 | 28 | export async function read(): Promise<Content | undefined> { |
22 | 29 | const os = platform() |
23 | 30 |
|
@@ -146,12 +153,7 @@ export namespace Clipboard { |
146 | 153 | }) |
147 | 154 |
|
148 | 155 | export async function copy(text: string): Promise<void> { |
149 | | - const renderer = rendererRef.current |
150 | | - if (renderer) { |
151 | | - // Try OSC52 but don't early return - always fall back to native method |
152 | | - // OSC52 may report success but not actually work in all terminals |
153 | | - renderer.copyToClipboardOSC52(text) |
154 | | - } |
| 156 | + writeOsc52(text) |
155 | 157 | await getCopyMethod()(text) |
156 | 158 | } |
157 | 159 | } |
0 commit comments