nnn/plugins/fzplug
Kabouik c2aeb51bcc
[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 <>
2021-05-01 06:23:33 +05:30

50 lines
2.3 KiB
Bash
Executable file

#!/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