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

@ -222,42 +222,32 @@ function M.chmodx()
cmd.edit() cmd.edit()
end 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, local oldName = vim.fs.basename(oldFilePath)
on_stderr = function(_, data) errMsg = errMsg .. (data and table.concat(data, " ")) end, if out.code == 0 then
on_exit = function(_, rc) u.bwipeout()
local oldName = vim.fs.basename(oldFilePath) u.notify(("%q deleted"):format(oldName))
if rc == 0 then else
u.bwipeout() local outmsg = out.stdout .. out.stderr
u.notify(("%q deleted"):format(oldName)) u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error")
else end
u.notify(("Trashing %q failed: " .. errMsg):format(oldName), "error") end)
end
end,
})
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------