[Plugins] Fuzzy find plugins and run them; unmount mountpoints from their subfolders (#977)

* Fuzzy find plugins and run them

* Hide find warning when $otherplugins is not set

* Fix prompt on error 2

* Use /home/mathieu

* unmount-parent plugin

* Add dependencies and shell description

* Add dependencies and fix CI issue?

* Improve fzfplug prompt, fix shellcheck warnings, restore mistakenly deleted line in README

* Typo

* Make both scripts POSIX-compliant and small improvements

* Final cosmetic changes

* bis

* Clarify description

* Typo

* Typo

* Better support for custom dirs and use bat if available

Co-authored-by: M <>
This commit is contained in:
Kabouik 2021-04-30 23:59:10 +00:00 committed by Arun Prakash Jana
parent 5ed2998023
commit c2aeb51bcc
No known key found for this signature in database
GPG key ID: A75979F35C080412
3 changed files with 99 additions and 0 deletions

View file

@ -27,6 +27,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
| [fzcd](fzcd) | Change to the directory of a fuzzy-selected file/dir | sh | fzf | | [fzcd](fzcd) | Change to the directory of a fuzzy-selected file/dir | sh | fzf |
| [fzhist](fzhist) | Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzf, mktemp | | [fzhist](fzhist) | Fuzzy-select a cmd from history, edit in `$EDITOR` and run | sh | fzf, mktemp |
| [fzopen](fzopen) | Fuzzy find a file in dir subtree and edit or open | sh | fzf, xdg-open | | [fzopen](fzopen) | Fuzzy find a file in dir subtree and edit or open | sh | fzf, xdg-open |
| [fzplug](fzplug) | Fuzzy find, preview and run other plugins | sh | fzf |
| [fzz](fzz) | Change to any directory in the z database with fzf | sh | fzf, z | | [fzz](fzz) | Change to any directory in the z database with fzf | sh | fzf, z |
| [getplugs](getplugs) | Update plugins to installed `nnn` version | sh | curl | | [getplugs](getplugs) | Update plugins to installed `nnn` version | sh | curl |
| [gpg\*](gpg\*) | Encrypt/decrypt files using GPG [✓] | sh | gpg | | [gpg\*](gpg\*) | Encrypt/decrypt files using GPG [✓] | sh | gpg |
@ -65,6 +66,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
| [togglex](togglex) | Toggle executable mode for selection [✓] | sh | chmod | | [togglex](togglex) | Toggle executable mode for selection [✓] | sh | chmod |
| [treeview](treeview) | Informative tree output in `$EDITOR` | sh | tree | | [treeview](treeview) | Informative tree output in `$EDITOR` | sh | tree |
| [uidgid](uidgid) | List user and group of all files in dir | sh | ls, less | | [uidgid](uidgid) | List user and group of all files in dir | sh | ls, less |
| [unmount-parent](unmount-parent) | Unmount a remote mountpoint from within | sh | fusermount |
| [upgrade](upgrade) | Upgrade nnn manually on Debian 9 Stretch | sh | curl | | [upgrade](upgrade) | Upgrade nnn manually on Debian 9 Stretch | sh | curl |
| [upload](upload) | Upload to Firefox Send or ix.io (text) or file.io (bin) | sh | [ffsend](https://github.com/timvisee/ffsend), curl, jq, tr | | [upload](upload) | Upload to Firefox Send or ix.io (text) or file.io (bin) | sh | [ffsend](https://github.com/timvisee/ffsend), curl, jq, tr |
| [vidthumb](vidthumb) | Show video thumbnails in terminal | sh | [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer),<br>[lsix](https://github.com/hackerb9/lsix) | | [vidthumb](vidthumb) | Show video thumbnails in terminal | sh | [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer),<br>[lsix](https://github.com/hackerb9/lsix) |

49
plugins/fzplug Executable file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env sh
# Description: Fuzzy find and execute nnn plugins (and, optionally, custom scripts located elsewhere).
# Description and details of plugins can be previewed from the fzf interface. Use `?` to toggle preview
# pane on and off.
#
# For better compatibility with as many nnn plugins as possible, fzfplug will first execute
# the chosen script on the file hovered in nnn, and upon failure, try to run it with no target
# (which should actually run it on selected files if nnn has an active selection). I don't
# have the required dependencies to confirm compatibility with all scripts though.
#
# Dependencies: find, fzf, cat (or bat, if installed)
# Shell: POSIX compliant
# Author: Kabouik
# OPTIONAL SCRIPTS SOURCES
# Leave blank or fill with the absolute path of a folder containing executable scripts other than nnn plugins
# (e.g., "$HOME/.local/share/nautilus/scripts", since there are numerous Nautilus script git repositories).
# Add extra variables if need be, but be sure to call them in the find command below at lines 28:49 and 30:49.
#CUSTOMDIR1="$HOME/.local/share/nautilus/scripts"
CUSTOMDIR1=""
CUSTOMDIR2=""
# REQUIRED VARIABLES
nnnpluginsdir="$HOME/.config/nnn/plugins"
# PREVIEW WITH bat INSTEAD OF cat IF INSTALLED
if [ -z "$(command -v bat)" ]; then
plugin=$(find "$nnnpluginsdir" "$CUSTOMDIR1" "$CUSTOMDIR2" -maxdepth 3 -perm -111 -type f 2>/dev/null | fzf --ansi --preview 'cat {}' --preview-window right:66% --delimiter / --with-nth -1 --bind="?:toggle-preview")
else
plugin=$(find "$nnnpluginsdir" "$CUSTOMDIR1" "$CUSTOMDIR2" -maxdepth 3 -perm -111 -type f 2>/dev/null | fzf --ansi --preview 'bat --color=always --style=grid {}' --preview-window right:66% --delimiter / --with-nth -1 --bind="?:toggle-preview")
fi
# TRY RUNNING THE SCRIPT ON HOVERED FILE FIRST, AND ABORT IF NO PLUGIN WAS SELECTED IN FZFPLUG (ESC OR ^C),
err=0
if ! [ "$plugin" = "" ]; then
"$plugin" "$1" || err=1
fi
# IF THAT FAILS WITH HOVERED FILE, TRY WITH NO TARGET (nnn SELECTIONS SHOULD STILL BE PASSED TO THE SCRIPT IN THAT CASE)
if [ "$err" -eq "1" ]; then
clear && "$plugin" || err=2
fi
# IF THAT FAILS TOO, ABORT AND SHOW AN ERROR
if [ "$err" -eq "2" ]; then
sep="\n---\n"
printf "$sep""Failed to execute '%s'. See error above or try without fzfplug. Press return to continue. " "$plugin" && read -r _ && clear
fi

48
plugins/unmount-parent Executable file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env sh
# Description: Autodetects a nnn remote mountpoint (mounted with `c`) from any of its subfolders and allows unmounting it
# from there without first going back to the top or entering the remote name. Also works when hovering the mountpoint
# directly like vanilla `u`.
#
# Dependencies: fusermount
# Shell: POSIX compliant
# Authors: Kabouik & 0xACE
#
# TODO: Try better avoiding lazy unmount by forcing nnn context to leave the subfolder before fusermount.
# I tried `printf "%s" "0c$m" > "$NNN_PIPE"` but this would break nnn UI all the time, see #854.
# ENVIRONMENT
err=0
m=$HOME/.config/nnn/mounts
if [ "$PWD" = "$m" ]; then # ALLOW USING THE SCRIPT ON HOVERED DIRECTORY IF USER IS IN ~/.config/nnn/mounts
d="$1"
else
d=$(dirname "$(readlink -f "$1")" | grep -oP "^$m\K.*" | cut -d"/" -f2)
fi
# TEST IF USER IS CURRENTLY WITHIN $m OR A SUBFOLDER, ABORT IF NOT
if [ "$d" = "" ]; then
clear && printf "You are not in a remote folder mounted with nnn. Press return to continue. " && read -r _
else
# TEST IF $m/$d IS A MOUNTPOINT AND TRY UNMOUNTING IF YES
mountpoint -q -- "$m/$d"
if [ "$?" -eq "1" ]; then
clear && printf "Parent '%s' is not a mountpoint. Press return to continue. " "$d" && read -r _
else
cd "$m" && fusermount -uq "$m/$d" || err=1
if [ "$err" -eq "0" ]; then
rmdir "$m/$d" && clear && printf "Parent '%s' unmounted." "$d"
else
clear && printf "Failed to unmount. Try lazy unmount? [Yy/Nn] " && read -r
fi
fi
fi
# IF FAILURE TO UNMOUNT, OFFER TO TRY LAZY UNMOUNT
if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
err=0
cd "$m" && fusermount -uqz "$m/$d" || err=1
if [ "$err" -eq "0" ]; then
rmdir "$m/$d" && clear && printf "Parent '%s' unmounted with lazy unmount. " "$d"
fi
fi