Skip to content

Commit

Permalink
feat(clipboard): process image when pasting from clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Mar 24, 2024
1 parent 294aa40 commit 841d8d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 11 additions & 6 deletions lua/img-clip/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,28 @@ M.content_is_image = function()
return false
end

---@param file_path string
---@return boolean
M.save_image = function(file_path)
local cmd = M.get_clip_cmd()
local process_cmd = config.get_opt("process_cmd")
if process_cmd ~= "" then
process_cmd = "| " .. process_cmd .. " "
end

-- Linux (X11)
if cmd == "xclip" then
local command = string.format('xclip -selection clipboard -o -t image/png > "%s"', file_path)
local command = string.format('xclip -selection clipboard -o -t image/png %s> "%s"', process_cmd, file_path)
local _, exit_code = util.execute(command)
return exit_code == 0

-- Linux (Wayland)
elseif cmd == "wl-paste" then
local command = string.format('wl-paste --type image/png > "%s"', file_path)
local command = string.format('wl-paste --type image/png %s> "%s"', process_cmd, file_path)
local _, exit_code = util.execute(command)
return exit_code == 0

-- MacOS (pngpaste) which is faster than osascript
elseif cmd == "pngpaste" then
local command = string.format('pngpaste "%s"', file_path)
local command = string.format('pngpaste - %s> "%s"', process_cmd, file_path)
local _, exit_code = util.execute(command)
return exit_code == 0

Expand All @@ -98,7 +100,10 @@ M.save_image = function(file_path)
local command = string.format(
[[osascript -e 'set theFile to (open for access POSIX file "%s" with write permission)' ]]
.. [[-e 'try' -e 'write (the clipboard as «class PNGf») to theFile' -e 'end try' ]]
.. [[-e 'close access theFile']],
.. [[-e 'close access theFile' -e 'do shell script "cat %s %s> %s"']],
file_path,
file_path,
process_cmd,
file_path
)
local _, exit_code = util.execute(command)
Expand Down
10 changes: 6 additions & 4 deletions lua/img-clip/paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ M.paste_image_from_clipboard = function()
return false
end

local output, exit_code = fs.process_image(file_path)
if exit_code ~= 0 then
util.warn("Could not process image.", true)
util.warn("Output: " .. output, true)
if util.has("wsl") then
local output, exit_code = fs.process_image(file_path)
if exit_code ~= 0 then
util.warn("Could not process image.", true)
util.warn("Output: " .. output, true)
end
end

if not markup.insert_markup(file_path) then
Expand Down

0 comments on commit 841d8d9

Please sign in to comment.