From 947d163b38940f64541a46cabfa884778fe69817 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 11 Nov 2021 18:41:16 +0300 Subject: fish: migration --- .local/bin/nuke | 228 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 137 insertions(+), 91 deletions(-) (limited to '.local/bin/nuke') diff --git a/.local/bin/nuke b/.local/bin/nuke index f8f9d8b..cbcf3a3 100755 --- a/.local/bin/nuke +++ b/.local/bin/nuke @@ -1,6 +1,5 @@ #!/usr/bin/env sh -# ############################################################################# # Description: Sample script to play files in apps by file type or mime # # Shell: POSIX compliant @@ -14,7 +13,7 @@ # 2. Run nnn with the program option to indicate a CLI opener # nnn -c # # The -c program option overrides option -e -# 3. nuke can use nnn plugins (e.g. mocplay is used for audio), $PATH is updated. +# 3. nuke can use nnn plugins (e.g. mocq is used for audio), $PATH is updated. # # Details: # Inspired by ranger's scope.sh, modified for usage with nnn. @@ -22,9 +21,10 @@ # Guards against accidentally opening mime types like executables, shared libs etc. # # Tries to play 'file' (1st argument) in the following order: -# i. by extension -# ii. by mime (image, video, audio, pdf) -# iii. by mime (other file types) +# 1. by extension +# 2. by mime (image, video, audio, pdf) +# 3. by mime (other file types) +# 4. by mime (prompt and run executables) # # Modification tips: # 1. Invokes CLI utilities by default. Set GUI to 1 to enable GUI apps. @@ -32,7 +32,7 @@ # 3. Start GUI apps in bg to unblock. Redirect stdout and strerr if required. # 4. Some CLI utilities are piped to the $PAGER, to wait and quit uniformly. # 5. If the output cannot be paged use "read -r _" to wait for user input. -# 6. On a DE, try 'xdg-open' in handle_fallback() as last resort. +# 6. On a DE, try 'xdg-open' or 'open' in handle_fallback() as last resort. # # Feel free to change the utilities to your favourites and add more mimes. # @@ -42,7 +42,7 @@ # rar: list with unrar # 7-zip: list with 7z # pdf: zathura (GUI), pdftotext, mutool, exiftool -# audio: mocplay (nnn plugin using MOC), mpv, mediainfo, exiftool +# audio: mocq (nnn plugin using MOC), mpv, media_client (Haiku), mediainfo, exiftool # avi|mkv|mp4: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool # log: vi # torrent: rtorrent, transmission-show @@ -51,22 +51,22 @@ # htm|html|xhtml: w3m, lynx, elinks # json: jq, python (json.tool module) # Multimedia by mime: -# image/*: imv/sxiv (GUI), viu (https://github.com/atanunq/viu), img2txt, exiftool +# image/*: imv/sxiv/nsxiv (GUI), viu (https://github.com/atanunq/viu), img2txt, exiftool # video/*: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool -# audio/*: mocplay (nnn plugin using MOC), mpv, mediainfo, exiftool +# audio/*: mocq (nnn plugin using MOC), mpv, media_client (Haiku), mediainfo, exiftool # application/pdf: zathura (GUI), pdftotext, mutool, exiftool # Other mimes: # text/troff: man -l # text/* | */xml: vi # image/vnd.djvu): djvutxt, exiftool # -# ToDo: +# TODO: # 1. Adapt, test and enable all mimes # 2. Clean-up the unnecessary exit codes -# ############################################################################# -# set to 1 to enable GUI apps -GUI="1" +# set to 1 to enable GUI apps and/or BIN execution +GUI="${GUI:-1}" +BIN="${BIN:-0}" set -euf -o noclobber -o noglob -o nounset IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # protect trailing \n @@ -76,66 +76,80 @@ IMAGE_CACHE_PATH="$(dirname "$1")"/.thumbs FPATH="$1" FNAME=$(basename "$1") -EDITOR="${EDITOR:-vi}" +EDITOR="${VISUAL:-${EDITOR:-vi}}" PAGER="${PAGER:-less -R}" ext="${FNAME##*.}" -if ! [ -z "$ext" ]; then +if [ -n "$ext" ]; then ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')" fi +is_mac() { + uname | grep -q "Darwin" +} + handle_pdf() { - if [ "$GUI" -ne 0 ] && which zathura >/dev/null 2>&1; then - zathura "${FPATH}" >/dev/null 2>&1 & - exit 0 - elif which pdftotext >/dev/null 2>&1; then + if [ "$GUI" -ne 0 ]; then + if is_mac; then + nohup open "${FPATH}" >/dev/null 2>&1 & + elif type zathura >/dev/null 2>&1; then + nohup zathura "${FPATH}" >/dev/null 2>&1 & + else + return + fi + 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 + else + return fi + exit 0 } handle_audio() { - if which mocp >/dev/null 2>&1 && which mocplay >/dev/null 2>&1; then - mocplay "${FPATH}" "opener" >/dev/null 2>&1 - exit 0 - elif which mpv >/dev/null 2>&1; then + if type mocp >/dev/null 2>&1 && type mocq >/dev/null 2>&1; then + mocq "${FPATH}" "opener" >/dev/null 2>&1 + elif type mpv >/dev/null 2>&1; then mpv "${FPATH}" >/dev/null 2>&1 & - exit 0 - elif which mediainfo >/dev/null 2>&1; then + elif type media_client >/dev/null 2>&1; then + media_client play "${FPATH}" >/dev/null 2>&1 & + 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 + else + return fi + exit 0 } handle_video() { - if [ "$GUI" -ne 0 ] && which smplayer >/dev/null 2>&1; then - smplayer "${FPATH}" >/dev/null 2>&1 & - exit 0 - elif [ "$GUI" -ne 0 ] && which mpv >/dev/null 2>&1; then - mpv "${FPATH}" >/dev/null 2>&1 & - exit 0 - elif which ffmpegthumbnailer >/dev/null 2>&1; then + if [ "$GUI" -ne 0 ]; then + if is_mac; then + nohup open "${FPATH}" >/dev/null 2>&1 & + elif type smplayer >/dev/null 2>&1; then + nohup smplayer "${FPATH}" >/dev/null 2>&1 & + elif type mpv >/dev/null 2>&1; then + nohup mpv "${FPATH}" >/dev/null 2>&1 & + else + return + fi + 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 + else + return fi + exit 0 } # handle this extension and exit @@ -144,22 +158,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 @@ -183,39 +197,35 @@ handle_extension() { ## Log files log) - # "$EDITOR" "${FPATH}" - "$PAGER" "${FPATH}" + "$EDITOR" "${FPATH}" exit 0;; ## 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 exit 1;; ## OpenDocument - odt|ods|odp|sxw|pptx|docx) - if which odt2txt >/dev/null 2>&1; then + odt|ods|odp|sxw) + if type odt2txt >/dev/null 2>&1; then ## Preview as text conversion odt2txt "${FPATH}" | eval "$PAGER" exit 0 - elif [ "$GUI" -ne 0 ] && which libreoffice >/dev/null 2>&1; then - libreoffice "${FPATH}" & - exit 0 fi exit 1;; ## 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 @@ -224,13 +234,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 @@ -238,40 +248,44 @@ 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 ;; - - ## TeX - tex) - "$EDITOR" "${FPATH}" - exit 0;; esac } +# sets the variable abs_target, this should be faster than calling printf abspath() { case "$1" in - /*) printf "%s\n" "$1";; - *) printf "%s\n" "$PWD/$1";; + /*) abs_target="$1";; + *) abs_target="$PWD/$1";; esac } +# storing the result to a tmp file is faster than calling listimages twice listimages() { - find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \ - '.*\(jpe?g\|bmp\|png\|gif\)$' -print0 | sort -z + find -L "///${1%/*}" -maxdepth 1 -type f -print0 | + grep -izZE '\.(jpe?g|png|gif|webp|tiff|bmp|ico|svg)$' | + sort -z | tee "$tmp" } load_dir() { - target="$(abspath "$2")" - count="$(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)" + abspath "$2" + tmp="${TMPDIR:-/tmp}/nuke_$$" + trap 'rm -f $tmp' EXIT + count="$(listimages "$abs_target" | grep -a -m 1 -ZznF "$abs_target" | cut -d: -f1)" if [ -n "$count" ]; then - listimages | xargs -0 "$1" -n "$count" -- + if [ "$GUI" -ne 0 ]; then + xargs -0 nohup "$1" -n "$count" -- < "$tmp" + else + xargs -0 "$1" -n "$count" -- < "$tmp" + fi else shift "$1" -- "$@" # fallback @@ -300,19 +314,27 @@ handle_multimedia() { ## Image image/*) - if [ "$GUI" -ne 0 ] && which 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 - load_dir sxiv "${FPATH}" >/dev/null 2>&1 & - exit 0 - elif which viu >/dev/null 2>&1; then + if [ "$GUI" -ne 0 ]; then + if is_mac; then + nohup open "${FPATH}" >/dev/null 2>&1 & + exit 0 + elif type imvr >/dev/null 2>&1; then + load_dir imvr "${FPATH}" >/dev/null 2>&1 & + exit 0 + elif type sxiv >/dev/null 2>&1; then + load_dir sxiv "${FPATH}" >/dev/null 2>&1 & + exit 0 + elif type nsxiv >/dev/null 2>&1; then + load_dir nsxiv "${FPATH}" >/dev/null 2>&1 & + exit 0 + fi + 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 @@ -455,11 +477,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 @@ -469,8 +491,13 @@ handle_mime() { handle_fallback() { if [ "$GUI" -ne 0 ]; then - xdg-open "${FPATH}" >/dev/null 2>&1 & - exit 0 + if type xdg-open >/dev/null 2>&1; then + nohup xdg-open "${FPATH}" >/dev/null 2>&1 & + exit 0 + elif type open >/dev/null 2>&1; then + nohup open "${FPATH}" >/dev/null 2>&1 & + exit 0 + fi fi echo '----- File details -----' && file --dereference --brief -- "${FPATH}" @@ -496,10 +523,29 @@ handle_blocked() { esac } -MIMETYPE="$( file --dereference --brief --mime-type -- "${FPATH}" )" +handle_bin() { + case "${MIMETYPE}" in + application/x-executable|application/x-shellscript) + clear + echo '-------- Executable File --------' && file --dereference --brief -- "${FPATH}" + printf "Run executable (y/N/'a'rgs)? " + read -r answer + case "$answer" in + [Yy]* ) exec "${FPATH}";; + [Aa]* ) + printf "args: " + read -r args + exec "${FPATH}" "$args";; + [Nn]* ) exit;; + esac + esac +} + +MIMETYPE="$( file -bL --mime-type -- "${FPATH}" )" handle_extension handle_multimedia "${MIMETYPE}" handle_mime "${MIMETYPE}" +[ "$BIN" -ne 0 ] && [ -x "${FPATH}" ] && handle_bin handle_blocked "${MIMETYPE}" handle_fallback -- cgit v1.2.3-70-g09d2