THEAARBS ranger commands

Raw

# This is a sample commands.py.  You can add your own commands here.
#
# Please refer to commands_full.py for all the default commands and a complete
# documentation.  Do NOT add them all here, or you may end up with defunct
# commands when upgrading ranger.

# You always need to import ranger.api.commands here to get the Command class: from ranger.api.commands import *

# A simple command for demonstration purposes follows. #——————————————————————————

# You can import any python module as needed. import os

# Any class that is a subclass of "Command" will be integrated into ranger as a # command. Try typing ":my_edit<ENTER>" in ranger! class my_edit(Command): # The so-called doc-string of the class will be visible in the built-in # help that is accessible by typing "?c" inside ranger. """:my_edit <filename> A sample command for demonstration purposes that opens a file in an editor. """

<span style="color:#75715e"># The execute method is called when you run this command in ranger.</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">execute</span>(self):
    <span style="color:#75715e"># self.arg(1) is the first (space-separated) argument to the function.</span>
    <span style="color:#75715e"># This way you can write &#34;:my_edit somefilename&lt;ENTER&gt;&#34;.</span>
    <span style="color:#66d9ef">if</span> self<span style="color:#f92672">.</span>arg(<span style="color:#ae81ff">1</span>):
        <span style="color:#75715e"># self.rest(1) contains self.arg(1) and everything that follows</span>
        target_filename <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>rest(<span style="color:#ae81ff">1</span>)
    <span style="color:#66d9ef">else</span>:
        <span style="color:#75715e"># self.fm is a ranger.core.filemanager.FileManager object and gives</span>
        <span style="color:#75715e"># you access to internals of ranger.</span>
        <span style="color:#75715e"># self.fm.thisfile is a ranger.container.file.File object and is a</span>
        <span style="color:#75715e"># reference to the currently selected file.</span>
        target_filename <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>thisfile<span style="color:#f92672">.</span>path

    <span style="color:#75715e"># This is a generic function to print text in ranger.  </span>
    self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>notify(<span style="color:#e6db74">&#34;Let&#39;s edit the file &#34;</span> <span style="color:#f92672">+</span> target_filename <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;!&#34;</span>)

    <span style="color:#75715e"># Using bad=True in fm.notify allows you to print error messages:</span>
    <span style="color:#66d9ef">if</span> <span style="color:#f92672">not</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>exists(target_filename):
        self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>notify(<span style="color:#e6db74">&#34;The given file does not exist!&#34;</span>, bad<span style="color:#f92672">=</span>True)
        <span style="color:#66d9ef">return</span>

    <span style="color:#75715e"># This executes a function from ranger.core.acitons, a module with a</span>
    <span style="color:#75715e"># variety of subroutines that can help you construct commands.</span>
    <span style="color:#75715e"># Check out the source, or run &#34;pydoc ranger.core.actions&#34; for a list.</span>
    self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>edit_file(target_filename)

<span style="color:#75715e"># The tab method is called when you press tab, and should return a list of</span>
<span style="color:#75715e"># suggestions that the user will tab through.</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">tab</span>(self):
    <span style="color:#75715e"># This is a generic tab-completion function that iterates through the</span>
    <span style="color:#75715e"># content of the current directory.</span>
    <span style="color:#66d9ef">return</span> self<span style="color:#f92672">.</span>_tab_directory_content()

# https://github.com/ranger/ranger/wiki/Integrating-File-Search-with-fzf # Now, simply bind this function to a key, by adding this to your ~/.config/ranger/rc.conf: map <C-f> fzf_select class fzf_select(Command): """ :fzf_select Find a file using fzf. With a prefix argument select only directories. See: https://github.com/junegunn/fzf """ def execute(self): import subprocess if self.quantifier: # match only directories command="find -L . ( -path '/.' -o -fstype 'dev' -o -fstype 'proc' ) -prune
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m" else: # match files and directories command="find -L . ( -path '/.' -o -fstype 'dev' -o -fstype 'proc' ) -prune
-o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m" fzf = self.fm.execute_command(command, stdout=subprocess.PIPE) stdout, stderr = fzf.communicate() if fzf.returncode == 0: fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n')) if os.path.isdir(fzf_file): self.fm.cd(fzf_file) else: self.fm.select_file(fzf_file) # fzf_locate class fzf_locate(Command): """ :fzf_locate Find a file using fzf. With a prefix argument select only directories. See: https://github.com/junegunn/fzf """ def execute(self): import subprocess if self.quantifier: command="locate home media | fzf -e -i" else: command="locate home media | fzf -e -i" fzf = self.fm.execute_command(command, stdout=subprocess.PIPE) stdout, stderr = fzf.communicate() if fzf.returncode == 0: fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n')) if os.path.isdir(fzf_file): self.fm.cd(fzf_file) else: self.fm.select_file(fzf_file)

class fzf_bring(Command): """ :fzf_bring Find a file using fzf and bring it to the current directory. See: https://github.com/junegunn/fzf """ def execute(self): import subprocess if self.quantifier: # match only directories command="find -L . ( -path '/.' -o -fstype 'dev' -o -fstype 'proc' ) -prune
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m" else: # match files and directories command="find -L . ( -path '/.' -o -fstype 'dev' -o -fstype 'proc' ) -prune
-o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m" fzf = self.fm.execute_command(command, stdout=subprocess.PIPE) stdout, stderr = fzf.communicate() if fzf.returncode == 0: fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n')) if os.path.isdir(fzf_file): self.fm.cd(fzf_file) else: self.fm.select_file(fzf_file)

import os from ranger.core.loader import CommandLoader

class compress(Command): def execute(self): """ Compress marked files to current directory """ cwd = self.fm.thisdir marked_files = cwd.get_selection()

    <span style="color:#66d9ef">if</span> <span style="color:#f92672">not</span> marked_files:
        <span style="color:#66d9ef">return</span>

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">refresh</span>(_):
        cwd <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>get_directory(original_path)
        cwd<span style="color:#f92672">.</span>load_content()

    original_path <span style="color:#f92672">=</span> cwd<span style="color:#f92672">.</span>path
    parts <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>line<span style="color:#f92672">.</span>split()
    au_flags <span style="color:#f92672">=</span> parts[<span style="color:#ae81ff">1</span>:]

    descr <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;compressing files in: &#34;</span> <span style="color:#f92672">+</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>basename(parts[<span style="color:#ae81ff">1</span>])
    obj <span style="color:#f92672">=</span> CommandLoader(args<span style="color:#f92672">=</span>[<span style="color:#e6db74">&#39;apack&#39;</span>] <span style="color:#f92672">+</span> au_flags <span style="color:#f92672">+</span> \
            [os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>relpath(f<span style="color:#f92672">.</span>path, cwd<span style="color:#f92672">.</span>path) <span style="color:#66d9ef">for</span> f <span style="color:#f92672">in</span> marked_files], descr<span style="color:#f92672">=</span>descr)

    obj<span style="color:#f92672">.</span>signal_bind(<span style="color:#e6db74">&#39;after&#39;</span>, refresh)
    self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>loader<span style="color:#f92672">.</span>add(obj)

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">tab</span>(self):
    <span style="color:#e6db74">&#34;&#34;&#34; Complete with current folder name &#34;&#34;&#34;</span>

    extension <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;.zip&#39;</span>, <span style="color:#e6db74">&#39;.tar.gz&#39;</span>, <span style="color:#e6db74">&#39;.rar&#39;</span>, <span style="color:#e6db74">&#39;.7z&#39;</span>]
    <span style="color:#66d9ef">return</span> [<span style="color:#e6db74">&#39;compress &#39;</span> <span style="color:#f92672">+</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>basename(self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>thisdir<span style="color:#f92672">.</span>path) <span style="color:#f92672">+</span> ext <span style="color:#66d9ef">for</span> ext <span style="color:#f92672">in</span> extension]

import os from ranger.core.loader import CommandLoader

class extracthere(Command): def execute(self): """ Extract copied files to current directory """ copied_files = tuple(self.fm.copy_buffer)

    <span style="color:#66d9ef">if</span> <span style="color:#f92672">not</span> copied_files:
        <span style="color:#66d9ef">return</span>

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">refresh</span>(_):
        cwd <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>get_directory(original_path)
        cwd<span style="color:#f92672">.</span>load_content()

    one_file <span style="color:#f92672">=</span> copied_files[<span style="color:#ae81ff">0</span>]
    cwd <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>thisdir
    original_path <span style="color:#f92672">=</span> cwd<span style="color:#f92672">.</span>path
    au_flags <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;-X&#39;</span>, cwd<span style="color:#f92672">.</span>path]
    au_flags <span style="color:#f92672">+=</span> self<span style="color:#f92672">.</span>line<span style="color:#f92672">.</span>split()[<span style="color:#ae81ff">1</span>:]
    au_flags <span style="color:#f92672">+=</span> [<span style="color:#e6db74">&#39;-e&#39;</span>]

    self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>copy_buffer<span style="color:#f92672">.</span>clear()
    self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>cut_buffer <span style="color:#f92672">=</span> False
    <span style="color:#66d9ef">if</span> len(copied_files) <span style="color:#f92672">==</span> <span style="color:#ae81ff">1</span>:
        descr <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;extracting: &#34;</span> <span style="color:#f92672">+</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>basename(one_file<span style="color:#f92672">.</span>path)
    <span style="color:#66d9ef">else</span>:
        descr <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;extracting files from: &#34;</span> <span style="color:#f92672">+</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>basename(one_file<span style="color:#f92672">.</span>dirname)
    obj <span style="color:#f92672">=</span> CommandLoader(args<span style="color:#f92672">=</span>[<span style="color:#e6db74">&#39;aunpack&#39;</span>] <span style="color:#f92672">+</span> au_flags \
            <span style="color:#f92672">+</span> [f<span style="color:#f92672">.</span>path <span style="color:#66d9ef">for</span> f <span style="color:#f92672">in</span> copied_files], descr<span style="color:#f92672">=</span>descr)

    obj<span style="color:#f92672">.</span>signal_bind(<span style="color:#e6db74">&#39;after&#39;</span>, refresh)
    self<span style="color:#f92672">.</span>fm<span style="color:#f92672">.</span>loader<span style="color:#f92672">.</span>add(obj)