Skip to content

Commit

Permalink
Fix crashes when deleting to trash
Browse files Browse the repository at this point in the history
The execute() method of the trash command (in ranger/config/commands.py)
used to pass a list of file paths (as strings) to fm.execute_file().

The documentation of the execute_file() method states that the 'files'
parameter must not be strings:
    [...]
    files: a list of file objects (not strings!)
    [...]

So I changed 'files' to be a list of File objects and that seems to fix the
issue.

Fixes #1798
  • Loading branch information
5hir0kur0 committed Mar 5, 2020
1 parent 081e731 commit f65e6f0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ranger/config/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,28 +722,31 @@ class trash(Command):
def execute(self):
import shlex
from functools import partial
from ranger.container.file import File

def is_directory_with_files(path):
return os.path.isdir(path) and not os.path.islink(path) and len(os.listdir(path)) > 0

if self.rest(1):
files = shlex.split(self.rest(1))
many_files = (len(files) > 1 or is_directory_with_files(files[0]))
file_names = shlex.split(self.rest(1))
files = [File(name) for name in file_names]
many_files = (len(files) > 1 or is_directory_with_files(files[0].path))
else:
cwd = self.fm.thisdir
tfile = self.fm.thisfile
if not cwd or not tfile:
self.fm.notify("Error: no file selected for deletion!", bad=True)
return

files = self.fm.thistab.get_selection()
# relative_path used for a user-friendly output in the confirmation.
files = [f.relative_path for f in self.fm.thistab.get_selection()]
file_names = [f.relative_path for f in files]
many_files = (cwd.marked_items or is_directory_with_files(tfile.path))

confirm = self.fm.settings.confirm_on_delete
if confirm != 'never' and (confirm != 'multiple' or many_files):
self.fm.ui.console.ask(
"Confirm deletion of: %s (y/N)" % ', '.join(files),
"Confirm deletion of: %s (y/N)" % ', '.join(file_names),
partial(self._question_callback, files),
('n', 'N', 'y', 'Y'),
)
Expand Down

2 comments on commit f65e6f0

@Henry-Ta
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix doesn't work. Still not be able to trash file by using command :trash. Still the same error , "str object has no attribute 'path' "

@toonn
Copy link
Member

@toonn toonn commented on f65e6f0 Apr 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only the tip of the PR. You need all these commits, https://github.com/ranger/ranger/pull/1871/commits.

Please sign in to comment.