| 1 |
#!/usr/bin/env sh |
| 2 |
|
| 3 |
# Description: Autodetects a nnn remote mountpoint (mounted with `c`) |
| 4 |
# from any of its subfolders and allows unmounting it |
| 5 |
# from the subdir without navigating to the mountppoint |
| 6 |
# or entering the remote name. Also works when hovering |
| 7 |
# the mountpoint directly like vanilla `u`. |
| 8 |
# |
| 9 |
# Dependencies: fusermount |
| 10 |
# |
| 11 |
# Shell: POSIX compliant |
| 12 |
# Authors: Kabouik & 0xACE |
| 13 |
# |
| 14 |
# TODO: |
| 15 |
# - Avoid lazy unmount by forcing nnn context to leave the subfolder before fusermount. |
| 16 |
# Tried `printf "%s" "0c$m" > "$NNN_PIPE"` but it breaks the nnn interface, see #854. |
| 17 |
|
| 18 |
err=0 |
| 19 |
m=$HOME/.config/nnn/mounts |
| 20 |
if [ "$PWD" = "$m" ]; then |
| 21 |
# Allow running the script on hovered directory if user is in ~/.config/nnn/mounts |
| 22 |
d="$1" |
| 23 |
else |
| 24 |
d=$(dirname "$(readlink -f "$1")" | grep -oP "^$m\K.*" | cut -d"/" -f2) |
| 25 |
fi |
| 26 |
|
| 27 |
# Test if user is within $m or a subdir, abort if not |
| 28 |
if [ "$d" = "" ]; then |
| 29 |
clear && printf "You are not in a remote folder mounted with nnn. Press return to continue. " && read -r _ |
| 30 |
else |
| 31 |
# Test if $m/$d is a mountpoint and try unmounting if it is |
| 32 |
mountpoint -q -- "$m/$d" |
| 33 |
if [ "$?" -eq "1" ]; then |
| 34 |
clear && printf "Parent '%s' is not a mountpoint. Press return to continue. " "$d" && read -r _ |
| 35 |
else |
| 36 |
cd "$m" && fusermount -uq "$m/$d" || err=1 |
| 37 |
if [ "$err" -eq "0" ]; then |
| 38 |
rmdir "$m/$d" && clear && printf "Parent '%s' unmounted." "$d" |
| 39 |
else |
| 40 |
clear && printf "Failed to unmount. Try lazy unmount? [Yy/Nn] " && read -r |
| 41 |
fi |
| 42 |
fi |
| 43 |
fi |
| 44 |
|
| 45 |
# If unmount fails, offer lazy unmount |
| 46 |
if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then |
| 47 |
err=0 |
| 48 |
cd "$m" && fusermount -uqz "$m/$d" || err=1 |
| 49 |
if [ "$err" -eq "0" ]; then |
| 50 |
rmdir "$m/$d" && clear && printf "Parent '%s' unmounted with lazy unmount. " "$d" |
| 51 |
fi |
| 52 |
fi |