0

I copied this snipped from the arch wiki. But instead of extracting the selected files, it extracts the yanked files. So I'd like to know one of this two things:

  1. How can I make extract_here using the selected files instead of the yanked files?

  2. How can I yank the selected files automatically from commands.py?

    class extract_here(Command):
    def execute(self):
        """ Extract yanked files to current directory.
            HOW TO USE: press yy to yank, then call the method
                        with a shortcut or the command itself."""
        copied_files = tuple(self.fm.copy_buffer)
    
        if not copied_files:
            return
    
        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()
    
        one_file = copied_files[0]
        cwd = self.fm.thisdir
        original_path = cwd.path
        au_flags = ['-X', cwd.path]
        au_flags += self.line.split()[1:]
        au_flags += ['-e']
    
        self.fm.copy_buffer.clear()
        self.fm.cut_buffer = False
        if len(copied_files) == 1:
            descr = "extracting: " + os.path.basename(one_file.path)
        else:
            descr = "extracting files from: " + os.path.basename(
                one_file.dirname)
        obj = CommandLoader(args=['aunpack'] + au_flags
                            + [f.path for f in copied_files], descr=descr,
                            read=True)
    
        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
        ```
    
0

Solution:

class extract_here(Command):
def execute(self):
    """ Extract selected files to current directory."""
    import os
    from ranger.core.loader import CommandLoader
    cwd = self.fm.thisdir
    copied_files = tuple(cwd.get_selection())

    def refresh(_):
        cwd = self.fm.get_directory(original_path)
        cwd.load_content()

    one_file = copied_files[0]
    cwd = self.fm.thisdir
    original_path = cwd.path
    au_flags = ['-X', cwd.path]
    au_flags += self.line.split()[1:]
    au_flags += ['-e']

    self.fm.copy_buffer.clear()
    self.fm.cut_buffer = False
    if len(copied_files) == 1:
        descr = "extracting: " + os.path.basename(one_file.path)
    else:
        descr = "extracting files from: " + os.path.basename(
            one_file.dirname)
    obj = CommandLoader(args=['aunpack'] + au_flags
                        + [f.path for f in copied_files], descr=descr,
                        read=True)

    obj.signal_bind('after', refresh)
    self.fm.loader.add(obj)

Edited in the arch wiki, hopefully no one will ever have this problem again.

| improve this answer | |
  • Not sure what you mean by 'problem'. The previous version was able to extract into any directory while your version is not. – laktak Jul 18 at 19:34
  • The name of the class is 'extract_here', not 'extract_into'. – Adrian Lopez Jul 20 at 14:34
  • Yes - you yank the files you want to extract, go to the desired directory and then 'extract them here'. – laktak Jul 21 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.