Ranger:如何从commands.py 中提取选定的文件?

Ranger:如何从commands.py 中提取选定的文件?

我从 arch wiki 复制了这个片段。但它不是提取选定的文件,而是提取拉出的文件。所以我想知道这两件事:

  1. 如何使用选定的文件而不是提取的文件来制作 extract_here ?

  2. 如何从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)
        ```
    

答案1

解决方案:

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)

在 arch wiki 中编辑,希望没有人会再遇到这个问题。

相关内容