From a5767f170319c0b7043c4e09c9e35331f15dcd29 Mon Sep 17 00:00:00 2001 From: toonn Date: Tue, 30 Jan 2018 15:05:43 +0100 Subject: Guard tmux title changes Changing the tmux window title to "ranger" was not guarded for the presence of the tmux executable, this tripped up at least one user with an `rc.conf` that still `set update_tmux_title true`. While the behavior is *not-a-bug*, I expect most people'd rather have the setting enabled by default since "python" is a less useful window title. Fix #1042 --- examples/rc_emacs.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/rc_emacs.conf b/examples/rc_emacs.conf index 26074a42..8924d1d6 100644 --- a/examples/rc_emacs.conf +++ b/examples/rc_emacs.conf @@ -129,7 +129,7 @@ set display_tags_in_all_columns true set update_title false # Set the title to "ranger" in the tmux program? -set update_tmux_title false +set update_tmux_title true # Shorten the title if it gets long? The number defines how many # directories are displayed at once, 0 turns off this feature. -- cgit 1.4.1-2-gfad0 From ed4eb4bf282354886e52ae6fa92bf68078edfef6 Mon Sep 17 00:00:00 2001 From: Stephane Fontaine Date: Sat, 24 Feb 2018 21:10:35 +0400 Subject: Add new option to disable display of free disk space in statusbar Fixes #1087 Useful on high-latency filesystems as it avoid calls to statvfs system call which costs: (number of redraw) * latency. --- doc/ranger.1 | 3 +++ doc/ranger.pod | 4 ++++ examples/rc_emacs.conf | 3 +++ ranger/config/rc.conf | 3 +++ ranger/container/settings.py | 1 + ranger/gui/widgets/statusbar.py | 15 ++++++++------- 6 files changed, 22 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/doc/ranger.1 b/doc/ranger.1 index fca30604..9ec79cfd 100644 --- a/doc/ranger.1 +++ b/doc/ranger.1 @@ -766,6 +766,9 @@ Display the file size in the main column? Display the file size in the status bar? .IP "display_tags_in_all_columns [bool]" 4 .IX Item "display_tags_in_all_columns [bool]" +Display the free disk space in the status bar? +.IP "display_free_space_in_status_bar [bool]" 4 +.IX Item "display_free_space_in_status_bar [bool]" Display tags in all columns? .IP "draw_borders [bool]" 4 .IX Item "draw_borders [bool]" diff --git a/doc/ranger.pod b/doc/ranger.pod index 523d8d9d..04598bd2 100644 --- a/doc/ranger.pod +++ b/doc/ranger.pod @@ -752,6 +752,10 @@ Display the file size in the main column? Display the file size in the status bar? +=item display_free_space_in_status_bar [bool] + +Display the free disk space in the status bar? + =item display_tags_in_all_columns [bool] Display tags in all columns? diff --git a/examples/rc_emacs.conf b/examples/rc_emacs.conf index 26074a42..4743f8a3 100644 --- a/examples/rc_emacs.conf +++ b/examples/rc_emacs.conf @@ -122,6 +122,9 @@ set mouse_enabled true set display_size_in_main_column true set display_size_in_status_bar true +# Display the free disk space in the status bar? +set display_free_space_in_status_bar true + # Display files tags in all columns or only in main column? set display_tags_in_all_columns true diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf index 676090fb..51d18ff2 100644 --- a/ranger/config/rc.conf +++ b/ranger/config/rc.conf @@ -137,6 +137,9 @@ set mouse_enabled true set display_size_in_main_column true set display_size_in_status_bar true +# Display the free disk space in the status bar? +set display_free_space_in_status_bar true + # Display files tags in all columns or only in main column? set display_tags_in_all_columns true diff --git a/ranger/container/settings.py b/ranger/container/settings.py index d0b094d0..e87b6e48 100644 --- a/ranger/container/settings.py +++ b/ranger/container/settings.py @@ -38,6 +38,7 @@ ALLOWED_SETTINGS = { 'dirname_in_tabs': bool, 'display_size_in_main_column': bool, 'display_size_in_status_bar': bool, + "display_free_space_in_status_bar": bool, 'display_tags_in_all_columns': bool, 'draw_borders': bool, 'draw_progress_bar_in_status_bar': bool, diff --git a/ranger/gui/widgets/statusbar.py b/ranger/gui/widgets/statusbar.py index 266d48ca..3457955e 100644 --- a/ranger/gui/widgets/statusbar.py +++ b/ranger/gui/widgets/statusbar.py @@ -275,13 +275,14 @@ class StatusBar(Widget): # pylint: disable=too-many-instance-attributes right.add("/" + str(len(target.marked_items))) else: right.add(human_readable(target.disk_usage, separator='') + " sum") - try: - free = get_free_space(target.mount_path) - except OSError: - pass - else: - right.add(", ", "space") - right.add(human_readable(free, separator='') + " free") + if self.settings.display_free_space_in_status_bar: + try: + free = get_free_space(target.mount_path) + except OSError: + pass + else: + right.add(", ", "space") + right.add(human_readable(free, separator='') + " free") right.add(" ", "space") if target.marked_items: -- cgit 1.4.1-2-gfad0 From 00366b27e12023d31dc445660df05000cc47a114 Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski Date: Sun, 1 Apr 2018 23:32:00 +0200 Subject: examples: Add plugin_avfs.py, a simple AVFS integration http://avf.sourceforge.net/ --- examples/plugin_avfs.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/plugin_avfs.py (limited to 'examples') diff --git a/examples/plugin_avfs.py b/examples/plugin_avfs.py new file mode 100644 index 00000000..6910533c --- /dev/null +++ b/examples/plugin_avfs.py @@ -0,0 +1,31 @@ +# Tested with ranger 1.9.1 +# +# A very simple and possibly buggy support for AVFS +# (http://avf.sourceforge.net/), that allows ranger to handle +# archives. +# +# Run `:avfs' to browse the selected archive. + +from __future__ import (absolute_import, division, print_function) + +from ranger.api.commands import Command +import os +import os.path + +class avfs(Command): + avfs_root = os.path.join(os.environ["HOME"], ".avfs") + avfs_suffix = "#" + + def execute(self): + if os.path.isdir(self.avfs_root): + archive_directory = "".join([ + self.avfs_root, + self.fm.thisfile.path, + self.avfs_suffix, + ]) + if os.path.isdir(archive_directory): + self.fm.cd(archive_directory) + else: + self.fm.notify("This file cannot be handled by avfs.", bad=True) + else: + self.fm.notify("Install `avfs' and run `mountavfs' first.", bad=True) -- cgit 1.4.1-2-gfad0 From 8a9ede42a95b703daa35364aca32b672cfb680f7 Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski Date: Sun, 1 Apr 2018 23:56:01 +0200 Subject: Fix pylint --- examples/plugin_avfs.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/plugin_avfs.py b/examples/plugin_avfs.py index 6910533c..07968a03 100644 --- a/examples/plugin_avfs.py +++ b/examples/plugin_avfs.py @@ -8,11 +8,13 @@ from __future__ import (absolute_import, division, print_function) -from ranger.api.commands import Command import os import os.path -class avfs(Command): +from ranger.api.commands import Command + + +class avfs(Command): # pylint: disable=invalid-name avfs_root = os.path.join(os.environ["HOME"], ".avfs") avfs_suffix = "#" -- cgit 1.4.1-2-gfad0 From 970d2ae77f7988e4af0dc604ff6444306f5d9be4 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 18:08:22 -0700 Subject: Update plugin_pmount.py Currently the pmount plugin uses static device letters and partition numbers. this change uses present devices and partitions. - Changes the binds to remove the need for uppercase device letters, if a device has no partitions mounts/unmounts the device. - With only one partition the same binds will handle it instead of the whole device. - With greater than one partitions, map for each. This is still not fully functional, devices not present when ranger is started *(usb drives)* won't have a map created unless ranger is restarted :| ... Is there a simple way to have ranger 'reload' this plugin, or use a different hook, etc.? --- examples/plugin_pmount.py | 67 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 20 deletions(-) (limited to 'examples') diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index 38c4a17c..feb54c83 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -3,38 +3,65 @@ # This plugin creates a bunch of keybindings used to mount and unmount # the devices using pmount(1). # -# alt+m : mount /dev/sd -# alt+m : mount /dev/sd -# alt+shift+m : unmount /dev/sd -# alt+shift+m : unmount /dev/sd -# alt+shift+n : list the devices +# (multiple partitions): alt+m : mount /dev/sd +# (one partition): alt+m : mount /dev/sd1 +# (no partitions): alt+m : mount /dev/sd +# +# (multiple partitions): alt+M : unmount /dev/sd +# (one partition): alt+M : unmount /dev/sd1 +# (no partitions): alt+M : unmount /dev/sd +# +# alt+n : list the devices from __future__ import (absolute_import, division, print_function) import ranger.api +import subprocess MOUNT_KEY = 'm' UMOUNT_KEY = 'M' -LIST_MOUNTS_KEY = 'N' - - +LIST_MOUNTS_KEY = 'n' HOOK_INIT_OLD = ranger.api.hook_init - def hook_init(fm): 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": + + diskcmd = "lsblk -lno NAME | grep -w 'sd[a-z]' | sed 's/sd//'" + disks = subprocess.check_output(diskcmd, + shell=True).decode('utf-8').replace('\r', '').replace('\n', '') + + for disk in disks: + partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk) + + try: + numparts = int(subprocess.check_output(partcmd, + shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) + except ValueError: + numparts = 0 + + if numparts == 0: + # no partition, mount the whole device + fm.execute_console("map {key}{0} chain shell pmount sd{0}; cd /media/sd{0}".format( + disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{0}".format( + disk, key=UMOUNT_KEY)) + + elif numparts == 1: + # only one partition, mount the partition 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)) + "map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( + disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format( + disk, key=UMOUNT_KEY)) + + else: + # use range start 1, /dev/sd{device}0 doesn't exist + for part in range(1, numparts + 1): + 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)) return HOOK_INIT_OLD(fm) -- cgit 1.4.1-2-gfad0 From 58bcf8691d522d658e8b4a1c0389d5bb53c3d1aa Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 19:01:13 -0700 Subject: Indentation and whitespace cleanup --- examples/plugin_pmount.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index feb54c83..f7e1bd3c 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -27,15 +27,13 @@ def hook_init(fm): fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY)) diskcmd = "lsblk -lno NAME | grep -w 'sd[a-z]' | sed 's/sd//'" - disks = subprocess.check_output(diskcmd, - shell=True).decode('utf-8').replace('\r', '').replace('\n', '') + disks = subprocess.check_output(diskcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '') for disk in disks: partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk) - + try: - numparts = int(subprocess.check_output(partcmd, - shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) + numparts = int(subprocess.check_output(partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) except ValueError: numparts = 0 @@ -48,18 +46,16 @@ def hook_init(fm): elif numparts == 1: # only one partition, mount the partition - fm.execute_console( - "map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( - disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( + disk, key=MOUNT_KEY)) fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format( disk, key=UMOUNT_KEY)) - + else: # use range start 1, /dev/sd{device}0 doesn't exist for part in range(1, numparts + 1): - 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 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)) -- cgit 1.4.1-2-gfad0 From 0e4577ea8ac0ea99e2c44164e2e81182d1ec4d07 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 19:07:30 -0700 Subject: import order and break long lines --- examples/plugin_pmount.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index f7e1bd3c..3079b5a3 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -15,8 +15,8 @@ from __future__ import (absolute_import, division, print_function) -import ranger.api import subprocess +import ranger.api MOUNT_KEY = 'm' UMOUNT_KEY = 'M' @@ -27,13 +27,15 @@ def hook_init(fm): fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY)) diskcmd = "lsblk -lno NAME | grep -w 'sd[a-z]' | sed 's/sd//'" - disks = subprocess.check_output(diskcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '') + disks = subprocess.check_output( + diskcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '') for disk in disks: partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk) try: - numparts = int(subprocess.check_output(partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) + numparts = int(subprocess.check_output( + partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) except ValueError: numparts = 0 @@ -43,19 +45,21 @@ def hook_init(fm): disk, key=MOUNT_KEY)) fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{0}".format( disk, key=UMOUNT_KEY)) - + elif numparts == 1: # only one partition, mount the partition - fm.execute_console("map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( - disk, key=MOUNT_KEY)) + fm.execute_console( + "map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( + disk, key=MOUNT_KEY)) fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format( disk, key=UMOUNT_KEY)) else: # use range start 1, /dev/sd{device}0 doesn't exist for part in range(1, numparts + 1): - 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 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)) -- cgit 1.4.1-2-gfad0 From d0f4e2c56af9e0e6452289fd136bb3c9934af225 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 19:10:47 -0700 Subject: 2 lines around function definition --- examples/plugin_pmount.py | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index 3079b5a3..f5bd1c67 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -23,6 +23,7 @@ UMOUNT_KEY = 'M' LIST_MOUNTS_KEY = 'n' HOOK_INIT_OLD = ranger.api.hook_init + def hook_init(fm): fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY)) -- cgit 1.4.1-2-gfad0 From 89b0c60b69f341a954d898eab031330fb1e1679a Mon Sep 17 00:00:00 2001 From: Mark Blakeney Date: Sun, 19 Aug 2018 14:04:04 +1000 Subject: Remove hard code of ranger path Hard coding the path of ranger prohibits the bash_automatic_cd.sh script from working when the user has installed ranger in a different path such as in /usr/local/bin/, or ~/.local/bin/ etc. --- examples/bash_automatic_cd.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/bash_automatic_cd.sh b/examples/bash_automatic_cd.sh index bdac5757..04b58e24 100644 --- a/examples/bash_automatic_cd.sh +++ b/examples/bash_automatic_cd.sh @@ -6,12 +6,10 @@ # the last visited one after ranger quits. # To undo the effect of this function, you can type "cd -" to return to the # original directory. -# -# On OS X 10 or later, replace `usr/bin/ranger` with `/usr/local/bin/ranger`. function ranger-cd { tempfile="$(mktemp -t tmp.XXXXXX)" - /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}" + ranger --choosedir="$tempfile" "${@:-$(pwd)}" test -f "$tempfile" && if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then cd -- "$(cat "$tempfile")" -- cgit 1.4.1-2-gfad0 From 573bc3aff2c6881928fae984280ff03ad5360fe7 Mon Sep 17 00:00:00 2001 From: natemaia Date: Wed, 5 Sep 2018 20:32:04 -0700 Subject: Remove extra pipe in subprocess call, rename plugin and add original --- examples/plugin_pmount.py | 66 +++++++++++------------------------- examples/plugin_pmount_dynamic.py | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 47 deletions(-) create mode 100644 examples/plugin_pmount_dynamic.py (limited to 'examples') diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index f5bd1c67..38c4a17c 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -3,66 +3,38 @@ # This plugin creates a bunch of keybindings used to mount and unmount # the devices using pmount(1). # -# (multiple partitions): alt+m : mount /dev/sd -# (one partition): alt+m : mount /dev/sd1 -# (no partitions): alt+m : mount /dev/sd -# -# (multiple partitions): alt+M : unmount /dev/sd -# (one partition): alt+M : unmount /dev/sd1 -# (no partitions): alt+M : unmount /dev/sd -# -# alt+n : list the devices +# alt+m : mount /dev/sd +# alt+m : mount /dev/sd +# alt+shift+m : unmount /dev/sd +# alt+shift+m : unmount /dev/sd +# alt+shift+n : list the devices from __future__ import (absolute_import, division, print_function) -import subprocess import ranger.api MOUNT_KEY = 'm' UMOUNT_KEY = 'M' -LIST_MOUNTS_KEY = 'n' +LIST_MOUNTS_KEY = 'N' + + HOOK_INIT_OLD = ranger.api.hook_init def hook_init(fm): fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY)) - - diskcmd = "lsblk -lno NAME | grep -w 'sd[a-z]' | sed 's/sd//'" - disks = subprocess.check_output( - diskcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '') - - for disk in disks: - partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk) - - try: - numparts = int(subprocess.check_output( - partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) - except ValueError: - numparts = 0 - - if numparts == 0: - # no partition, mount the whole device - fm.execute_console("map {key}{0} chain shell pmount sd{0}; cd /media/sd{0}".format( - disk, key=MOUNT_KEY)) - fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{0}".format( - disk, key=UMOUNT_KEY)) - - elif numparts == 1: - # only one partition, mount the partition + 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} chain shell pmount sd{0}1; cd /media/sd{0}1".format( - disk, key=MOUNT_KEY)) - fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format( - disk, key=UMOUNT_KEY)) - - else: - # use range start 1, /dev/sd{device}0 doesn't exist - for part in range(1, numparts + 1): - 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)) + "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)) return HOOK_INIT_OLD(fm) diff --git a/examples/plugin_pmount_dynamic.py b/examples/plugin_pmount_dynamic.py new file mode 100644 index 00000000..1448e15b --- /dev/null +++ b/examples/plugin_pmount_dynamic.py @@ -0,0 +1,70 @@ +# Tested with ranger 1.7.2 +# +# This plugin creates a bunch of keybindings used to mount and unmount +# the devices using pmount(1). +# +# (multiple partitions): alt+m : mount /dev/sd +# (one partition): alt+m : mount /dev/sd1 +# (no partitions): alt+m : mount /dev/sd +# +# (multiple partitions): alt+M : unmount /dev/sd +# (one partition): alt+M : unmount /dev/sd1 +# (no partitions): alt+M : unmount /dev/sd +# +# alt+n : list the devices + +from __future__ import (absolute_import, division, print_function) + +import subprocess +import ranger.api + +MOUNT_KEY = 'm' +UMOUNT_KEY = 'M' +LIST_MOUNTS_KEY = 'n' +HOOK_INIT_OLD = ranger.api.hook_init + + +def hook_init(fm): + fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY)) + + diskcmd = "lsblk -lno NAME | awk '!/[1-9]/ {sub(/sd/, \"\"); print}'" + disks = subprocess.check_output( + diskcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '') + + for disk in disks: + partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk) + + try: + numparts = int(subprocess.check_output( + partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) + except ValueError: + numparts = 0 + + if numparts == 0: + # no partition, mount the whole device + fm.execute_console("map {key}{0} chain shell pmount sd{0}; cd /media/sd{0}".format( + disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{0}".format( + disk, key=UMOUNT_KEY)) + + elif numparts == 1: + # only one partition, mount the partition + fm.execute_console( + "map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( + disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format( + disk, key=UMOUNT_KEY)) + + else: + # use range start 1, /dev/sd{device}0 doesn't exist + for part in range(1, numparts + 1): + 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)) + + return HOOK_INIT_OLD(fm) + + +ranger.api.hook_init = hook_init -- cgit 1.4.1-2-gfad0