commands.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. from ranger.core.loader import CommandLoader
  14. class MountC(Command):
  15. """:MountC
  16. Mounts Windows disk C.
  17. """
  18. def execute(self):
  19. self.fm.run("udisksctl mount -b /dev/sda1")
  20. class MountD(Command):
  21. """:MountD
  22. Mounts Windows disk D.
  23. """
  24. def execute(self):
  25. self.fm.run("udisksctl mount -b /dev/sda2")
  26. class UnmountC(Command):
  27. """:UnmountC
  28. Unmounts Windows disk C.
  29. """
  30. def execute(self):
  31. self.fm.run("udisksctl unmount -b /dev/sda1")
  32. class UnmountD(Command):
  33. """:UnmountD
  34. Unmounts Windows disk D.
  35. """
  36. def execute(self):
  37. self.fm.run("udisksctl unmount -b /dev/sda2")
  38. class extract_here(Command):
  39. def execute(self):
  40. """ extract selected files to current directory."""
  41. cwd = self.fm.thisdir
  42. marked_files = tuple(cwd.get_selection())
  43. def refresh(_):
  44. cwd = self.fm.get_directory(original_path)
  45. cwd.load_content()
  46. one_file = marked_files[0]
  47. cwd = self.fm.thisdir
  48. original_path = cwd.path
  49. au_flags = ['-x', cwd.path]
  50. au_flags += self.line.split()[1:]
  51. au_flags += ['-e']
  52. self.fm.copy_buffer.clear()
  53. self.fm.cut_buffer = False
  54. if len(marked_files) == 1:
  55. descr = "extracting: " + os.path.basename(one_file.path)
  56. else:
  57. descr = "extracting files from: " + os.path.basename(
  58. one_file.dirname)
  59. obj = CommandLoader(args=['aunpack'] + au_flags
  60. + [f.path for f in marked_files], descr=descr,
  61. read=True)
  62. obj.signal_bind('after', refresh)
  63. self.fm.loader.add(obj)# # Any class that is a subclass of "Command" will be integrated into ranger as a
  64. class compress(Command):
  65. def execute(self):
  66. """ Compress marked files to current directory """
  67. cwd = self.fm.thisdir
  68. marked_files = cwd.get_selection()
  69. if not marked_files:
  70. return
  71. def refresh(_):
  72. cwd = self.fm.get_directory(original_path)
  73. cwd.load_content()
  74. original_path = cwd.path
  75. parts = self.line.split()
  76. au_flags = parts[1:]
  77. descr = "compressing files in: " + os.path.basename(parts[1])
  78. obj = CommandLoader(args=['apack'] + au_flags + \
  79. [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr, read=True)
  80. obj.signal_bind('after', refresh)
  81. self.fm.loader.add(obj)
  82. def tab(self, tabnum):
  83. """ Complete with current folder name """
  84. extension = ['.zip', '.tar.gz', '.rar', '.7z']
  85. return ['compress ' + os.path.basename(self.fm.thisdir.path) + ext for ext in extension]
  86. # # command. Try typing ":my_edit<ENTER>" in ranger!
  87. # class my_edit(Command):
  88. # # The so-called doc-string of the class will be visible in the built-in
  89. # # help that is accessible by typing "?c" inside ranger.
  90. # """:my_edit <filename>
  91. #
  92. # A sample command for demonstration purposes that opens a file in an editor.
  93. # """
  94. #
  95. # # The execute method is called when you run this command in ranger.
  96. # def execute(self):
  97. # # self.arg(1) is the first (space-separated) argument to the function.
  98. # # This way you can write ":my_edit somefilename<ENTER>".
  99. # if self.arg(1):
  100. # # self.rest(1) contains self.arg(1) and everything that follows
  101. # target_filename = self.rest(1)
  102. # else:
  103. # # self.fm is a ranger.core.filemanager.FileManager object and gives
  104. # # you access to internals of ranger.
  105. # # self.fm.thisfile is a ranger.container.file.File object and is a
  106. # # reference to the currently selected file.
  107. # target_filename = self.fm.thisfile.path
  108. #
  109. # # This is a generic function to print text in ranger.
  110. # self.fm.notify("Let's edit the file " + target_filename + "!")
  111. #
  112. # # Using bad=True in fm.notify allows you to print error messages:
  113. # if not os.path.exists(target_filename):
  114. # self.fm.notify("The given file does not exist!", bad=True)
  115. # return
  116. #
  117. # # This executes a function from ranger.core.acitons, a module with a
  118. # # variety of subroutines that can help you construct commands.
  119. # # Check out the source, or run "pydoc ranger.core.actions" for a list.
  120. # self.fm.edit_file(target_filename)
  121. #
  122. # # The tab method is called when you press tab, and should return a list of
  123. # # suggestions that the user will tab through.
  124. # # tabnum is 1 for <TAB> and -1 for <S-TAB> by default
  125. # def tab(self, tabnum):
  126. # # This is a generic tab-completion function that iterates through the
  127. # # content of the current directory.
  128. # return self._tab_directory_content()