Dotfiles for my tiling window manager + terminal workflow.
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.

216 lines
7.5 KiB

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