|
| 1 | +--- Image pasting functionality from clipboard |
| 2 | +--- @see https://github.com/sst/opencode/blob/45180104fe84e2d0b9d29be0f9f8a5e52d18e102/packages/opencode/src/cli/cmd/tui/util/clipboard.ts |
| 3 | +local context = require('opencode.context') |
| 4 | +local state = require('opencode.state') |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +--- Check if a file exists and has content |
| 9 | +--- @param path string |
| 10 | +--- @return boolean |
| 11 | +local function is_valid_file(path) |
| 12 | + return vim.fn.getfsize(path) > 0 |
| 13 | +end |
| 14 | + |
| 15 | +--- Run shell or powershell command and return success |
| 16 | +--- @param cmd string|table |
| 17 | +--- @param opts table? |
| 18 | +--- @return boolean |
| 19 | +local function run_shell_cmd(cmd, opts) |
| 20 | + local sys_cmd |
| 21 | + if type(cmd) == 'string' then |
| 22 | + sys_cmd = { 'sh', '-c', cmd } |
| 23 | + else |
| 24 | + sys_cmd = cmd |
| 25 | + end |
| 26 | + return vim.system(sys_cmd, opts):wait().code == 0 |
| 27 | +end |
| 28 | + |
| 29 | +--- Save base64 data to file |
| 30 | +--- @param data string |
| 31 | +--- @param path string |
| 32 | +--- @return boolean |
| 33 | +local function save_base64(data, path) |
| 34 | + if vim.fn.has('win32') == 1 then |
| 35 | + local script = |
| 36 | + string.format('[System.IO.File]::WriteAllBytes("%s", [System.Convert]::FromBase64String("%s"))', path, data) |
| 37 | + return run_shell_cmd({ 'powershell.exe', '-command', '-' }, { stdin = script }) |
| 38 | + else |
| 39 | + local decode_arg = vim.uv.os_uname().sysname == 'Darwin' and '-D' or '-d' |
| 40 | + return run_shell_cmd(string.format('base64 %s > "%s"', decode_arg, path), { stdin = data }) |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +--- macOS clipboard image handler using osascript |
| 45 | +--- @param path string |
| 46 | +--- @return boolean |
| 47 | +local function handle_darwin_clipboard(path) |
| 48 | + if vim.fn.executable('osascript') ~= 1 then |
| 49 | + return false |
| 50 | + end |
| 51 | + local cmd = string.format( |
| 52 | + "osascript -e 'set imageData to the clipboard as \"PNGf\"' -e 'set fileRef to open for access POSIX file \"%s\" with write permission' -e 'set eof fileRef to 0' -e 'write imageData to fileRef' -e 'close access fileRef'", |
| 53 | + path |
| 54 | + ) |
| 55 | + return run_shell_cmd(cmd) |
| 56 | +end |
| 57 | + |
| 58 | +--- Linux clipboard image handler supporting Wayland and X11 |
| 59 | +--- @param path string |
| 60 | +--- @return boolean |
| 61 | +local function handle_linux_clipboard(path) |
| 62 | + if vim.fn.executable('wl-paste') == 1 and run_shell_cmd(string.format('wl-paste -t image/png > "%s"', path)) then |
| 63 | + return true |
| 64 | + end |
| 65 | + return vim.fn.executable('xclip') == 1 |
| 66 | + and run_shell_cmd(string.format('xclip -selection clipboard -t image/png -o > "%s"', path)) |
| 67 | +end |
| 68 | + |
| 69 | +--- Windows clipboard image handler using PowerShell |
| 70 | +--- @param path string |
| 71 | +--- @return boolean |
| 72 | +local function handle_windows_clipboard(path) |
| 73 | + if vim.fn.executable('powershell.exe') ~= 1 then |
| 74 | + return false |
| 75 | + end |
| 76 | + local script = string.format( |
| 77 | + [[ |
| 78 | + Add-Type -AssemblyName System.Windows.Forms; |
| 79 | + $img = [System.Windows.Forms.Clipboard]::GetImage(); |
| 80 | + if ($img) { |
| 81 | + $img.Save('%s', [System.Drawing.Imaging.ImageFormat]::Png); |
| 82 | + } else { |
| 83 | + exit 1 |
| 84 | + } |
| 85 | + ]], |
| 86 | + path |
| 87 | + ) |
| 88 | + return run_shell_cmd({ 'powershell.exe', '-command', '-' }, { stdin = script }) |
| 89 | +end |
| 90 | + |
| 91 | +local handlers = { |
| 92 | + Darwin = handle_darwin_clipboard, |
| 93 | + Linux = handle_linux_clipboard, |
| 94 | + Windows_NT = handle_windows_clipboard, |
| 95 | +} |
| 96 | + |
| 97 | +--- Try to get image from system clipboard |
| 98 | +--- @param image_path string |
| 99 | +--- @return boolean |
| 100 | +local function try_system_clipboard(image_path) |
| 101 | + local os_name = vim.uv.os_uname().sysname |
| 102 | + local handler = handlers[os_name] |
| 103 | + |
| 104 | + -- WSL detection and override |
| 105 | + if vim.fn.exists('$WSL_DISTRO_NAME') == 1 then |
| 106 | + handler = handlers.Windows_NT |
| 107 | + end |
| 108 | + |
| 109 | + return handler and handler(image_path) and is_valid_file(image_path) or false |
| 110 | +end |
| 111 | + |
| 112 | +--- Try to parse base64 image data from clipboard |
| 113 | +--- @param temp_dir string |
| 114 | +--- @param timestamp string |
| 115 | +--- @return boolean, string? |
| 116 | +local function try_base64_clipboard(temp_dir, timestamp) |
| 117 | + local content = vim.fn.getreg('+') |
| 118 | + if not content or content == '' then |
| 119 | + return false |
| 120 | + end |
| 121 | + |
| 122 | + local format, data = content:match('^data:image/([^;]+);base64,(.+)$') |
| 123 | + if not format or not data then |
| 124 | + return false |
| 125 | + end |
| 126 | + |
| 127 | + local image_path = string.format('%s/pasted_image_%s.%s', temp_dir, timestamp, format) |
| 128 | + local success = save_base64(data, image_path) and is_valid_file(image_path) |
| 129 | + |
| 130 | + return success, success and image_path or nil |
| 131 | +end |
| 132 | + |
| 133 | +--- Handle clipboard image data by saving it to a file and adding it to context |
| 134 | +--- @return boolean success True if image was successfully handled |
| 135 | +function M.paste_image_from_clipboard() |
| 136 | + local temp_dir = vim.fn.tempname() |
| 137 | + vim.fn.mkdir(temp_dir, 'p') |
| 138 | + local timestamp = os.date('%Y%m%d_%H%M%S') |
| 139 | + local image_path = string.format('%s/pasted_image_%s.png', temp_dir, timestamp) |
| 140 | + |
| 141 | + local success = try_system_clipboard(image_path) |
| 142 | + |
| 143 | + if not success then |
| 144 | + local base64_success, base64_path = try_base64_clipboard(temp_dir, timestamp --[[@as string]]) |
| 145 | + success = base64_success |
| 146 | + if base64_path then |
| 147 | + image_path = base64_path |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + if success then |
| 152 | + context.add_file(image_path) |
| 153 | + state.context_updated_at = os.time() |
| 154 | + vim.notify('Image saved and added to context: ' .. vim.fn.fnamemodify(image_path, ':t'), vim.log.levels.INFO) |
| 155 | + return true |
| 156 | + end |
| 157 | + |
| 158 | + vim.notify('No image found in clipboard.', vim.log.levels.WARN) |
| 159 | + return false |
| 160 | +end |
| 161 | + |
| 162 | +return M |
0 commit comments