From 359c492d0da7804f4ccb5bc12260e8101e375402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Harnes?= Date: Mon, 18 Mar 2024 11:22:58 +0100 Subject: [PATCH] feat: preprocess before embedding as base64 --- lua/img-clip/clipboard.lua | 16 ++++++++++++---- lua/img-clip/config.lua | 1 + lua/img-clip/fs.lua | 6 +++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lua/img-clip/clipboard.lua b/lua/img-clip/clipboard.lua index 112f4b9..070178d 100644 --- a/lua/img-clip/clipboard.lua +++ b/lua/img-clip/clipboard.lua @@ -1,4 +1,5 @@ local util = require("img-clip.util") +local config = require("img-clip.config") local M = {} @@ -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 @@ -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 diff --git a/lua/img-clip/config.lua b/lua/img-clip/config.lua index 40113df..95fcec7 100644 --- a/lua/img-clip/config.lua +++ b/lua/img-clip/config.lua @@ -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 diff --git a/lua/img-clip/fs.lua b/lua/img-clip/fs.lua index 24fc900..8ac3774 100644 --- a/lua/img-clip/fs.lua +++ b/lua/img-clip/fs.lua @@ -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 @@ -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