From a7548bee17da8f8a5ba5ed5d92c4c68a2defa82d Mon Sep 17 00:00:00 2001 From: Alexander Lutsai Date: Fri, 11 Jun 2021 15:47:46 +0300 Subject: [PATCH] open selected mountpoint in the current tab --- README.md | 1 + menu.py | 24 ++++++++++++++++++++---- mounter.py | 11 ++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2b49ba1..5105fde 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,4 @@ Enter in ranger `:mount`, than will be shown menu. In this menu you can press: - `m` to mount selected partition - `u` to unmount selected partition - `e` to unmount all partitions of selected partition's drive +- `ENTER` to open selected mountpoint in current tab diff --git a/menu.py b/menu.py index d0d9878..aa91c90 100644 --- a/menu.py +++ b/menu.py @@ -9,17 +9,19 @@ import curses import curses.ascii import subprocess import json +import sys class ChoosePartition: blkinfo = None screen = None selected_partn = 1 + selected_mountpoint = None partn = 1 help_message = [("Press 'm' to mount, " + "'u' to unmount, " + "'g' to refresh"), - " and 'e' to unmount the whole drive"] + " and 'e' to unmount, 'enter' to cd"] message = "" def __init__(self): @@ -75,9 +77,13 @@ class ChoosePartition: label = part[f] break - mp = "Not mounted" + mp = None if part['mountpoint'] is not None: mp = part['mountpoint'] + if is_selected: + self.selected_mountpoint = mp + if mp is None: + mp = "Not mounted" s = "{name:<12} {size:<8} {label:<16} {mp}".format( name=part['name'] if part['name'] is not None else "None", @@ -145,7 +151,8 @@ class ChoosePartition: sel = None x = 0 # quit when pressed `q` or `Esc` or `Ctrl+g` - while x not in (ord('q'), curses.ascii.ESC, curses.ascii.BEL): + while x not in (ord('q'), curses.ascii.ESC, + curses.ascii.BEL, curses.ascii.NL): self._select_print(x) x = self.screen.getch() if x in (ord('j'), curses.ascii.SO, curses.KEY_DOWN): @@ -171,6 +178,9 @@ class ChoosePartition: elif x == ord('g') or x == ord('r'): self._read_partitions() curses.endwin() + if self.selected_mountpoint is not None and x == curses.ascii.NL: + return self.selected_mountpoint + return "" def _udisk_mount_unmount(self, cmd, dev): r = "" @@ -193,4 +203,10 @@ class ChoosePartition: if __name__ == "__main__": cp = ChoosePartition() - cp.select() + sel = cp.select() + # print(sel) + # print(len(sys.argv)) + if len(sys.argv) >= 2: + # print(sys.argv[1]) + with open(sys.argv[1], 'w') as f: + f.write(sel) diff --git a/mounter.py b/mounter.py index 2701de1..905b5e6 100644 --- a/mounter.py +++ b/mounter.py @@ -6,6 +6,8 @@ # Description: This launches script that draws menu to choose, mount and unmount drives from ranger file manager from ranger.api.commands import Command +import tempfile +import os class mount(Command): @@ -16,5 +18,12 @@ class mount(Command): def execute(self): """ Show menu to mount and unmount """ + (f, p) = tempfile.mkstemp() + os.close(f) self.fm.execute_console( - "shell python3 ~/.config/ranger/ranger_udisk_menu/menu.py") + f"shell python3 ~/.config/ranger/ranger_udisk_menu/menu.py {p}") + with open(p, 'r') as f: + d = f.readline() + if os.path.exists(d): + self.fm.cd(d) + os.remove(p)