mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-19 10:03:19 +00:00
- Make @mariozechner/clipboard an optional dependency - Lazy-load clipboard module with graceful fallback - Add Termux clipboard support via termux-clipboard-set - Skip image clipboard on Termux (not supported) fixes #1164
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { execSync, spawn } from "child_process";
|
|
import { platform } from "os";
|
|
import { isWaylandSession } from "./clipboard-image.js";
|
|
|
|
export function copyToClipboard(text: string): void {
|
|
// Always emit OSC 52 - works over SSH/mosh, harmless locally
|
|
const encoded = Buffer.from(text).toString("base64");
|
|
process.stdout.write(`\x1b]52;c;${encoded}\x07`);
|
|
|
|
// Also try native tools (best effort for local sessions)
|
|
const p = platform();
|
|
const options = { input: text, timeout: 5000 };
|
|
|
|
try {
|
|
if (p === "darwin") {
|
|
execSync("pbcopy", options);
|
|
} else if (p === "win32") {
|
|
execSync("clip", options);
|
|
} else {
|
|
// Linux. Try Termux, Wayland, or X11 clipboard tools.
|
|
if (process.env.TERMUX_VERSION) {
|
|
try {
|
|
execSync("termux-clipboard-set", options);
|
|
return;
|
|
} catch {
|
|
// Fall back to Wayland or X11 tools.
|
|
}
|
|
}
|
|
|
|
const isWayland = isWaylandSession();
|
|
if (isWayland) {
|
|
try {
|
|
// Verify wl-copy exists (spawn errors are async and won't be caught)
|
|
execSync("which wl-copy", { stdio: "ignore" });
|
|
// wl-copy with execSync hangs due to fork behavior; use spawn instead
|
|
const proc = spawn("wl-copy", [], { stdio: ["pipe", "ignore", "ignore"] });
|
|
proc.stdin.on("error", () => {
|
|
// Ignore EPIPE errors if wl-copy exits early
|
|
});
|
|
proc.stdin.write(text);
|
|
proc.stdin.end();
|
|
proc.unref();
|
|
} catch {
|
|
// Fall back to xclip/xsel (works on XWayland)
|
|
try {
|
|
execSync("xclip -selection clipboard", options);
|
|
} catch {
|
|
execSync("xsel --clipboard --input", options);
|
|
}
|
|
}
|
|
} else {
|
|
try {
|
|
execSync("xclip -selection clipboard", options);
|
|
} catch {
|
|
execSync("xsel --clipboard --input", options);
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
// Ignore - OSC 52 already emitted as fallback
|
|
}
|
|
}
|