2021-04-30 23:59:10 +00:00
|
|
|
#!/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
|
2021-05-14 14:29:43 +00:00
|
|
|
# pane on and off, ^Up/^Dn to scroll.
|
2021-04-30 23:59:10 +00:00
|
|
|
#
|
2021-05-14 14:29:43 +00:00
|
|
|
# For better compatibility with as many nnn plugins as possible, fzplug will first execute
|
2021-04-30 23:59:10 +00:00
|
|
|
# the chosen script on the file hovered in nnn, and upon failure, try to run it with no target
|
2021-05-14 12:03:28 +00:00
|
|
|
# (i.e on an active selection, if present).
|
2021-04-30 23:59:10 +00:00
|
|
|
#
|
|
|
|
# Dependencies: find, fzf, cat (or bat, if installed)
|
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Author: Kabouik
|
|
|
|
|
2021-05-14 12:03:28 +00:00
|
|
|
# Optional scripts sources
|
2021-04-30 23:59:10 +00:00
|
|
|
# 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).
|
2021-05-14 14:29:43 +00:00
|
|
|
# Add extra variables if need be, but be sure to call them in the find command below at lines 27 and 33.
|
2021-04-30 23:59:10 +00:00
|
|
|
#CUSTOMDIR1="$HOME/.local/share/nautilus/scripts"
|
|
|
|
CUSTOMDIR1=""
|
|
|
|
CUSTOMDIR2=""
|
|
|
|
|
|
|
|
nnnpluginsdir="$HOME/.config/nnn/plugins"
|
|
|
|
|
2021-05-14 12:03:28 +00:00
|
|
|
# Preview with bat if installed
|
2021-04-30 23:59:10 +00:00
|
|
|
if [ -z "$(command -v bat)" ]; then
|
2021-05-14 14:29:43 +00:00
|
|
|
plugin=$(find "$nnnpluginsdir" "$CUSTOMDIR1" "$CUSTOMDIR2" \
|
|
|
|
-maxdepth 3 -perm -111 -type f 2>/dev/null | fzf --ansi --preview \
|
|
|
|
"cat {}" \
|
|
|
|
--preview-window right:66%:wrap --delimiter / --with-nth -1 \
|
|
|
|
--bind="?:toggle-preview")
|
2021-04-30 23:59:10 +00:00
|
|
|
else
|
2021-05-14 14:29:43 +00:00
|
|
|
plugin=$(find "$nnnpluginsdir" "$CUSTOMDIR1" "$CUSTOMDIR2" \
|
|
|
|
-maxdepth 3 -perm -111 -type f 2>/dev/null | fzf --ansi --preview \
|
|
|
|
"bat --terminal-width='$(tput cols)' --decorations=always --color=always \
|
|
|
|
--style='${BAT_STYLE:-header,numbers}' {}" \
|
|
|
|
--preview-window right:66% --delimiter / --with-nth -1 \
|
|
|
|
--bind="?:toggle-preview")
|
2021-04-30 23:59:10 +00:00
|
|
|
fi
|
|
|
|
|
2021-05-14 12:03:28 +00:00
|
|
|
# Try running the script on the hovered file, and abort if no plugin was selected (ESC or ^C pressed),
|
2021-04-30 23:59:10 +00:00
|
|
|
err=0
|
|
|
|
if ! [ "$plugin" = "" ]; then
|
|
|
|
"$plugin" "$1" || err=1
|
|
|
|
fi
|
|
|
|
|
2021-05-14 12:03:28 +00:00
|
|
|
# If attempt with hovered file fails, try without any target
|
|
|
|
# (nnn selections should still be passed to the script int hat case)
|
2021-04-30 23:59:10 +00:00
|
|
|
if [ "$err" -eq "1" ]; then
|
|
|
|
clear && "$plugin" || err=2
|
|
|
|
fi
|
|
|
|
|
2021-05-14 12:03:28 +00:00
|
|
|
# Abort and show error if both fail
|
2021-04-30 23:59:10 +00:00
|
|
|
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
|