123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- # 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.
- # A simple command for demonstration purposes follows.
- # -----------------------------------------------------------------------------
- from __future__ import (absolute_import, division, print_function)
- # You can import any python module as needed.
- import os
- # You always need to import ranger.api.commands here to get the Command class:
- from ranger.api.commands import Command
- from ranger.core.loader import CommandLoader
- class MountC(Command):
- """:MountC
- Mounts Windows disk C.
- """
- def execute(self):
- self.fm.run("udisksctl mount -b /dev/sda1")
- class MountD(Command):
- """:MountD
- Mounts Windows disk D.
- """
- def execute(self):
- self.fm.run("udisksctl mount -b /dev/sda2")
- class UnmountC(Command):
- """:UnmountC
- Unmounts Windows disk C.
- """
- def execute(self):
- self.fm.run("udisksctl unmount -b /dev/sda1")
- class UnmountD(Command):
- """:UnmountD
- Unmounts Windows disk D.
- """
- def execute(self):
- self.fm.run("udisksctl unmount -b /dev/sda2")
- class extract_here(Command):
- def execute(self):
- """ extract selected files to current directory."""
- cwd = self.fm.thisdir
- marked_files = tuple(cwd.get_selection())
- def refresh(_):
- cwd = self.fm.get_directory(original_path)
- cwd.load_content()
- one_file = marked_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(marked_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 marked_files], descr=descr,
- read=True)
- obj.signal_bind('after', refresh)
- self.fm.loader.add(obj)# # Any class that is a subclass of "Command" will be integrated into ranger as a
- class compress(Command):
- def execute(self):
- """ Compress marked files to current directory """
- cwd = self.fm.thisdir
- marked_files = cwd.get_selection()
- if not marked_files:
- return
- def refresh(_):
- cwd = self.fm.get_directory(original_path)
- cwd.load_content()
- original_path = cwd.path
- parts = self.line.split()
- au_flags = parts[1:]
- descr = "compressing files in: " + os.path.basename(parts[1])
- obj = CommandLoader(args=['apack'] + au_flags + \
- [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr, read=True)
- obj.signal_bind('after', refresh)
- self.fm.loader.add(obj)
- def tab(self, tabnum):
- """ Complete with current folder name """
- extension = ['.zip', '.tar.gz', '.rar', '.7z']
- return ['compress ' + os.path.basename(self.fm.thisdir.path) + ext for ext in extension]
- # # 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.
- # """
- #
- # # The execute method is called when you run this command in ranger.
- # def execute(self):
- # # self.arg(1) is the first (space-separated) argument to the function.
- # # This way you can write ":my_edit somefilename<ENTER>".
- # if self.arg(1):
- # # self.rest(1) contains self.arg(1) and everything that follows
- # target_filename = self.rest(1)
- # else:
- # # self.fm is a ranger.core.filemanager.FileManager object and gives
- # # you access to internals of ranger.
- # # self.fm.thisfile is a ranger.container.file.File object and is a
- # # reference to the currently selected file.
- # target_filename = self.fm.thisfile.path
- #
- # # This is a generic function to print text in ranger.
- # self.fm.notify("Let's edit the file " + target_filename + "!")
- #
- # # Using bad=True in fm.notify allows you to print error messages:
- # if not os.path.exists(target_filename):
- # self.fm.notify("The given file does not exist!", bad=True)
- # return
- #
- # # This executes a function from ranger.core.acitons, a module with a
- # # variety of subroutines that can help you construct commands.
- # # Check out the source, or run "pydoc ranger.core.actions" for a list.
- # self.fm.edit_file(target_filename)
- #
- # # The tab method is called when you press tab, and should return a list of
- # # suggestions that the user will tab through.
- # # tabnum is 1 for <TAB> and -1 for <S-TAB> by default
- # def tab(self, tabnum):
- # # This is a generic tab-completion function that iterates through the
- # # content of the current directory.
- # return self._tab_directory_content()
|