You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.7 KiB

4 years ago
  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. # A simple command for demonstration purposes follows.
  7. # -----------------------------------------------------------------------------
  8. from __future__ import (absolute_import, division, print_function)
  9. # You can import any python module as needed.
  10. import os
  11. # You always need to import ranger.api.commands here to get the Command class:
  12. from ranger.api.commands import Command
  13. # Any class that is a subclass of "Command" will be integrated into ranger as a
  14. # command. Try typing ":my_edit<ENTER>" in ranger!
  15. class my_edit(Command):
  16. # The so-called doc-string of the class will be visible in the built-in
  17. # help that is accessible by typing "?c" inside ranger.
  18. """:my_edit <filename>
  19. A sample command for demonstration purposes that opens a file in an editor.
  20. """
  21. # The execute method is called when you run this command in ranger.
  22. def execute(self):
  23. # self.arg(1) is the first (space-separated) argument to the function.
  24. # This way you can write ":my_edit somefilename<ENTER>".
  25. if self.arg(1):
  26. # self.rest(1) contains self.arg(1) and everything that follows
  27. target_filename = self.rest(1)
  28. else:
  29. # self.fm is a ranger.core.filemanager.FileManager object and gives
  30. # you access to internals of ranger.
  31. # self.fm.thisfile is a ranger.container.file.File object and is a
  32. # reference to the currently selected file.
  33. target_filename = self.fm.thisfile.path
  34. # This is a generic function to print text in ranger.
  35. self.fm.notify("Let's edit the file " + target_filename + "!")
  36. # Using bad=True in fm.notify allows you to print error messages:
  37. if not os.path.exists(target_filename):
  38. self.fm.notify("The given file does not exist!", bad=True)
  39. return
  40. # This executes a function from ranger.core.acitons, a module with a
  41. # variety of subroutines that can help you construct commands.
  42. # Check out the source, or run "pydoc ranger.core.actions" for a list.
  43. self.fm.edit_file(target_filename)
  44. # The tab method is called when you press tab, and should return a list of
  45. # suggestions that the user will tab through.
  46. # tabnum is 1 for <TAB> and -1 for <S-TAB> by default
  47. def tab(self, tabnum):
  48. # This is a generic tab-completion function that iterates through the
  49. # content of the current directory.
  50. return self._tab_directory_content()