Skip to content

Commit

Permalink
fix: escape special characters in process_cmd (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes authored Mar 25, 2024
1 parent 21b2c9c commit ba847e5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lua/img-clip/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,20 @@ M.save_image = function(file_path)

-- Linux (X11)
if cmd == "xclip" then
local command = string.format('xclip -selection clipboard -o -t image/png %s> "%s"', process_cmd, file_path)
local command =
string.format('xclip -selection clipboard -o -t image/png %s> "%s"', process_cmd:gsub("%%", "%%%%"), 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> "%s"', process_cmd, file_path)
local command = string.format('wl-paste --type image/png %s> "%s"', process_cmd:gsub("%%", "%%%%"), 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> "%s"', process_cmd, file_path)
local command = string.format('pngpaste - %s> "%s"', process_cmd:gsub("%%", "%%%%"), file_path)
local _, exit_code = util.execute(command)
return exit_code == 0

Expand All @@ -103,7 +104,7 @@ M.save_image = function(file_path)
.. [[-e 'close access theFile' -e 'do shell script "cat %s %s> %s"']],
file_path,
file_path,
process_cmd,
process_cmd:gsub("%%", "%%%%"),
file_path
)
local _, exit_code = util.execute(command)
Expand Down
4 changes: 2 additions & 2 deletions lua/img-clip/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ M.process_image = function(file_path, opts)

-- process image
local output, exit_code =
util.execute(string.format("cat '%s' | %s > '%s'", file_path, process_cmd, tmp_file_path), true)
util.execute(string.format("cat '%s' | %s > '%s'", file_path, process_cmd:gsub("%%", "%%%%"), tmp_file_path), true)
if exit_code == 0 then
M.copy_file(tmp_file_path, file_path)
end
Expand Down Expand Up @@ -207,7 +207,7 @@ M.get_base64_encoded_image = function(file_path)

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

0 comments on commit ba847e5

Please sign in to comment.