From 434e74ad30f02bf62bc4b2ba3a3248efb368e280 Mon Sep 17 00:00:00 2001 From: fz0x1 Date: Mon, 26 Feb 2024 14:28:46 -0500 Subject: [PATCH] feat: add xsel for pasting images * https://github.com/renerocksai/telekasten.nvim/issues/131 - fixed a problem with `xclip` in linux (added `xsel`) * removed debug info * xclip --- lua/telekasten.lua | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 3a298de..729d795 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -372,7 +372,22 @@ local function imgFromClipboard() end local get_paste_command - if vim.fn.executable("xclip") == 1 then + if vim.fn.executable("xsel") == 1 then + get_paste_command = function(dir, filename) + local _image_path = vim.fn.system("xsel --clipboard --output") + local image_path = _image_path:gsub("file://", "") + if + vim.fn + .system("file --mime-type -b " .. image_path) + :gsub("%s+", "") + == "image/png" + then + return "cp " .. image_path .. " " .. dir .. "/" .. filename + else + return "" + end + end + elseif vim.fn.executable("xclip") == 1 then get_paste_command = function(dir, filename) return "xclip -selection clipboard -t image/png -o > " .. dir @@ -397,7 +412,7 @@ local function imgFromClipboard() return end - -- TODO: check `xclip -selection clipboard -t TARGETS -o` for the occurence of `image/png` + -- TODO: check `xclip -selection clipboard -t TARGETS -o` for the occurrence of `image/png` -- using plenary.job::new():sync() with on_stdout(_, data) unfortunately did some weird ASCII translation on the -- data, so the PNGs were invalid. It seems like 0d 0a and single 0a bytes were stripped by the plenary job: @@ -1578,7 +1593,7 @@ local function RenameNote() local fname = M.Cfg.home .. "/" .. newname .. M.Cfg.extension local fexists = fileutils.file_exists(fname) if fexists then - tkutils.print_error("File alreay exists. Renaming abandonned") + tkutils.print_error("File already exists. Renaming abandoned") return end