Skip to content
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

An option to trash files instead of removing them #169

Closed
paveloom opened this issue Aug 6, 2022 · 4 comments · May be fixed by #196
Closed

An option to trash files instead of removing them #169

paveloom opened this issue Aug 6, 2022 · 4 comments · May be fixed by #196
Labels
enhancement New feature or request

Comments

@paveloom
Copy link

paveloom commented Aug 6, 2022

Is your feature request related to a problem? Please describe.
I would like to put files in the trash instead of removing them.

Describe the solution you'd like
A generic way to override the remove action (so I can change it to the trash command).

Describe alternatives you've considered
A configuration flag.

@paveloom paveloom added the enhancement New feature or request label Aug 6, 2022
@fdschmidt93
Copy link
Member

fdschmidt93 commented Aug 8, 2022

I briefly looked into it and it's very OS-dependent (as eg there is no libuv function to implement this). Consequently, I wouldn't support this from telescope-file-browser.nvim.

Instead, you can very easily implement your own solution if you know a little (pretty much only a tiny) bit of lua. Have a look at

--- Move multi-selected files or folders to current directory in |telescope-file-browser.picker.file_browser|.<br>
--- Note: Performs a blocking synchronized file-system operation.
---@param prompt_bufnr number: The prompt bufnr
fb_actions.move = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
local finder = current_picker.finder
local selections = fb_utils.get_selected_files(prompt_bufnr, false)
if vim.tbl_isempty(selections) then
fb_utils.notify("actions.move", { msg = "No selection to be moved!", level = "WARN", quiet = finder.quiet })
return
end
local target_dir = get_target_dir(finder)
local moved = {}
local skipped = {}
for idx, selection in ipairs(selections) do
local filename = selection.filename:sub(#selection:parent().filename + 2)
local new_path = Path:new { target_dir, filename }
if new_path:exists() then
table.insert(skipped, filename)
else
selection:rename {
new_name = new_path.filename,
}
table.insert(moved, filename)
if idx == 1 and #selections == 1 then
fb_utils.selection_callback(current_picker, new_path:absolute())
end
end
end

and then implement a function ala

require "telescope".setup {
 extensions = {
   file_browser = {
     mappings = {
       ["i"] = {
         -- move selection(s) to where your trash bin is
         ["KEY"] = function(prompt_bufnr) ... end 
       }
     }
   }
 }
}

The core idea is that rename a file/folder is essentially the same thing as moving it to the folder, that is, you change the original folder prefix to the trash prefix.

@fdschmidt93
Copy link
Member

If there is a clean proposal and PR to implement this, I'd very much consider it, but I'm generally quite happy with current scope of filesys operations of telescope-file-browser.nvim, especially considering how straightforward anyone can implement the original request themselves balanced with implementation and maintenance overhead, as I only use linux.

For the time being, closing.

@paveloom
Copy link
Author

paveloom commented Aug 9, 2022

Thanks for the hints. I just rewrote my config in Lua the other day, had no experience with overwriting Telescope actions.

There are also issues with duplicates and setting up original folders and deletion timestamps, so it's not as straightforward as just moving files.

I ended up with the following implementation:

require("telescope").setup({
  extensions = {
    file_browser = {
      -- <...>
      mappings = {
        ["i"] = {
          -- Trash files instead of deleting them
          ["<A-d>"] = function(prompt_bufnr)
            local action_state = require("telescope.actions.state")
            local call = require("config.utils").call
            local fb_utils = require("telescope._extensions.file_browser.utils")
            -- Get the finder
            local current_picker = action_state.get_current_picker(prompt_bufnr)
            local finder = current_picker.finder
            -- Get the selections
            local selections = fb_utils.get_selected_files(prompt_bufnr, false)
            if vim.tbl_isempty(selections) then
              fb_utils.notify("actions.trash",
                { msg = "No selection to be trashed!", level = "WARN", quiet = finder.quiet })
              return
            end
            -- Trash the selected files
            local trashed = {}
            for _, selection in ipairs(selections) do
              local filename = selection.filename:sub(#selection:parent().filename + 2)
              -- `trash-put` is from the `trash-cli` package
              if call({ "trash-put", "--", selection:absolute() }) then
                table.insert(trashed, filename)
              end
            end
            -- Notify about operations
            local message = ""
            if not vim.tbl_isempty(trashed) then
              message = message .. "Trashed: " .. table.concat(trashed, ", ")
            end
            fb_utils.notify("actions.trash", { msg = message, level = "INFO", quiet = finder.quiet })
            -- Reset multi selection
            current_picker:refresh(current_picker.finder, { reset_prompt = true })
          end,
        }
      }
    },
  }
})

where require("config.utils").call is

-- Do a system call and check the status code
function call(cmd)
    fn.system(cmd)
    return vim.v.shell_error == 0
end

@fdschmidt93
Copy link
Member

fdschmidt93 commented Aug 9, 2022

I'm very glad to hear you've managed to whip up a solution that works for you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants