Skip to content

Commit

Permalink
Replace which with builtin type in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jarun committed May 14, 2021
1 parent 513b4f2 commit f343f31
Show file tree
Hide file tree
Showing 29 changed files with 137 additions and 161 deletions.
16 changes: 8 additions & 8 deletions plugins/.cbcp
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # protect trailing \n

selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}

if which xsel >/dev/null 2>&1; then
if type xsel >/dev/null 2>&1; then
# Linux
tr '\0' '\n' < "$selection" | xsel -bi
elif which xclip >/dev/null 2>&1; then
elif type xclip >/dev/null 2>&1; then
# Linux
tr '\0' '\n' < "$selection" | xclip -sel clip
elif which pbcopy >/dev/null 2>&1; then
elif type pbcopy >/dev/null 2>&1; then
# macOS
tr '\0' '\n' < "$selection" | pbcopy
elif which termux-clipboard-set >/dev/null 2>&1; then
elif type termux-clipboard-set >/dev/null 2>&1; then
# Termux
tr '\0' '\n' < "$selection" | termux-clipboard-set
elif which clip.exe >/dev/null 2>&1; then
elif type clip.exe >/dev/null 2>&1; then
# WSL
tr '\0' '\n' < "$selection" | clip.exe
elif which clip >/dev/null 2>&1; then
elif type clip >/dev/null 2>&1; then
# Cygwin
tr '\0' '\n' < "$selection" | clip
elif which wl-copy >/dev/null 2>&1; then
elif type wl-copy >/dev/null 2>&1; then
# Wayland
tr '\0' '\n' < "$selection" | wl-copy
elif which clipboard >/dev/null 2>&1; then
elif type clipboard >/dev/null 2>&1; then
# Haiku
tr '\0' '\n' < "$selection" | clipboard --stdin
fi
2 changes: 1 addition & 1 deletion plugins/.nnn-plugin-helper
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ nnn_cd () {
}

cmd_exists () {
which "$1" > /dev/null 2>&1
type "$1" > /dev/null 2>&1
echo $?
}
4 changes: 2 additions & 2 deletions plugins/.ntfy
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

OS="$(uname)"

if which notify-send >/dev/null 2>&1; then
if type notify-send >/dev/null 2>&1; then
notify-send nnn "Done!"
elif [ "$OS" = "Darwin" ]; then
osascript -e 'display notification "Done!" with title "nnn"'
elif which ntfy >/dev/null 2>&1; then
elif type ntfy >/dev/null 2>&1; then
ntfy -t nnn send "Done!"
elif [ "$OS" = "Haiku" ]; then
notify --title "nnn" "Done!"
Expand Down
4 changes: 2 additions & 2 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ There are many plugins provided by `nnn` which can be used as examples. Here are
printf "pattern: "
read -r pattern

if ! [ -z "$pattern" ]; then
if [ -n "$pattern" ]; then
printf "%s" "+l" > "$NNN_PIPE"
eval "fd -HI $pattern -0" > "$NNN_PIPE"
fi
Expand All @@ -306,7 +306,7 @@ There are many plugins provided by `nnn` which can be used as examples. Here are
printf "pattern: "
read -r pattern

if ! [ -z "$pattern" ]; then
if [ -n "$pattern" ]; then
printf "%s" "+l" > "$NNN_PIPE"
eval "rg -l0 --hidden -S $pattern" > "$NNN_PIPE"
fi
Expand Down
8 changes: 4 additions & 4 deletions plugins/autojump
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ if [ ! -p "$NNN_PIPE" ]; then
exit 2
fi

if which jump >/dev/null 2>&1; then
if type jump >/dev/null 2>&1; then
printf "jump to : "
read -r dir
odir="$(jump cd "$dir")"
printf "%s" "0c$odir" > "$NNN_PIPE"
elif which autojump >/dev/null 2>&1; then
elif type autojump >/dev/null 2>&1; then
printf "jump to : "
read -r dir
odir="$(autojump "$dir")"
printf "%s" "0c$odir" > "$NNN_PIPE"
elif which zoxide >/dev/null 2>&1; then
if which fzf >/dev/null 2>&1; then
elif type zoxide >/dev/null 2>&1; then
if type fzf >/dev/null 2>&1; then
odir="$(zoxide query -i --)"
printf "%s" "0c$odir" > "$NNN_PIPE"
else
Expand Down
2 changes: 1 addition & 1 deletion plugins/boom
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if [ -n "$GUIPLAYER" ]; then

# detach the player
sleep 1
elif which mocp >/dev/null 2>&1; then
elif type mocp >/dev/null 2>&1; then
cmd=$(pgrep -x mocp 2>/dev/null)
ret=$cmd

Expand Down
4 changes: 2 additions & 2 deletions plugins/cleanfilename
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Description: Clean filename or dirname (either hovered or selections)
# to be more shell-friendly. This script cleans
# any character which is not A-Za-z0-9._-
# non A-Za-z0-9._- characters.
# and replaces it with underscore (_).
#
# It supports cleaning single/double quote, newline,
Expand All @@ -15,7 +15,7 @@
# qwe\trty -> __qwe_rty
#
# And if there are two almost similar filenames
# like: 'asd]f' and 'asd f' which both will be renamed to 'asd_f',
# like: 'asd]f' and 'asd f' both will be renamed to 'asd_f',
# to avoid overwriting, the last file will be prepended by _.
# So they will be: 'asd_f' and '_asd_f'
#
Expand Down
2 changes: 1 addition & 1 deletion plugins/diffs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}

if which nvim >/dev/null 2>&1; then
if type nvim >/dev/null 2>&1; then
diffcmd="nvim -d"
else
diffcmd="vimdiff +0"
Expand Down
2 changes: 1 addition & 1 deletion plugins/dragdrop
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
resp=f
all=
if which dragon-drag-and-drop >/dev/null 2>&1; then
if type dragon-drag-and-drop >/dev/null 2>&1; then
dnd="dragon-drag-and-drop"
else
dnd="dragon"
Expand Down
2 changes: 1 addition & 1 deletion plugins/fzhist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Shell: POSIX compliant
# Author: Arun Prakash Jana

if which fzf >/dev/null 2>&1; then
if type fzf >/dev/null 2>&1; then
fuzzy=fzf
else
exit 1
Expand Down
6 changes: 3 additions & 3 deletions plugins/fzopen
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
# Shell: POSIX compliant
# Author: Arun Prakash Jana

if which fzf >/dev/null 2>&1; then
if type fzf >/dev/null 2>&1; then
cmd="$FZF_DEFAULT_COMMAND"
if which fd >/dev/null 2>&1; then
if type fd >/dev/null 2>&1; then
[ -z "$cmd" ] && cmd="fd -t f 2>/dev/null"
else
[ -z "$cmd" ] && cmd="find . -type f 2>/dev/null"
fi
entry="$(eval "$cmd" | fzf --delimiter / --nth=-1 --tiebreak=begin --info=hidden)"
# To show only the file name
# entry=$(find . -type f 2>/dev/null | fzf --delimiter / --with-nth=-1 --tiebreak=begin --info=hidden)
elif which sk >/dev/null 2>&1; then
elif type sk >/dev/null 2>&1; then
entry=$(find . -type f 2>/dev/null | sk)
else
exit 1
Expand Down
15 changes: 7 additions & 8 deletions plugins/fzplug
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,42 @@
#
# 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.
# (i.e on an active selection, if present).
#
# Dependencies: find, fzf, cat (or bat, if installed)
# Shell: POSIX compliant
# Author: Kabouik

# OPTIONAL SCRIPTS SOURCES
# 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
# Preview with bat 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),
# Try running the script on the hovered file, and abort if no plugin was selected (ESC or ^C pressed),
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 attempt with hovered file fails, try without any target
# (nnn selections should still be passed to the script int hat case)
if [ "$err" -eq "1" ]; then
clear && "$plugin" || err=2
fi

# IF THAT FAILS TOO, ABORT AND SHOW AN ERROR
# Abort and show error if both fail
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
Expand Down
2 changes: 1 addition & 1 deletion plugins/fzz
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

. "$(dirname "$0")"/.nnn-plugin-helper

if which fzf >/dev/null 2>&1; then
if type fzf >/dev/null 2>&1; then
fuzzy=fzf
else
exit 1
Expand Down
17 changes: 2 additions & 15 deletions plugins/getplugs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
CONFIG_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/
PLUGIN_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins

# is_cmd_exists () {
# which "$1" > /dev/null 2>&1
# echo $?
# }

merge () {
if which nvim >/dev/null 2>&1; then
if type nvim >/dev/null 2>&1; then
nvim -d "$1" "$2"
else
vimdiff +0 "$1" "$2"
Expand All @@ -35,18 +30,10 @@ prompt () {
fi
}

# if [ "$(is_cmd_exists sudo)" -eq "0" ]; then
# sucmd=sudo
# elif [ "$(is_cmd_exists doas)" -eq "0" ]; then
# sucmd=doas
# else
# sucmd=: # noop
# fi

if [ "$1" = "master" ] ; then
VER="master"
ARCHIVE_URL=https://github.com/jarun/nnn/archive/master.tar.gz
elif which nnn >/dev/null 2>&1; then
elif type nnn >/dev/null 2>&1; then
VER=$(nnn -V)
ARCHIVE_URL=https://github.com/jarun/nnn/releases/download/v"$VER"/nnn-v"$VER".tar.gz
else
Expand Down
2 changes: 1 addition & 1 deletion plugins/hexview
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Author: Arun Prakash Jana

if [ -n "$1" ]; then
if which hx >/dev/null 2>&1; then
if type hx >/dev/null 2>&1; then
hx "$1"
else
xxd "$1" | $PAGER
Expand Down
20 changes: 10 additions & 10 deletions plugins/imgur
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,31 @@ fi

# dependency check
if [ "${1}" = "--check" ]; then
(which grep &>/dev/null && echo "OK: found grep") || echo "ERROR: grep not found"
(type grep &>/dev/null && echo "OK: found grep") || echo "ERROR: grep not found"
if is_mac; then
if which growlnotify &>/dev/null; then
if type growlnotify &>/dev/null; then
echo "OK: found growlnotify"
elif which terminal-notifier &>/dev/null; then
elif type terminal-notifier &>/dev/null; then
echo "OK: found terminal-notifier"
else
echo "ERROR: growlnotify nor terminal-notifier found"
fi
(which screencapture &>/dev/null && echo "OK: found screencapture") || echo "ERROR: screencapture not found"
(which pbcopy &>/dev/null && echo "OK: found pbcopy") || echo "ERROR: pbcopy not found"
(type screencapture &>/dev/null && echo "OK: found screencapture") || echo "ERROR: screencapture not found"
(type pbcopy &>/dev/null && echo "OK: found pbcopy") || echo "ERROR: pbcopy not found"
else
(which notify-send &>/dev/null && echo "OK: found notify-send") || echo "ERROR: notify-send (from libnotify-bin) not found"
(which scrot &>/dev/null && echo "OK: found scrot") || echo "ERROR: scrot not found"
(which xclip &>/dev/null && echo "OK: found xclip") || echo "ERROR: xclip not found"
(type notify-send &>/dev/null && echo "OK: found notify-send") || echo "ERROR: notify-send (from libnotify-bin) not found"
(type scrot &>/dev/null && echo "OK: found scrot") || echo "ERROR: scrot not found"
(type xclip &>/dev/null && echo "OK: found xclip") || echo "ERROR: xclip not found"
fi
(which curl &>/dev/null && echo "OK: found curl") || echo "ERROR: curl not found"
(type curl &>/dev/null && echo "OK: found curl") || echo "ERROR: curl not found"
exit 0
fi


# notify <'ok'|'error'> <title> <text>
function notify() {
if is_mac; then
if which growlnotify &>/dev/null; then
if type growlnotify &>/dev/null; then
growlnotify --icon "${imgur_icon_path}" --iconpath "${imgur_icon_path}" --title "${2}" --message "${3}"
else
terminal-notifier -appIcon "${imgur_icon_path}" -contentImage "${imgur_icon_path}" -title "imgur: ${2}" -message "${3}"
Expand Down
6 changes: 3 additions & 3 deletions plugins/imgview
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ if uname | grep -q "Darwin"; then
fi
# `imvr` is often callable as `imv` on Linux distros
# You might need to change the reference below
elif which imvr >/dev/null 2>&1; then
elif type imvr >/dev/null 2>&1; then
if [ -f "$1" ]; then
view_dir imvr "$1" >/dev/null 2>&1 &
elif [ -d "$1" ] || [ -h "$1" ]; then
imvr "$1" >/dev/null 2>&1 &
fi
elif which sxiv >/dev/null 2>&1; then
elif type sxiv >/dev/null 2>&1; then
if [ -f "$1" ]; then
view_dir sxiv "$1" >/dev/null 2>&1 &
elif [ -d "$1" ] || [ -h "$1" ]; then
sxiv -aqt "$1" >/dev/null 2>&1 &
fi
elif which viu >/dev/null 2>&1; then
elif type viu >/dev/null 2>&1; then
viu -n "$1" | less -R
else
printf "Please install imv/sxiv/viu and check their callable names match the plugin source"
Expand Down
2 changes: 1 addition & 1 deletion plugins/launch
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
IFS=':'

get_selection() {
if which fzf >/dev/null 2>&1; then
if type fzf >/dev/null 2>&1; then
{ IFS=':'; ls -H $PATH; } | sort | fzf
else
exit 1
Expand Down

0 comments on commit f343f31

Please sign in to comment.