Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

85 lines
2.5 KiB

4 years ago
  1. import os
  2. from ranger.api.commands import *
  3. from ranger.core.loader import CommandLoader
  4. class extract(Command):
  5. def execute(self):
  6. """Extract copied files to current directory or directory
  7. specified in a command line
  8. """
  9. cwd = self.fm.thisdir
  10. copied_files = cwd.get_selection()
  11. if not copied_files:
  12. return
  13. def refresh(_):
  14. cwd = self.fm.get_directory(original_path)
  15. cwd.load_content()
  16. one_file = copied_files[0]
  17. cwd = self.fm.thisdir
  18. original_path = cwd.path
  19. line_args = self.line.split()[1:]
  20. if line_args:
  21. extraction_dir = os.path.join(cwd.path, "".join(line_args))
  22. os.makedirs(extraction_dir, exist_ok=True)
  23. flags = ['-X', extraction_dir]
  24. flags += ['-e']
  25. else:
  26. flags = ['-X', cwd.path]
  27. flags += ['-e']
  28. self.fm.copy_buffer.clear()
  29. self.fm.cut_buffer = False
  30. if len(copied_files) == 1:
  31. descr = "Extracting: " + os.path.basename(one_file.path)
  32. else:
  33. descr = "Extracting files from: " + os.path.basename(one_file.dirname)
  34. obj = CommandLoader(args=['aunpack'] + flags \
  35. + [f.path for f in copied_files], descr=descr, read=True)
  36. obj.signal_bind('after', refresh)
  37. self.fm.loader.add(obj)
  38. class extract_to_dirs(Command):
  39. def execute(self):
  40. """ Extract copied files to a subdirectories """
  41. cwd = self.fm.thisdir
  42. original_path = cwd.path
  43. copied_files = cwd.get_selection()
  44. if not copied_files:
  45. return
  46. def refresh(_):
  47. cwd = self.fm.get_directory(original_path)
  48. cwd.load_content()
  49. def make_flags(fn):
  50. fn_wo_ext = os.path.basename(os.path.splitext(fn)[0])
  51. flags = ['-X', fn_wo_ext]
  52. return flags
  53. one_file = copied_files[0]
  54. self.fm.copy_buffer.clear()
  55. self.fm.cut_buffer = False
  56. # Making description line
  57. if len(copied_files) == 1:
  58. descr = "Extracting: " + os.path.basename(one_file.path)
  59. else:
  60. descr = "Extracting files from: " + os.path.basename(one_file.dirname)
  61. # Extracting files
  62. for f in copied_files:
  63. obj = CommandLoader(
  64. args=['aunpack'] + make_flags(f.path) + [f.path],
  65. descr=descr, read=True
  66. )
  67. obj.signal_bind('after', refresh)
  68. self.fm.loader.add(obj)