Skip to content

Commit

Permalink
feat: preprocess before embedding as base64
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Mar 18, 2024
1 parent 5a8efb3 commit 359c492
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lua/img-clip/clipboard.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local util = require("img-clip.util")
local config = require("img-clip.config")

local M = {}

Expand Down Expand Up @@ -162,24 +163,29 @@ end

M.get_base64_encoded_image = function()
local cmd = M.get_clip_cmd()
local preprocess_cmd = config.get_opt("preprocess_cmd")
if preprocess_cmd ~= "" then
preprocess_cmd = "| " .. preprocess_cmd .. " "
end

-- Linux (X11)
if cmd == "xclip" then
local output, exit_code = util.execute("xclip -selection clipboard -o -t image/png | base64 | tr -d '\n'")
local output, exit_code =
util.execute("xclip -selection clipboard -o -t image/png " .. preprocess_cmd .. "| base64 | tr -d '\n'")
if exit_code == 0 then
return output
end

-- Linux (Wayland)
elseif cmd == "wl-paste" then
local output, exit_code = util.execute("wl-paste --type image/png | base64 | tr -d '\n'")
local output, exit_code = util.execute("wl-paste --type image/png " .. preprocess_cmd .. "| base64 | tr -d '\n'")
if exit_code == 0 then
return output
end

-- MacOS (pngpaste)
elseif cmd == "pngpaste" then
local output, exit_code = util.execute("pngpaste - | base64 | tr -d '\n'")
local output, exit_code = util.execute("pngpaste - " .. preprocess_cmd .. "| base64 | tr -d '\n'")
if exit_code == 0 then
return output
end
Expand All @@ -189,7 +195,9 @@ M.get_base64_encoded_image = function()
local output, exit_code = util.execute(
[[osascript -e 'set theFile to (open for access POSIX file "/tmp/image.png" with write permission)' ]]
.. [[-e 'try' -e 'write (the clipboard as «class PNGf») to theFile' -e 'end try' -e 'close access theFile'; ]]
.. [[cat /tmp/image.png | base64 | tr -d "\n" ]]
.. [[/tmp/image.png ]]
.. preprocess_cmd
.. [[ | base64 | tr -d "\n" ]]
)
if exit_code == 0 then
return output
Expand Down
1 change: 1 addition & 0 deletions lua/img-clip/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local defaults = {
default = {
dir_path = "assets", -- directory path to save images to, can be relative (cwd or current file) or absolute
file_name = "%Y-%m-%d-%H-%M-%S", -- file name format (see lua.org/pil/22.1.html)
preprocess_cmd = "", -- cli command to process image before saving/embedding (expects image from stdin, writes image to stdout)
url_encode_path = false, -- encode spaces and special characters in file path
use_absolute_path = false, -- expands dir_path to an absolute path
relative_to_current_file = false, -- make dir_path relative to current file rather than the cwd
Expand Down
6 changes: 5 additions & 1 deletion lua/img-clip/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ end
---@return string | nil
M.get_base64_encoded_image = function(file_path)
local cmd = clipoard.get_clip_cmd()
local preprocess_cmd = config.get_opt("preprocess_cmd")
if preprocess_cmd ~= "" then
preprocess_cmd = "| " .. preprocess_cmd .. " "
end

-- Windows
if cmd == "powershell.exe" then
Expand All @@ -173,7 +177,7 @@ M.get_base64_encoded_image = function(file_path)

-- Linux/MacOS
else
local command = string.format("base64 '%s' | tr -d '\n'", file_path)
local command = string.format("cat '%s' %s| base64 | tr -d '\n'", file_path, preprocess_cmd)
local output, exit_code = util.execute(command)
if exit_code == 0 then
return output
Expand Down

0 comments on commit 359c492

Please sign in to comment.