open selected mountpoint in the current tab

This commit is contained in:
Alexander Lutsai 2021-06-11 15:47:46 +03:00
parent 0dde4583b7
commit a7548bee17
3 changed files with 31 additions and 5 deletions

View File

@ -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

24
menu.py
View File

@ -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)

View File

@ -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)