Skip to content

feat: add trashing #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ require'nvim-tree'.setup {
-- list of mappings to set on the tree manually
list = {}
}
}
},

-- trashing is currently a UNIX only feature
trash = {
-- the command used to trash files, "trash" is the default which needs trash-cli to be installed
cmd = "trash",
-- if false trashing won't require y/n confirmation
require_confirm = true
},
}
```

Expand Down Expand Up @@ -209,6 +217,7 @@ highlight NvimTreeFolderIcon guibg=blue
- type `gy` will copy absolute path to system clipboard
- type `p` to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation)
- type `d` to delete a file (will prompt for confirmation)
- type `D` to trash a file (configured in setup())
- type `]c` to go to next git item
- type `[c` to go to prev git item
- type `-` to navigate up to the parent directory of the current file/directory
Expand Down Expand Up @@ -260,6 +269,7 @@ local list = {
{ key = "R", cb = tree_cb("refresh") },
{ key = "a", cb = tree_cb("create") },
{ key = "d", cb = tree_cb("remove") },
{ key = "D", cb = tree_cb("trash") },
{ key = "r", cb = tree_cb("rename") },
{ key = "<C-r>", cb = tree_cb("full_rename") },
{ key = "x", cb = tree_cb("cut") },
Expand Down
1 change: 1 addition & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ Defaults to:
{ key = "R", cb = tree_cb("refresh") },
{ key = "a", cb = tree_cb("create") },
{ key = "d", cb = tree_cb("remove") },
{ key = "D", cb = tree_cb("trash") },
{ key = "r", cb = tree_cb("rename") },
{ key = "<C-r>", cb = tree_cb("full_rename") },
{ key = "x", cb = tree_cb("cut") },
Expand Down
31 changes: 31 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ local keypress_funcs = {
)
luv.unref(process.handle)
end,
trash = function(node)
if _config.is_unix then
if _config.trash.cmd == nil then _config.trash.cmd = 'trash' end
if _config.trash.require_confirm == nil then _config.trash.require_confirm = true end
else
print('trash is currently a UNIX only feature!')
end

local function get_user_input_char()
local c = vim.fn.getchar()
return vim.fn.nr2char(c)
end

if (node) then
local is_confirmed = true
if _config.trash.require_confirm then
is_confirmed = false
print("Trash "..node.name.." ? y/n")
if get_user_input_char():match('^y') then is_confirmed = true end
end

if is_confirmed then
vim.fn.jobstart(_config.trash.cmd.." "..node.absolute_path, {
detach = true,
on_exit = function(_job_id, _data, _event) lib.refresh_tree() end,
})
end
end

end,
}

function M.on_keypress(mode)
Expand Down Expand Up @@ -446,6 +476,7 @@ function M.setup(conf)
_config.system_open = opts.system_open
_config.open_on_setup = opts.open_on_setup
_config.ignore_ft_on_setup = opts.ignore_ft_on_setup
_config.trash = opts.trash or {}
if type(opts.update_to_buf_dir) == "boolean" then
utils.echo_warning("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir")
_config.update_to_buf_dir = {
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ M.View = {
{ key = "R", cb = M.nvim_tree_callback("refresh") },
{ key = "a", cb = M.nvim_tree_callback("create") },
{ key = "d", cb = M.nvim_tree_callback("remove") },
{ key = "D", cb = M.nvim_tree_callback("trash") },
{ key = "r", cb = M.nvim_tree_callback("rename") },
{ key = "<C-r>", cb = M.nvim_tree_callback("full_rename") },
{ key = "x", cb = M.nvim_tree_callback("cut") },
Expand Down