Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/tui/src/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile, spawn } from "node:child_process"
import fs from "node:fs"
import { readFile, rm } from "node:fs/promises"
import { platform, release, tmpdir } from "node:os"
import path from "node:path"
Expand All @@ -23,7 +24,12 @@ function command(command: string, args: string[] = [], input?: string) {
function writeOsc52(text: string) {
if (!process.stdout.isTTY) return
const sequence = `\x1b]52;c;${Buffer.from(text).toString("base64")}\x07`
process.stdout.write(process.env.TMUX || process.env.STY ? `\x1bPtmux;\x1b${sequence}\x1b\\` : sequence)
const payload = process.env.TMUX || process.env.STY ? `\x1bPtmux;\x1b${sequence}\x1b\\` : sequence
try {
fs.writeSync(process.stdout.fd, payload)
} catch {
// Terminal not available
}
}

export async function read() {
Expand Down Expand Up @@ -80,7 +86,7 @@ export function copyCommand(
): string[] | undefined {
if (os === "darwin" && has("osascript")) return ["osascript"]
if (os === "linux" && wayland && has("wl-copy")) return ["wl-copy"]
if (os === "linux" && has("xclip")) return ["xclip", "-selection", "clipboard"]
if (os === "linux" && has("xclip")) return ["xclip", "-selection", "clipboard", "-i"]
if (os === "linux" && has("xsel")) return ["xsel", "--clipboard", "--input"]
if (os === "win32" && has("powershell.exe")) {
return [
Expand All @@ -102,17 +108,17 @@ function getCopyMethod() {
if (native?.[0] === "osascript") {
return async (text: string) => {
const escaped = text.replace(/\\/g, "\\\\").replace(/"/g, '\\"')
await command("osascript", ["-e", `set the clipboard to "${escaped}"`]).catch(() => undefined)
await command("osascript", ["-e", `set the clipboard to "${escaped}"`])
}
}
if (native) {
return async (text: string) => {
await command(native[0], native.slice(1), text).catch(() => undefined)
await command(native[0], native.slice(1), text)
}
}
return async (text: string) => {
const { default: clipboardy } = await import("clipboardy")
await clipboardy.write(text).catch(() => undefined)
await clipboardy.write(text)
}
})())
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tui/test/clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test("uses osascript on macOS", () => {
})

test("falls back through X11 clipboard commands", () => {
expect(copyCommand("linux", true, (name) => name === "xclip")).toEqual(["xclip", "-selection", "clipboard"])
expect(copyCommand("linux", true, (name) => name === "xclip")).toEqual(["xclip", "-selection", "clipboard", "-i"])
expect(copyCommand("linux", false, (name) => name === "xsel")).toEqual(["xsel", "--clipboard", "--input"])
})

Expand Down
Loading