add cross-platform clipboard support and /copy command

This commit is contained in:
Markus Ylisiurunen 2025-12-03 23:43:02 +02:00
parent db6d655ee9
commit 667e7aa730
2 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,28 @@
import { execSync } from "child_process";
import { platform } from "os";
export function copyToClipboard(text: string): void {
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 xclip first, fall back to xsel
try {
execSync("xclip -selection clipboard", options);
} catch {
execSync("xsel --clipboard --input", options);
}
}
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
if (p === "linux") {
throw new Error(`Failed to copy to clipboard. Install xclip or xsel: ${msg}`);
}
throw new Error(`Failed to copy to clipboard: ${msg}`);
}
}