diff --git a/plugins/.cbcp b/plugins/.cbcp index 04a09dc7..3a71927e 100755 --- a/plugins/.cbcp +++ b/plugins/.cbcp @@ -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 diff --git a/plugins/.nnn-plugin-helper b/plugins/.nnn-plugin-helper index bcb9649f..4738c699 100644 --- a/plugins/.nnn-plugin-helper +++ b/plugins/.nnn-plugin-helper @@ -33,6 +33,6 @@ nnn_cd () { } cmd_exists () { - which "$1" > /dev/null 2>&1 + type "$1" > /dev/null 2>&1 echo $? } diff --git a/plugins/.ntfy b/plugins/.ntfy index 3a7f27fa..2a614781 100755 --- a/plugins/.ntfy +++ b/plugins/.ntfy @@ -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!" diff --git a/plugins/README.md b/plugins/README.md index cdf7445d..7e725427 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -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 @@ -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 diff --git a/plugins/autojump b/plugins/autojump index 8f058d0b..0b842532 100755 --- a/plugins/autojump +++ b/plugins/autojump @@ -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 diff --git a/plugins/boom b/plugins/boom index 10f14f60..39da84c2 100755 --- a/plugins/boom +++ b/plugins/boom @@ -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 diff --git a/plugins/cleanfilename b/plugins/cleanfilename index ce573fab..40471520 100755 --- a/plugins/cleanfilename +++ b/plugins/cleanfilename @@ -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, @@ -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' # diff --git a/plugins/diffs b/plugins/diffs index dea3a4cd..1028f4e7 100755 --- a/plugins/diffs +++ b/plugins/diffs @@ -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" diff --git a/plugins/dragdrop b/plugins/dragdrop index a69ed841..ef352da7 100755 --- a/plugins/dragdrop +++ b/plugins/dragdrop @@ -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" diff --git a/plugins/fzhist b/plugins/fzhist index e824fc2f..2a801f08 100755 --- a/plugins/fzhist +++ b/plugins/fzhist @@ -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 diff --git a/plugins/fzopen b/plugins/fzopen index fa44e0fc..63f2e2f9 100755 --- a/plugins/fzopen +++ b/plugins/fzopen @@ -9,9 +9,9 @@ # 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" @@ -19,7 +19,7 @@ if which fzf >/dev/null 2>&1; then 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 diff --git a/plugins/fzplug b/plugins/fzplug index 50b485b6..b945e0c6 100755 --- a/plugins/fzplug +++ b/plugins/fzplug @@ -6,14 +6,13 @@ # # 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. @@ -21,28 +20,28 @@ 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 diff --git a/plugins/fzz b/plugins/fzz index 049cf9dc..a79cee0a 100755 --- a/plugins/fzz +++ b/plugins/fzz @@ -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 diff --git a/plugins/getplugs b/plugins/getplugs index d8750746..361a605f 100755 --- a/plugins/getplugs +++ b/plugins/getplugs @@ -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" @@ -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 diff --git a/plugins/hexview b/plugins/hexview index 22936bc8..ad6e97fa 100755 --- a/plugins/hexview +++ b/plugins/hexview @@ -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 diff --git a/plugins/imgur b/plugins/imgur index 2da549c6..c6a46ae1 100755 --- a/plugins/imgur +++ b/plugins/imgur @@ -117,23 +117,23 @@ 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 @@ -141,7 +141,7 @@ fi # notify <'ok'|'error'> <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}" diff --git a/plugins/imgview b/plugins/imgview index 42f60cde..9a308322 100755 --- a/plugins/imgview +++ b/plugins/imgview @@ -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" diff --git a/plugins/launch b/plugins/launch index c131235e..d666cc56 100755 --- a/plugins/launch +++ b/plugins/launch @@ -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 diff --git a/plugins/nuke b/plugins/nuke index 10d478cd..1018a621 100755 --- a/plugins/nuke +++ b/plugins/nuke @@ -91,36 +91,36 @@ handle_pdf() { if [ "$GUI" -ne 0 ] && is_mac; then nohup open "${FPATH}" >/dev/null 2>&1 & exit 0 - elif [ "$GUI" -ne 0 ] && which zathura >/dev/null 2>&1; then + elif [ "$GUI" -ne 0 ] && type zathura >/dev/null 2>&1; then nohup zathura "${FPATH}" >/dev/null 2>&1 & exit 0 - elif which pdftotext >/dev/null 2>&1; then + elif type pdftotext >/dev/null 2>&1; then ## Preview as text conversion pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER" exit 0 - elif which mutool >/dev/null 2>&1; then + elif type mutool >/dev/null 2>&1; then mutool draw -F txt -i -- "${FPATH}" 1-10 | eval "$PAGER" exit 0 - elif which exiftool >/dev/null 2>&1; then + elif type exiftool >/dev/null 2>&1; then exiftool "${FPATH}" | eval "$PAGER" exit 0 fi } handle_audio() { - if which mocp >/dev/null 2>&1 && which mocplay >/dev/null 2>&1; then + if type mocp >/dev/null 2>&1 && type mocplay >/dev/null 2>&1; then mocplay "${FPATH}" "opener" >/dev/null 2>&1 exit 0 - elif which mpv >/dev/null 2>&1; then + elif type mpv >/dev/null 2>&1; then mpv "${FPATH}" >/dev/null 2>&1 & exit 0 - elif which media_client >/dev/null 2>&1; then + elif type media_client >/dev/null 2>&1; then media_client play "${FPATH}" >/dev/null 2>&1 & exit 0 - elif which mediainfo >/dev/null 2>&1; then + elif type mediainfo >/dev/null 2>&1; then mediainfo "${FPATH}" | eval "$PAGER" exit 0 - elif which exiftool >/dev/null 2>&1; then + elif type exiftool >/dev/null 2>&1; then exiftool "${FPATH}"| eval "$PAGER" exit 0 fi @@ -130,22 +130,22 @@ handle_video() { if [ "$GUI" -ne 0 ] && is_mac; then nohup open "${FPATH}" >/dev/null 2>&1 & exit 0 - elif [ "$GUI" -ne 0 ] && which smplayer >/dev/null 2>&1; then + elif [ "$GUI" -ne 0 ] && type smplayer >/dev/null 2>&1; then nohup smplayer "${FPATH}" >/dev/null 2>&1 & exit 0 - elif [ "$GUI" -ne 0 ] && which mpv >/dev/null 2>&1; then + elif [ "$GUI" -ne 0 ] && type mpv >/dev/null 2>&1; then nohup mpv "${FPATH}" >/dev/null 2>&1 & exit 0 - elif which ffmpegthumbnailer >/dev/null 2>&1; then + elif type ffmpegthumbnailer >/dev/null 2>&1; then # Thumbnail [ -d "${IMAGE_CACHE_PATH}" ] || mkdir "${IMAGE_CACHE_PATH}" ffmpegthumbnailer -i "${FPATH}" -o "${IMAGE_CACHE_PATH}/${FNAME}.jpg" -s 0 viu -n "${IMAGE_CACHE_PATH}/${FNAME}.jpg" | eval "$PAGER" exit 0 - elif which mediainfo >/dev/null 2>&1; then + elif type mediainfo >/dev/null 2>&1; then mediainfo "${FPATH}" | eval "$PAGER" exit 0 - elif which exiftool >/dev/null 2>&1; then + elif type exiftool >/dev/null 2>&1; then exiftool "${FPATH}"| eval "$PAGER" exit 0 fi @@ -157,22 +157,22 @@ handle_extension() { ## Archive a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\ rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip) - if which atool >/dev/null 2>&1; then + if type atool >/dev/null 2>&1; then atool --list -- "${FPATH}" | eval "$PAGER" exit 0 - elif which bsdtar >/dev/null 2>&1; then + elif type bsdtar >/dev/null 2>&1; then bsdtar --list --file "${FPATH}" | eval "$PAGER" exit 0 fi exit 1;; rar) - if which unrar >/dev/null 2>&1; then + if type unrar >/dev/null 2>&1; then ## Avoid password prompt by providing empty password unrar lt -p- -- "${FPATH}" | eval "$PAGER" fi exit 1;; 7z) - if which 7z >/dev/null 2>&1; then + if type 7z >/dev/null 2>&1; then ## Avoid password prompt by providing empty password 7z l -p -- "${FPATH}" | eval "$PAGER" exit 0 @@ -201,10 +201,10 @@ handle_extension() { ## BitTorrent torrent) - if which rtorrent >/dev/null 2>&1; then + if type rtorrent >/dev/null 2>&1; then rtorrent "${FPATH}" exit 0 - elif which transmission-show >/dev/null 2>&1; then + elif type transmission-show >/dev/null 2>&1; then transmission-show -- "${FPATH}" exit 0 fi @@ -212,7 +212,7 @@ handle_extension() { ## OpenDocument odt|ods|odp|sxw) - if which odt2txt >/dev/null 2>&1; then + if type odt2txt >/dev/null 2>&1; then ## Preview as text conversion odt2txt "${FPATH}" | eval "$PAGER" exit 0 @@ -221,10 +221,10 @@ handle_extension() { ## Markdown md) - if which glow >/dev/null 2>&1; then + if type glow >/dev/null 2>&1; then glow -sdark "${FPATH}" | eval "$PAGER" exit 0 - elif which lowdown >/dev/null 2>&1; then + elif type lowdown >/dev/null 2>&1; then lowdown -Tterm "${FPATH}" | eval "$PAGER" exit 0 fi @@ -233,13 +233,13 @@ handle_extension() { ## HTML htm|html|xhtml) ## Preview as text conversion - if which w3m >/dev/null 2>&1; then + if type w3m >/dev/null 2>&1; then w3m -dump "${FPATH}" | eval "$PAGER" exit 0 - elif which lynx >/dev/null 2>&1; then + elif type lynx >/dev/null 2>&1; then lynx -dump -- "${FPATH}" | eval "$PAGER" exit 0 - elif which elinks >/dev/null 2>&1; then + elif type elinks >/dev/null 2>&1; then elinks -dump "${FPATH}" | eval "$PAGER" exit 0 fi @@ -247,10 +247,10 @@ handle_extension() { ## JSON json) - if which jq >/dev/null 2>&1; then + if type jq >/dev/null 2>&1; then jq --color-output . "${FPATH}" | eval "$PAGER" exit 0 - elif which python >/dev/null 2>&1; then + elif type python >/dev/null 2>&1; then python -m json.tool -- "${FPATH}" | eval "$PAGER" exit 0 fi @@ -311,19 +311,19 @@ handle_multimedia() { if [ "$GUI" -ne 0 ] && is_mac; then nohup open "${FPATH}" >/dev/null 2>&1 & exit 0 - elif [ "$GUI" -ne 0 ] && which imvr >/dev/null 2>&1; then + elif [ "$GUI" -ne 0 ] && type imvr >/dev/null 2>&1; then load_dir imvr "${FPATH}" >/dev/null 2>&1 & exit 0 - elif [ "$GUI" -ne 0 ] && which sxiv >/dev/null 2>&1; then + elif [ "$GUI" -ne 0 ] && type sxiv >/dev/null 2>&1; then load_dir sxiv "${FPATH}" >/dev/null 2>&1 & exit 0 - elif which viu >/dev/null 2>&1; then + elif type viu >/dev/null 2>&1; then viu -n "${FPATH}" | eval "$PAGER" exit 0 - elif which img2txt >/dev/null 2>&1; then + elif type img2txt >/dev/null 2>&1; then img2txt --gamma=0.6 -- "${FPATH}" | eval "$PAGER" exit 0 - elif which exiftool >/dev/null 2>&1; then + elif type exiftool >/dev/null 2>&1; then exiftool "${FPATH}" | eval "$PAGER" exit 0 fi @@ -466,11 +466,11 @@ handle_mime() { ## DjVu image/vnd.djvu) - if which djvutxt >/dev/null 2>&1; then + if type djvutxt >/dev/null 2>&1; then ## Preview as text conversion (requires djvulibre) djvutxt "${FPATH}" | eval "$PAGER" exit 0 - elif which exiftool >/dev/null 2>&1; then + elif type exiftool >/dev/null 2>&1; then exiftool "${FPATH}" | eval "$PAGER" exit 0 fi @@ -479,10 +479,10 @@ handle_mime() { } handle_fallback() { - if [ "$GUI" -ne 0 ] && which xdg-open >/dev/null 2>&1; then + if [ "$GUI" -ne 0 ] && type xdg-open >/dev/null 2>&1; then nohup xdg-open "${FPATH}" >/dev/null 2>&1 & exit 0 - elif [ "$GUI" -ne 0 ] && which open >/dev/null 2>&1; then + elif [ "$GUI" -ne 0 ] && type open >/dev/null 2>&1; then nohup open "${FPATH}" >/dev/null 2>&1 & exit 0 fi diff --git a/plugins/preview-tabbed b/plugins/preview-tabbed index 43cb40bb..0fcb3148 100755 --- a/plugins/preview-tabbed +++ b/plugins/preview-tabbed @@ -54,11 +54,11 @@ PAGER=${PAGER:-"vim -R"} NUKE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke" -if which xterm >/dev/null 2>&1 ; then +if type xterm >/dev/null 2>&1 ; then TERMINAL="xterm -into" -elif which urxvt >/dev/null 2>&1 ; then +elif type urxvt >/dev/null 2>&1 ; then TERMINAL="urxvt -embed" -elif which st >/dev/null 2>&1 ; then +elif type st >/dev/null 2>&1 ; then TERMINAL="st -w" else echo "No xembed term found" >&2 @@ -142,28 +142,28 @@ previewer_loop () { case "$MIME" in video/*) - if which mpv >/dev/null 2>&1 ; then + if type mpv >/dev/null 2>&1 ; then mpv --force-window=immediate --loop-file --wid="$XID" "$FILE" & else term_nuke "$XID" "$FILE" fi ;; audio/*) - if which mpv >/dev/null 2>&1 ; then + if type mpv >/dev/null 2>&1 ; then mpv --force-window=immediate --loop-file --wid="$XID" "$FILE" & else term_nuke "$XID" "$FILE" fi ;; image/*) - if which sxiv >/dev/null 2>&1 ; then + if type sxiv >/dev/null 2>&1 ; then sxiv -e "$XID" "$FILE" & else term_nuke "$XID" "$FILE" fi ;; application/pdf) - if which zathura >/dev/null 2>&1 ; then + if type zathura >/dev/null 2>&1 ; then zathura -e "$XID" "$FILE" & else term_nuke "$XID" "$FILE" diff --git a/plugins/preview-tui b/plugins/preview-tui index 2b0d8d45..c0b055a1 100755 --- a/plugins/preview-tui +++ b/plugins/preview-tui @@ -138,7 +138,7 @@ togglepreview() { } exists() { - which "$1" >/dev/null 2>&1 + type "$1" >/dev/null 2>&1 } fifo_pager() { diff --git a/plugins/preview-tui-ext b/plugins/preview-tui-ext index 4f03419d..e50b3e41 100755 --- a/plugins/preview-tui-ext +++ b/plugins/preview-tui-ext @@ -158,7 +158,7 @@ togglepreview() { } exists() { - which "$1" >/dev/null 2>&1 + type "$1" >/dev/null 2>&1 } fifo_pager() { diff --git a/plugins/pskill b/plugins/pskill index 89008097..e01c7391 100755 --- a/plugins/pskill +++ b/plugins/pskill @@ -14,15 +14,15 @@ read -r psname # shellcheck disable=SC2009 if [ -n "$psname" ]; then - if which sudo >/dev/null 2>&1; then + if type sudo >/dev/null 2>&1; then sucmd=sudo - elif which doas >/dev/null 2>&1; then + elif type doas >/dev/null 2>&1; then sucmd=doas else sucmd=: # noop fi - if which fzf >/dev/null 2>&1; then + if type fzf >/dev/null 2>&1; then fuzzy=fzf else exit 1 diff --git a/plugins/renamer b/plugins/renamer index 532434a9..eb79435f 100755 --- a/plugins/renamer +++ b/plugins/renamer @@ -14,10 +14,10 @@ selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} -if which qmv >/dev/null 2>&1; then +if type qmv >/dev/null 2>&1; then batchrenamesel="qmv -fdo -da" batchrename="qmv -fdo -a" -elif which vidir >/dev/null 2>&1; then +elif type vidir >/dev/null 2>&1; then batchrenamesel="vidir" batchrename="vidir" else diff --git a/plugins/suedit b/plugins/suedit index 7598525b..e0fdfb5d 100755 --- a/plugins/suedit +++ b/plugins/suedit @@ -7,15 +7,10 @@ EDITOR="${EDITOR:-vim}" -is_cmd_exists () { - which "$1" > /dev/null 2>&1 - echo $? -} - -if [ "$(is_cmd_exists sudo)" -eq "0" ]; then +if type sudo >/dev/null 2>&1; then sudo "$EDITOR" "$1" -elif [ "$(is_cmd_exists sudoedit)" -eq "0" ]; then +elif type sudoedit >/dev/null 2>&1; then sudoedit "$1" -elif [ "$(is_cmd_exists doas)" -eq "0" ]; then +elif type doas >/dev/null 2>&1; then doas "$EDITOR" "$1" fi diff --git a/plugins/unmount-parent b/plugins/unmount-parent index 1d876ef8..4abf6ab0 100755 --- a/plugins/unmount-parent +++ b/plugins/unmount-parent @@ -11,38 +11,38 @@ # 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 +if [ "$PWD" = "$m" ]; then + # Allow running 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 _ +# Test if user is within $m or a subdir, 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 it is + mountpoint -q -- "$m/$d" + if [ "$?" -eq "1" ]; then + clear && printf "Parent '%s' is not a mountpoint. Press return to continue. " "$d" && 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 + cd "$m" && fusermount -uq "$m/$d" || err=1 if [ "$err" -eq "0" ]; then - rmdir "$m/$d" && clear && printf "Parent '%s' unmounted with lazy unmount. " "$d" + 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 unmount fails, offer 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 diff --git a/plugins/upload b/plugins/upload index 6a929a57..0bbf5626 100755 --- a/plugins/upload +++ b/plugins/upload @@ -10,7 +10,7 @@ # Author: Arun Prakash Jana if [ -n "$1" ] && [ -s "$1" ]; then - if which ffsend >/dev/null 2>&1; then + if type ffsend >/dev/null 2>&1; then ffsend -fiq u "$1" elif [ "$(mimetype --output-format %m "$1" | awk -F '/' '{print $1}')" = "text" ]; then curl -F "f:1=@$1" ix.io diff --git a/plugins/wall b/plugins/wall index 2471d066..a1541346 100755 --- a/plugins/wall +++ b/plugins/wall @@ -6,16 +6,11 @@ # Shell: POSIX Compliant # Author: juacq97 -cmd_exists () { - which "$1" > /dev/null 2>&1 - echo $? -} - if [ -n "$1" ]; then if [ "$(file --mime-type "$1" | awk '{print $NF}' | awk -F '/' '{print $1}')" = "image" ]; then - if [ "$(cmd_exists nitrogen)" -eq "0" ]; then + if type nitrogen >/dev/null 2>&1; then nitrogen --set-zoom-fill --save "$1" - elif [ "$(cmd_exists wal)" -eq "0" ]; then + elif type wal >/dev/null 2>&1; then wal -i "$1" else printf "nitrogen ir pywal missing" diff --git a/plugins/x2sel b/plugins/x2sel index 0a0fcd4e..0e6cbad7 100755 --- a/plugins/x2sel +++ b/plugins/x2sel @@ -22,28 +22,28 @@ selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} getclip () { - if which xsel >/dev/null 2>&1; then + if type xsel >/dev/null 2>&1; then # Linux xsel -bo - elif which xclip >/dev/null 2>&1; then + elif type xclip >/dev/null 2>&1; then # Linux xclip -sel clip -o - elif which pbpaste >/dev/null 2>&1; then + elif type pbpaste >/dev/null 2>&1; then # macOS pbpaste - elif which termux-clipboard-get >/dev/null 2>&1; then + elif type termux-clipboard-get >/dev/null 2>&1; then # Termux termux-clipboard-get - elif which powershell.exe >/dev/null 2>&1; then + elif type powershell.exe >/dev/null 2>&1; then # WSL powershell.exe Get-Clipboard elif [ -r /dev/clipboard ] ; then # Cygwin cat /dev/clipboard - elif which wl-paste >/dev/null 2>&1; then + elif type wl-paste >/dev/null 2>&1; then # Wayland wl-paste - elif which clipboard >/dev/null 2>&1; then + elif type clipboard >/dev/null 2>&1; then # Haiku clipboard --print fi