summary refs log tree commit diff stats
path: root/examples/plugin_pmount.py
blob: 5db85385ac3d7d594313ad586888796f024a8ce9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Tested with ranger 1.7.2
#
# This plugin creates a bunch of keybindings used to mount and unmount
# the devices using pmount(1).
#
# alt+m       <letter>            <digit>: mount /dev/sd<letter><digit>
# alt+m       <uppercase letter>         : mount /dev/sd<letter>
# alt+shift+m <letter>            <digit>: unmount /dev/sd<letter><digit>
# alt+shift+m <uppercase letter>         : unmount /dev/sd<letter>
# alt+shift+n                            : list the devices

from __future__ import (absolute_import, print_function)

import ranger.api

MOUNT_KEY = '<alt>m'
UMOUNT_KEY = '<alt>M'
LIST_MOUNTS_KEY = '<alt>N'


HOOK_INIT_OLD = ranger.api.hook_init


def hook_init(fm):
    try:
        fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY))
        for disk in "abcdefgh":
            fm.execute_console("map {key}{0} chain shell pmount sd{1}; cd /media/sd{1}".format(
                disk.upper(), disk, key=MOUNT_KEY))
            fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{1}".format(
                disk.upper(), disk, key=UMOUNT_KEY))
            for part in "123456789":
                fm.execute_console(
                    "map {key}{0}{1} chain shell pmount sd{0}{1}; cd /media/sd{0}{1}".format(
                        disk, part, key=MOUNT_KEY)
                )
                fm.execute_console("map {key}{0}{1} chain cd; shell pumount sd{0}{1}".format(
                    disk, part, key=UMOUNT_KEY))
    except Exception:
        pass

    return HOOK_INIT_OLD(fm)


ranger.api.hook_init = hook_init