dotfiles

My dotfiles used for LARBS
Log | Files | Refs | README | LICENSE

commands.py (7569B)


      1 # This is a sample commands.py.  You can add your own commands here.
      2 #
      3 # Please refer to commands_full.py for all the default commands and a complete
      4 # documentation.  Do NOT add them all here, or you may end up with defunct
      5 # commands when upgrading ranger.
      6 
      7 # You always need to import ranger.api.commands here to get the Command class:
      8 from ranger.api.commands import *
      9 from ranger.core.loader import CommandLoader
     10 
     11 # A simple command for demonstration purposes follows.
     12 #------------------------------------------------------------------------------
     13 
     14 # You can import any python module as needed.
     15 import os
     16 
     17 # Any class that is a subclass of "Command" will be integrated into ranger as a
     18 # command.  Try typing ":my_edit<ENTER>" in ranger!
     19 class my_edit(Command):
     20     # The so-called doc-string of the class will be visible in the built-in
     21     # help that is accessible by typing "?c" inside ranger.
     22     """:my_edit <filename>
     23 
     24     A sample command for demonstration purposes that opens a file in an editor.
     25     """
     26 
     27     # The execute method is called when you run this command in ranger.
     28     def execute(self):
     29         # self.arg(1) is the first (space-separated) argument to the function.
     30         # This way you can write ":my_edit somefilename<ENTER>".
     31         if self.arg(1):
     32             # self.rest(1) contains self.arg(1) and everything that follows
     33             target_filename = self.rest(1)
     34         else:
     35             # self.fm is a ranger.core.filemanager.FileManager object and gives
     36             # you access to internals of ranger.
     37             # self.fm.thisfile is a ranger.container.file.File object and is a
     38             # reference to the currently selected file.
     39             target_filename = self.fm.thisfile.path
     40 
     41         # This is a generic function to print text in ranger.  
     42         self.fm.notify("Let's edit the file " + target_filename + "!")
     43 
     44         # Using bad=True in fm.notify allows you to print error messages:
     45         if not os.path.exists(target_filename):
     46             self.fm.notify("The given file does not exist!", bad=True)
     47             return
     48 
     49         # This executes a function from ranger.core.acitons, a module with a
     50         # variety of subroutines that can help you construct commands.
     51         # Check out the source, or run "pydoc ranger.core.actions" for a list.
     52         self.fm.edit_file(target_filename)
     53 
     54     # The tab method is called when you press tab, and should return a list of
     55     # suggestions that the user will tab through.
     56     def tab(self):
     57         # This is a generic tab-completion function that iterates through the
     58         # content of the current directory.
     59         return self._tab_directory_content()
     60 
     61 
     62 # https://github.com/ranger/ranger/wiki/Integrating-File-Search-with-fzf
     63 # Now, simply bind this function to a key, by adding this to your ~/.config/ranger/rc.conf: map <C-f> fzf_select
     64 class fzf_select(Command):
     65     """
     66     :fzf_select
     67 
     68     Find a file using fzf.
     69 
     70     With a prefix argument select only directories.
     71 
     72     See: https://github.com/junegunn/fzf
     73     """
     74     def execute(self):
     75         import subprocess
     76         if self.quantifier:
     77             # match only directories
     78             command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
     79             -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
     80         else:
     81             # match files and directories
     82             command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
     83             -o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
     84         fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
     85         stdout, stderr = fzf.communicate()
     86         if fzf.returncode == 0:
     87             fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
     88             if os.path.isdir(fzf_file):
     89                 self.fm.cd(fzf_file)
     90             else:
     91                 self.fm.select_file(fzf_file)
     92 
     93 
     94 # fzf_locate
     95 class fzf_locate(Command):
     96     """
     97     :fzf_locate
     98 
     99     Find a file using fzf.
    100 
    101     With a prefix argument select only directories.
    102 
    103     See: https://github.com/junegunn/fzf
    104     """
    105     def execute(self):
    106         import subprocess
    107         if self.quantifier:
    108             command="locate home media | fzf -e -i"
    109         else:
    110             command="locate home media | fzf -e -i"
    111         fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
    112         stdout, stderr = fzf.communicate()
    113         if fzf.returncode == 0:
    114             fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
    115             if os.path.isdir(fzf_file):
    116                 self.fm.cd(fzf_file)
    117             else:
    118                 self.fm.select_file(fzf_file)
    119 
    120 
    121 class fzf_bring(Command):
    122     """
    123     :fzf_bring
    124 
    125     Find a file using fzf and bring it to the current directory.
    126 
    127     See: https://github.com/junegunn/fzf
    128     """
    129     def execute(self):
    130         import subprocess
    131         import shutil
    132         if self.quantifier:
    133             # match only directories
    134             command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
    135             -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
    136         else:
    137             # match files and directories
    138             command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
    139             -o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
    140         fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
    141         stdout, stderr = fzf.communicate()
    142         if fzf.returncode == 0:
    143             fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
    144             shutil.move(fzf_file, self.fm.thisdir.path)
    145 
    146 
    147 class compress(Command):
    148     def execute(self):
    149         """ Compress marked files to current directory """
    150         cwd = self.fm.thisdir
    151         marked_files = cwd.get_selection()
    152 
    153         if not marked_files:
    154             return
    155 
    156         def refresh(_):
    157             cwd = self.fm.get_directory(original_path)
    158             cwd.load_content()
    159 
    160         original_path = cwd.path
    161         parts = self.line.split()
    162         au_flags = parts[1:]
    163 
    164         descr = "compressing files in: " + os.path.basename(parts[1])
    165         obj = CommandLoader(args=['apack'] + au_flags + \
    166                 [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr)
    167 
    168         obj.signal_bind('after', refresh)
    169         self.fm.loader.add(obj)
    170 
    171     def tab(self):
    172         """ Complete with current folder name """
    173 
    174         extension = ['.zip', '.tar.gz', '.rar', '.7z']
    175         return ['compress ' + os.path.basename(self.fm.thisdir.path) + ext for ext in extension]
    176 
    177 
    178 class extracthere(Command):
    179     def execute(self):
    180         """ Extract copied files to current directory """
    181         copied_files = tuple(self.fm.copy_buffer)
    182 
    183         if not copied_files:
    184             return
    185 
    186         def refresh(_):
    187             cwd = self.fm.get_directory(original_path)
    188             cwd.load_content()
    189 
    190         one_file = copied_files[0]
    191         cwd = self.fm.thisdir
    192         original_path = cwd.path
    193         au_flags = ['-X', cwd.path]
    194         au_flags += self.line.split()[1:]
    195         au_flags += ['-e']
    196 
    197         self.fm.copy_buffer.clear()
    198         self.fm.cut_buffer = False
    199         if len(copied_files) == 1:
    200             descr = "extracting: " + os.path.basename(one_file.path)
    201         else:
    202             descr = "extracting files from: " + os.path.basename(one_file.dirname)
    203         obj = CommandLoader(args=['aunpack'] + au_flags \
    204                 + [f.path for f in copied_files], descr=descr)
    205 
    206         obj.signal_bind('after', refresh)
    207         self.fm.loader.add(obj)
    208