* Implement turning off y/n prompt for file deletion * Refactor different prompt configs to single ui table * Remove unused fields * add ui.confirm doc, format/tidy * trash.require_confirm -> ui.confirm.trash * Fix nil value trash field Co-authored-by: Alexander Courtis <alex@courtis.org>
112 lines
2.9 KiB
Lua
112 lines
2.9 KiB
Lua
local lib = require "nvim-tree.lib"
|
|
local notify = require "nvim-tree.notify"
|
|
|
|
local M = {}
|
|
|
|
local utils = require "nvim-tree.utils"
|
|
local events = require "nvim-tree.events"
|
|
|
|
local function clear_buffer(absolute_path)
|
|
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
|
|
for _, buf in pairs(bufs) do
|
|
if buf.name == absolute_path then
|
|
if buf.hidden == 0 and #bufs > 1 then
|
|
local winnr = vim.api.nvim_get_current_win()
|
|
vim.api.nvim_set_current_win(buf.windows[1])
|
|
vim.cmd ":bn"
|
|
vim.api.nvim_set_current_win(winnr)
|
|
end
|
|
vim.api.nvim_buf_delete(buf.bufnr, {})
|
|
return
|
|
end
|
|
end
|
|
end
|
|
|
|
function M.fn(node)
|
|
if node.name == ".." then
|
|
return
|
|
end
|
|
|
|
-- configs
|
|
if utils.is_unix then
|
|
if M.config.trash.cmd == nil then
|
|
M.config.trash.cmd = "trash"
|
|
end
|
|
if M.config.ui.confirm.trash == nil then
|
|
M.config.ui.confirm.trash = true
|
|
end
|
|
else
|
|
notify.warn "Trash is currently a UNIX only feature!"
|
|
return
|
|
end
|
|
|
|
local binary = M.config.trash.cmd:gsub(" .*$", "")
|
|
if vim.fn.executable(binary) == 0 then
|
|
notify.warn(binary .. " is not executable.")
|
|
return
|
|
end
|
|
|
|
local err_msg = ""
|
|
local function on_stderr(_, data)
|
|
err_msg = err_msg .. (data and table.concat(data, " "))
|
|
end
|
|
|
|
-- trashes a path (file or folder)
|
|
local function trash_path(on_exit)
|
|
vim.fn.jobstart(M.config.trash.cmd .. ' "' .. node.absolute_path .. '"', {
|
|
detach = true,
|
|
on_exit = on_exit,
|
|
on_stderr = on_stderr,
|
|
})
|
|
end
|
|
|
|
local function do_trash()
|
|
if node.nodes ~= nil and not node.link_to then
|
|
trash_path(function(_, rc)
|
|
if rc ~= 0 then
|
|
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
|
|
return
|
|
end
|
|
events._dispatch_folder_removed(node.absolute_path)
|
|
if M.enable_reload then
|
|
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
|
|
end
|
|
end)
|
|
else
|
|
trash_path(function(_, rc)
|
|
if rc ~= 0 then
|
|
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
|
|
return
|
|
end
|
|
events._dispatch_file_removed(node.absolute_path)
|
|
clear_buffer(node.absolute_path)
|
|
if M.enable_reload then
|
|
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
if M.config.ui.confirm.trash then
|
|
local prompt_select = "Trash " .. node.name .. " ?"
|
|
local prompt_input = prompt_select .. " y/n: "
|
|
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
|
|
utils.clear_prompt()
|
|
if item_short == "y" then
|
|
do_trash()
|
|
end
|
|
end)
|
|
else
|
|
do_trash()
|
|
end
|
|
end
|
|
|
|
function M.setup(opts)
|
|
M.config = {}
|
|
M.config.ui = opts.ui or {}
|
|
M.config.trash = opts.trash or {}
|
|
M.enable_reload = not opts.filesystem_watchers.enable
|
|
end
|
|
|
|
return M
|