| 27 | } |
| 28 | |
| 29 | export async function read() { |
| 30 | if (platform() === "darwin") { |
| 31 | const file = path.join(tmpdir(), "opencode-clipboard.png") |
| 32 | try { |
| 33 | await exec("osascript", [ |
| 34 | "-e", |
| 35 | 'set imageData to the clipboard as "PNGf"', |
| 36 | "-e", |
| 37 | `set fileRef to open for access POSIX file "${file}" with write permission`, |
| 38 | "-e", |
| 39 | "set eof fileRef to 0", |
| 40 | "-e", |
| 41 | "write imageData to fileRef", |
| 42 | "-e", |
| 43 | "close access fileRef", |
| 44 | ]) |
| 45 | return { data: (await readFile(file)).toString("base64"), mime: "image/png" } |
| 46 | } catch { |
| 47 | // Fall through to text clipboard. |
| 48 | } finally { |
| 49 | await rm(file, { force: true }).catch(() => {}) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (platform() === "win32" || release().includes("WSL")) { |
| 54 | const script = |
| 55 | "Add-Type -AssemblyName System.Windows.Forms; $img = [System.Windows.Forms.Clipboard]::GetImage(); if ($img) { $ms = New-Object System.IO.MemoryStream; $img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png); [System.Convert]::ToBase64String($ms.ToArray()) }" |
| 56 | const image = await command("powershell.exe", ["-NonInteractive", "-NoProfile", "-command", script]).catch(() => |
| 57 | Buffer.alloc(0), |
| 58 | ) |
| 59 | if (image.length) return { data: image.toString().trim(), mime: "image/png" } |
| 60 | } |
| 61 | |
| 62 | if (platform() === "linux") { |
| 63 | const wayland = await command("wl-paste", ["-t", "image/png"]).catch(() => Buffer.alloc(0)) |
| 64 | if (wayland.length) return { data: wayland.toString("base64"), mime: "image/png" } |
| 65 | const x11 = await command("xclip", ["-selection", "clipboard", "-t", "image/png", "-o"]).catch(() => |
| 66 | Buffer.alloc(0), |
| 67 | ) |
| 68 | if (x11.length) return { data: x11.toString("base64"), mime: "image/png" } |
| 69 | } |
| 70 | |
| 71 | const { default: clipboardy } = await import("clipboardy") |
| 72 | const text = await clipboardy.read().catch(() => undefined) |
| 73 | if (text) return { data: text, mime: "text/plain" } |
| 74 | } |
| 75 | |
| 76 | export function copyCommand( |
| 77 | os: NodeJS.Platform, |