refactor: `trashFile` uses new nvim API

remotes/origin/HEAD
Chris Grieser 2024-05-20 11:37:24 +02:00
parent 508547531f
commit cd02e898eb
1 changed files with 19 additions and 29 deletions

View File

@ -224,40 +224,30 @@ end
---@param opts? { trashCmd: string } ---@param opts? { trashCmd: string }
function M.trashFile(opts) function M.trashFile(opts)
if opts == nil then opts = {} end local userCmd = opts and opts.trashCmd
local defaultTrashCmds = { local defaultCmd
macos = "trash", -- not installed by default if fn.has("mac") == 1 then defaultCmd = "trash" end
windows = "trash", -- not installed by default if fn.has("linux") == 1 then defaultCmd = "trash" end
linux = "gio trash", if fn.has("win32") == 1 then defaultCmd = "gio trash" end
} local trashCmd = userCmd or defaultCmd
assert(defaultCmd, "Unknown operating system & no custom trashCmd provided.")
cmd("silent! update")
local system
if fn.has("mac") == 1 then system = "macos" end
if fn.has("linux") == 1 then system = "linux" end
if fn.has("win32") == 1 then system = "windows" end
local trashCmd = opts.trashCmd or defaultTrashCmds[system]
-- Use a trash command -- Use a trash command
local trashArgs = vim.split(trashCmd, " ") local trashArgs = vim.split(trashCmd, " ")
local oldFilePath = vim.api.nvim_buf_get_name(0) local oldFilePath = vim.api.nvim_buf_get_name(0)
table.insert(trashArgs, oldFilePath) table.insert(trashArgs, oldFilePath)
local errMsg = "" cmd("silent! update")
vim.fn.jobstart(trashArgs, { vim.system(trashArgs, { detach = true }, function(out)
detach = true,
on_stderr = function(_, data) errMsg = errMsg .. (data and table.concat(data, " ")) end,
on_exit = function(_, rc)
local oldName = vim.fs.basename(oldFilePath) local oldName = vim.fs.basename(oldFilePath)
if rc == 0 then if out.code == 0 then
u.bwipeout() u.bwipeout()
u.notify(("%q deleted"):format(oldName)) u.notify(("%q deleted"):format(oldName))
else else
u.notify(("Trashing %q failed: " .. errMsg):format(oldName), "error") local outmsg = out.stdout .. out.stderr
u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error")
end end
end, end)
})
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------