feat: Add option to choose clipboard program (#322)

* Refactor and add option to choose clipboard program

* Warn user early when clipboard program specified is not executable

closes #320
This commit is contained in:
andy941
2024-04-17 08:16:16 +01:00
committed by GitHub
parent adbe1ea3c2
commit 3ef86c924b
2 changed files with 57 additions and 35 deletions

View File

@@ -181,6 +181,8 @@ telekasten.setup({opts})
} }
}, },
-- Specify a clipboard program to use
clipboard_program = "", -- xsel, xclip, wl-paste, osascript
} }
< <

View File

@@ -163,6 +163,9 @@ local function defaultConfig(home)
follow_url_fallback = nil, follow_url_fallback = nil,
-- Enable creation new notes with Ctrl-n when finding notes -- Enable creation new notes with Ctrl-n when finding notes
enable_create_new = true, enable_create_new = true,
-- Specify a clipboard program to use
clipboard_program = "", -- xsel, xclip, wl-paste, osascript
} }
M.Cfg = cfg M.Cfg = cfg
M.note_type_templates = { M.note_type_templates = {
@@ -371,9 +374,8 @@ local function imgFromClipboard()
return return
end end
local get_paste_command local paste_command = {}
if vim.fn.executable("xsel") == 1 then paste_command["xsel"] = function(dir, filename)
get_paste_command = function(dir, filename)
local _image_path = vim.fn.system("xsel --clipboard --output") local _image_path = vim.fn.system("xsel --clipboard --output")
local image_path = _image_path:gsub("file://", "") local image_path = _image_path:gsub("file://", "")
if if
@@ -387,19 +389,13 @@ local function imgFromClipboard()
return "" return ""
end end
end end
elseif vim.fn.executable("xclip") == 1 then paste_command["xclip"] = function(dir, filename)
get_paste_command = function(dir, filename)
return "xclip -selection clipboard -t image/png -o > " return "xclip -selection clipboard -t image/png -o > "
.. dir .. dir
.. "/" .. "/"
.. filename .. filename
end end
elseif vim.fn.executable("wl-paste") == 1 then paste_command["osascript"] = function(dir, filename)
get_paste_command = function(dir, filename)
return "wl-paste -n -t image/png > " .. dir .. "/" .. filename
end
elseif vim.fn.executable("osascript") == 1 then
get_paste_command = function(dir, filename)
return string.format( return string.format(
'osascript -e "tell application \\"System Events\\" to write (the clipboard as «class PNGf») to ' 'osascript -e "tell application \\"System Events\\" to write (the clipboard as «class PNGf») to '
.. '(make new file at folder \\"%s\\" with properties {name:\\"%s\\"})"', .. '(make new file at folder \\"%s\\" with properties {name:\\"%s\\"})"',
@@ -407,9 +403,33 @@ local function imgFromClipboard()
filename filename
) )
end end
paste_command["wl-paste"] = function(dir, filename)
return "wl-paste -n -t image/png > " .. dir .. "/" .. filename
end
local get_paste_command
if paste_command[M.Cfg.clipboard_program] ~= nil then
if vim.fn.executable(M.Cfg.clipboard_program) ~= 1 then
vim.api.nvim_err_write(
"The clipboard program specified [`"
.. M.cfg.clipboard_program
.. "`] is not executable or not in your $PATH\n"
)
end
get_paste_command = paste_command[M.Cfg.clipboard_program]
elseif vim.fn.executable("xsel") == 1 then
get_paste_command = paste_command["xsel"]
elseif vim.fn.executable("xclip") == 1 then
get_paste_command = paste_command["xclip"]
elseif vim.fn.executable("wl-paste") == 1 then
get_paste_command = paste_command["wl-paste"]
elseif vim.fn.executable("osascript") == 1 then
get_paste_command = paste_command["osascript"]
else else
vim.api.nvim_err_write("No xclip installed!\n") vim.api.nvim_err_write(
return "No clipboard programs found!\nChecked executables: xsel, xclip, wl-paste, osascript\n"
)
end end
-- TODO: check `xclip -selection clipboard -t TARGETS -o` for the occurrence of `image/png` -- TODO: check `xclip -selection clipboard -t TARGETS -o` for the occurrence of `image/png`