How to implement custom trash action? #2333
-
Hi, there's a node.js implemented trash-cli which works good on Windows.
I have used the nodejs one with neo-tree.nvim with below config: local function trash_bin(state)
local inputs = require("neo-tree.ui.inputs")
local path = state.tree:get_node().path
local msg = "Are you sure you want to move '"
.. vim.fn.fnamemodify(vim.fn.fnameescape(path), ":~:.")
.. "' to trash bin?"
inputs.confirm(msg, function(confirmed)
if not confirmed then
return
end
vim.fn.system({ "trash", vim.fn.fnameescape(path) })
require("neo-tree.sources.manager").refresh(state.name)
end)
end
require('neo-tree').setup({
window = {
mappings = { d = vim.fn.executable('trash') > 0 and trash_bin or "delete",
}
}) I want to know how should I config custom action to support this with nvim-tree? I have read the doc, the only custom action I found is action_cb, not sure if I missed something. And by default the Please help me with this, thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Oh, I suddenly found the custom action here: https://github.com/nvim-tree/nvim-tree.lua/blob/master/doc/nvim-tree-lua.txt#L1930C32-L1930C47 In case someone has similar issues, I post my config here: local on_attach = function(bufnr)
local api = require("nvim-tree.api")
local function trash_put()
local node = require("nvim-tree.api").tree.get_node_under_cursor()
local function trash_impl(on_exit, on_stderr)
local j =
vim.fn.jobstart("trash" .. ' "' .. node.absolute_path .. '"', {
on_exit = on_exit,
on_stderr = on_stderr,
})
vim.fn.jobwait({ j })
end
local err_msg = ""
local function on_trash_error(_, data)
if type(data) == "table" then
err_msg = err_msg .. table.concat(data, " ")
end
end
local function on_trash_exit(_, rc)
if rc ~= 0 then
require("nvim-tree.notify").warn(
"trash failed: " .. err_msg .. ""
)
end
end
local prompt_select = "Trash " .. node.name .. " ?"
local prompt_input = prompt_select .. " y/n: "
require("nvim-tree.lib").prompt(
prompt_input,
prompt_select,
{ "y", "n" },
{ "Yes", "No" },
function(item_short)
require("nvim-tree.utils").clear_prompt()
if item_short == "y" then
trash_impl(on_trash_exit, on_trash_error)
end
end
)
end
if vim.fn.executable("trash") > 0 then
vim.keymap.set("n", "d", vim.fn.has('win32') > 0 and trash_put or api.fs.trash, opts("Trash Put"))
end
end
require('nvim-tree').setup({
on_attach = on_attach
}) The implementation is almost a fork of actions.fs.trash. |
Beta Was this translation helpful? Give feedback.
Oh, I suddenly found the custom action here: https://github.com/nvim-tree/nvim-tree.lua/blob/master/doc/nvim-tree-lua.txt#L1930C32-L1930C47
In case someone has similar issues, I post my config here: