]> Sergey Matveev's repositories - nnn.git/blob - plugins/fzplug
Clear less'es screen
[nnn.git] / plugins / fzplug
1 #!/usr/bin/env sh
2
3 # Description: Fuzzy find and execute nnn plugins (and optionally,
4 #              custom scripts located elsewhere).
5 #              Description and details of plugins can be previewed
6 #              from the fzf interface. Use `?` to toggle preview
7 #              pane on and off, ^Up/^Dn to scroll.
8 #
9 # Dependencies: find, fzf, cat (or bat, if installed)
10 #
11 # Note: For better compatibility with as many nnn plugins as possible,
12 #       fzplug will first execute the chosen script on the file hovered
13 #       in nnn, and upon failure, try to run it with no target (i.e on
14 #       an active selection, if present).
15 #
16 # Shell: POSIX compliant
17 # Author: Kabouik
18
19 # Optional scripts sources
20
21 # Leave blank or fill with the absolute path of a folder containing executable
22 # scripts other than nnn plugins (e.g., "$HOME/.local/share/nautilus/scripts",
23 # since there are numerous Nautilus script git repositories).
24 # Add extra variables if needed, make sure you call them in the find command.
25
26 #CUSTOMDIR1="$HOME/.local/share/nautilus/scripts"
27 CUSTOMDIR1=""
28 CUSTOMDIR2=""
29
30 nnnpluginsdir="$HOME/.config/nnn/plugins"
31
32 # Preview with bat if installed
33 if type bat >/dev/null; then
34     BAT="bat --terminal-width='$(tput cols)' --decorations=always --color=always --style='${BAT_STYLE:-header,numbers}'"
35 fi
36
37 plugin=$(find "$nnnpluginsdir" "$CUSTOMDIR1" "$CUSTOMDIR2" \
38 -maxdepth 3 -perm -111 -type f 2>/dev/null | fzf --ansi --preview \
39     "${BAT:-cat} {}" --preview-window="right:66%:wrap" --delimiter / \
40     --with-nth -1 --bind="?:toggle-preview")
41
42 # Try running the script on the hovered file, and abort
43 # abort if no plugin was selected (ESC or ^C pressed).
44 err=0
45 if ! [ "$plugin" = "" ]; then
46     "$plugin" "$1" || err=1
47 fi
48
49 # If attempt with hovered file fails, try without any target
50 # (nnn selections should still be passed to the script in that case)
51 if [ "$err" -eq "1" ]; then
52     clear && "$plugin" || err=2
53 fi
54
55 # Abort and show error if both fail
56 if [ "$err" -eq "2" ]; then
57     sep="\n---\n"
58     printf "$sep""Failed to execute '%s'. See error above or try without fzfplug. Press return to continue. " "$plugin" && read -r _ && clear
59 fi