nnn/plugins/nuke

553 lines
18 KiB
Plaintext
Raw Normal View History

2019-12-07 17:18:33 +00:00
#!/usr/bin/env sh
2019-12-08 19:04:53 +00:00
# Description: Sample script to play files in apps by file type or mime
2019-12-07 17:18:33 +00:00
#
2019-12-08 19:04:53 +00:00
# Shell: POSIX compliant
# Usage: nuke filepath
#
# Integration with nnn:
2019-12-07 17:18:33 +00:00
# 1. Export the required config:
2019-12-08 14:57:06 +00:00
# export NNN_OPENER=/absolute/path/to/nuke
# # Otherwise, if nuke is in $PATH
# # export NNN_OPENER=nuke
2019-12-07 17:18:33 +00:00
# 2. Run nnn with the program option to indicate a CLI opener
# nnn -c
# # The -c program option overrides option -e
2021-05-15 21:16:17 +00:00
# 3. nuke can use nnn plugins (e.g. mocq is used for audio), $PATH is updated.
2019-12-07 17:18:33 +00:00
#
2019-12-08 19:04:53 +00:00
# Details:
2019-12-07 17:18:33 +00:00
# Inspired by ranger's scope.sh, modified for usage with nnn.
#
# Guards against accidentally opening mime types like executables, shared libs etc.
#
2019-12-07 17:18:33 +00:00
# Tries to play 'file' (1st argument) in the following order:
2021-07-20 23:12:37 +00:00
# 1. by extension
# 2. by mime (image, video, audio, pdf)
# 3. by mime (other file types)
# 4. by mime (prompt and run executables)
2019-12-07 17:18:33 +00:00
#
2019-12-08 19:04:53 +00:00
# Modification tips:
2019-12-07 17:18:33 +00:00
# 1. Invokes CLI utilities by default. Set GUI to 1 to enable GUI apps.
# 2. PAGER is "less -R".
# 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' or 'open' in handle_fallback() as last resort.
2019-12-07 17:18:33 +00:00
#
# Feel free to change the utilities to your favourites and add more mimes.
#
2019-12-08 19:04:53 +00:00
# Defaults:
2020-02-23 02:48:07 +00:00
# By extension (only the enabled ones):
2019-12-07 17:18:33 +00:00
# most archives: list with atool, bsdtar
# rar: list with unrar
# 7-zip: list with 7z
# pdf: zathura (GUI), pdftotext, mutool, exiftool
2021-05-15 21:16:17 +00:00
# audio: mocq (nnn plugin using MOC), mpv, media_client (Haiku), mediainfo, exiftool
2020-02-14 17:07:47 +00:00
# avi|mkv|mp4: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool
2020-02-19 14:52:00 +00:00
# log: vi
# torrent: rtorrent, transmission-show
2019-12-07 17:18:33 +00:00
# odt|ods|odp|sxw: odt2txt
# md: glow (https://github.com/charmbracelet/glow), lowdown (https://kristaps.bsd.lv/lowdown)
2019-12-07 17:18:33 +00:00
# htm|html|xhtml: w3m, lynx, elinks
# json: jq, python (json.tool module)
# Multimedia by mime:
# image/*: imv/sxiv/nsxiv (GUI), viu (https://github.com/atanunq/viu), img2txt, exiftool
2019-12-07 17:18:33 +00:00
# video/*: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool
2021-05-15 21:16:17 +00:00
# audio/*: mocq (nnn plugin using MOC), mpv, media_client (Haiku), mediainfo, exiftool
2019-12-07 17:18:33 +00:00
# application/pdf: zathura (GUI), pdftotext, mutool, exiftool
# Other mimes:
# text/troff: man -l
2019-12-07 17:18:33 +00:00
# text/* | */xml: vi
# image/vnd.djvu): djvutxt, exiftool
#
2021-05-15 17:32:01 +00:00
# TODO:
2019-12-07 17:18:33 +00:00
# 1. Adapt, test and enable all mimes
2020-02-23 02:48:07 +00:00
# 2. Clean-up the unnecessary exit codes
2019-12-07 17:18:33 +00:00
2021-07-20 23:12:37 +00:00
# set to 1 to enable GUI apps and/or BIN execution
GUI="${GUI:-0}"
2021-07-20 23:12:37 +00:00
BIN="${BIN:-0}"
2019-12-07 17:18:33 +00:00
set -euf -o noclobber -o noglob -o nounset
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # protect trailing \n
PATH=$PATH:"${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins"
IMAGE_CACHE_PATH="$(dirname "$1")"/.thumbs
FPATH="$1"
FNAME=$(basename "$1")
EDITOR="${VISUAL:-${EDITOR:-vi}}"
PAGER="${PAGER:-less -R}"
2019-12-07 17:18:33 +00:00
ext="${FNAME##*.}"
if [ -n "$ext" ]; then
2019-12-07 17:18:33 +00:00
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
fi
is_mac() {
uname | grep -q "Darwin"
}
handle_pdf() {
2021-05-22 03:04:59 +00:00
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"
elif type mutool >/dev/null 2>&1; then
2020-05-30 13:18:12 +00:00
mutool draw -F txt -i -- "${FPATH}" 1-10 | eval "$PAGER"
elif type exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | eval "$PAGER"
2021-05-22 03:04:59 +00:00
else
return
fi
2021-05-22 03:04:59 +00:00
exit 0
}
handle_audio() {
2021-05-15 21:16:17 +00:00
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 &
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"
elif type exiftool >/dev/null 2>&1; then
exiftool "${FPATH}"| eval "$PAGER"
2021-05-22 03:04:59 +00:00
else
return
fi
2021-05-22 03:04:59 +00:00
exit 0
}
handle_video() {
2021-05-22 03:04:59 +00:00
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"
elif type mediainfo >/dev/null 2>&1; then
mediainfo "${FPATH}" | eval "$PAGER"
elif type exiftool >/dev/null 2>&1; then
exiftool "${FPATH}"| eval "$PAGER"
2021-05-22 03:04:59 +00:00
else
return
fi
2021-05-22 03:04:59 +00:00
exit 0
}
2019-12-07 17:18:33 +00:00
# handle this extension and exit
handle_extension() {
case "${ext}" in
## 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 type atool >/dev/null 2>&1; then
atool --list -- "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type bsdtar >/dev/null 2>&1; then
bsdtar --list --file "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
exit 1;;
rar)
if type unrar >/dev/null 2>&1; then
2019-12-07 17:18:33 +00:00
## Avoid password prompt by providing empty password
unrar lt -p- -- "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
fi
exit 1;;
7z)
if type 7z >/dev/null 2>&1; then
2019-12-07 17:18:33 +00:00
## Avoid password prompt by providing empty password
7z l -p -- "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
exit 1;;
## PDF
pdf)
handle_pdf
2019-12-07 17:18:33 +00:00
exit 1;;
## Audio
aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|wma)
handle_audio
exit 1;;
## Video
2020-02-14 17:07:47 +00:00
avi|mkv|mp4)
handle_video
2019-12-07 17:18:33 +00:00
exit 1;;
2020-02-19 14:52:00 +00:00
## Log files
log)
"$EDITOR" "${FPATH}"
2020-02-19 14:52:00 +00:00
exit 0;;
2019-12-07 17:18:33 +00:00
## BitTorrent
torrent)
if type rtorrent >/dev/null 2>&1; then
rtorrent "${FPATH}"
exit 0
elif type transmission-show >/dev/null 2>&1; then
2019-12-07 17:18:33 +00:00
transmission-show -- "${FPATH}"
exit 0
fi
exit 1;;
## OpenDocument
odt|ods|odp|sxw)
if type odt2txt >/dev/null 2>&1; then
2019-12-07 17:18:33 +00:00
## Preview as text conversion
odt2txt "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
exit 1;;
2020-01-22 17:50:31 +00:00
## Markdown
md)
if type glow >/dev/null 2>&1; then
glow -sdark "${FPATH}" | eval "$PAGER"
2020-01-22 17:50:31 +00:00
exit 0
elif type lowdown >/dev/null 2>&1; then
lowdown -Tterm "${FPATH}" | eval "$PAGER"
exit 0
2020-01-22 17:50:31 +00:00
fi
;;
2019-12-07 17:18:33 +00:00
## HTML
htm|html|xhtml)
## Preview as text conversion
if type w3m >/dev/null 2>&1; then
w3m -dump "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type lynx >/dev/null 2>&1; then
lynx -dump -- "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type elinks >/dev/null 2>&1; then
elinks -dump "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
;;
2020-02-19 14:52:00 +00:00
2019-12-07 17:18:33 +00:00
## JSON
json)
if type jq >/dev/null 2>&1; then
jq --color-output . "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type python >/dev/null 2>&1; then
python -m json.tool -- "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
;;
esac
}
# sets the variable abs_target, this should be faster than calling printf
2020-03-03 00:46:52 +00:00
abspath() {
case "$1" in
/*) abs_target="$1";;
*) abs_target="$PWD/$1";;
2020-03-03 00:46:52 +00:00
esac
}
# storing the result to a tmp file is faster than calling listimages twice
2020-03-03 00:46:52 +00:00
listimages() {
find -L "///${1%/*}" -maxdepth 1 -type f -print0 |
grep -izZE '\.(jpe?g|png|gif|webp|tiff|bmp|ico|svg)$' |
sort -z | tee "$tmp"
2020-03-03 00:46:52 +00:00
}
2020-06-10 20:19:40 +00:00
load_dir() {
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)"
2020-03-03 00:46:52 +00:00
if [ -n "$count" ]; then
if [ "$GUI" -ne 0 ]; then
xargs -0 nohup "$1" -n "$count" -- < "$tmp"
else
xargs -0 "$1" -n "$count" -- < "$tmp"
fi
2020-03-03 00:46:52 +00:00
else
2020-06-10 20:19:40 +00:00
shift
"$1" -- "$@" # fallback
2020-03-03 00:46:52 +00:00
fi
}
2019-12-07 17:18:33 +00:00
handle_multimedia() {
## Size of the preview if there are multiple options or it has to be
## rendered from vector graphics. If the conversion program allows
## specifying only one dimension while keeping the aspect ratio, the width
## will be used.
# local DEFAULT_SIZE="1920x1080"
mimetype="${1}"
case "${mimetype}" in
## SVG
# image/svg+xml|image/svg)
# convert -- "${FPATH}" "${IMAGE_CACHE_PATH}" && exit 6
# exit 1;;
## DjVu
# image/vnd.djvu)
# ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
# - "${IMAGE_CACHE_PATH}" < "${FPATH}" \
# && exit 6 || exit 1;;
## Image
image/*)
2021-05-22 03:04:59 +00:00
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
2021-05-22 03:04:59 +00:00
fi
elif type viu >/dev/null 2>&1; then
viu -n "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type img2txt >/dev/null 2>&1; then
img2txt --gamma=0.6 -- "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
# local orientation
# orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FPATH}" )"
## If orientation data is present and the image actually
## needs rotating ("1" means no rotation)...
# if [[ -n "$orientation" && "$orientation" != 1 ]]; then
## ...auto-rotate the image according to the EXIF data.
# convert -- "${FPATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
# fi
## `w3mimgdisplay` will be called for all images (unless overridden
2019-12-07 17:18:33 +00:00
## as above), but might fail for unsupported types.
exit 7;;
## PDF
application/pdf)
handle_pdf
2019-12-07 17:18:33 +00:00
exit 1;;
## Audio
audio/*)
handle_audio
2019-12-07 17:18:33 +00:00
exit 1;;
## Video
video/*)
handle_video
2019-12-07 17:18:33 +00:00
exit 1;;
# pdftoppm -f 1 -l 1 \
# -scale-to-x "${DEFAULT_SIZE%x*}" \
# -scale-to-y -1 \
# -singlefile \
# -jpeg -tiffcompression jpeg \
# -- "${FPATH}" "${IMAGE_CACHE_PATH%.*}" \
# && exit 6 || exit 1;;
## ePub, MOBI, FB2 (using Calibre)
# application/epub+zip|application/x-mobipocket-ebook|\
# application/x-fictionbook+xml)
# # ePub (using https://github.com/marianosimone/epub-thumbnailer)
# epub-thumbnailer "${FPATH}" "${IMAGE_CACHE_PATH}" \
# "${DEFAULT_SIZE%x*}" && exit 6
# ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FPATH}" \
# >/dev/null && exit 6
# exit 1;;
## Font
# application/font*|application/*opentype)
# preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
# if fontimage -o "${preview_png}" \
# --pixelsize "120" \
# --fontname \
# --pixelsize "80" \
# --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
# --text " abcdefghijklmnopqrstuvwxyz " \
# --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
# --text " The quick brown fox jumps over the lazy dog. " \
# "${FPATH}";
# then
# convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
# && rm "${preview_png}" \
# && exit 6
# else
# exit 1
# fi
# ;;
## Preview archives using the first image inside.
## (Very useful for comic book collections for example.)
# application/zip|application/x-rar|application/x-7z-compressed|\
# application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
# local fn=""; local fe=""
# local zip=""; local rar=""; local tar=""; local bsd=""
# case "${mimetype}" in
# application/zip) zip=1 ;;
# application/x-rar) rar=1 ;;
# application/x-7z-compressed) ;;
# *) tar=1 ;;
# esac
# { [ "$tar" ] && fn=$(tar --list --file "${FPATH}"); } || \
# { fn=$(bsdtar --list --file "${FPATH}") && bsd=1 && tar=""; } || \
# { [ "$rar" ] && fn=$(unrar lb -p- -- "${FPATH}"); } || \
# { [ "$zip" ] && fn=$(zipinfo -1 -- "${FPATH}"); } || return
#
# fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
# [ print(l, end='') for l in sys.stdin if \
# (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
# sort -V | head -n 1)
# [ "$fn" = "" ] && return
# [ "$bsd" ] && fn=$(printf '%b' "$fn")
#
# [ "$tar" ] && tar --extract --to-stdout \
# --file "${FPATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
# fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
# [ "$bsd" ] && bsdtar --extract --to-stdout \
# --file "${FPATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
# [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
# [ "$rar" ] && unrar p -p- -inul -- "${FPATH}" "$fn" > \
# "${IMAGE_CACHE_PATH}" && exit 6
# [ "$zip" ] && unzip -pP "" -- "${FPATH}" "$fe" > \
# "${IMAGE_CACHE_PATH}" && exit 6
# [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
# ;;
esac
}
handle_mime() {
mimetype="${1}"
case "${mimetype}" in
## Manpages
text/troff)
man -l "${FPATH}"
exit 0;;
2019-12-07 17:18:33 +00:00
## Text
text/* | */xml)
"$EDITOR" "${FPATH}"
2020-01-22 17:50:31 +00:00
exit 0;;
2019-12-07 17:18:33 +00:00
## Syntax highlight
# if [[ "$( stat --printf='%s' -- "${FPATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
# exit 2
# fi
# if [[ "$( tput colors )" -ge 256 ]]; then
# local pygmentize_format='terminal256'
# local highlight_format='xterm256'
# else
# local pygmentize_format='terminal'
# local highlight_format='ansi'
# fi
# env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
# --out-format="${highlight_format}" \
# --force -- "${FPATH}" && exit 5
# pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
# -- "${FPATH}" && exit 5
# exit 2;;
## DjVu
image/vnd.djvu)
if type djvutxt >/dev/null 2>&1; then
2019-12-07 17:18:33 +00:00
## Preview as text conversion (requires djvulibre)
djvutxt "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
elif type exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | eval "$PAGER"
2019-12-07 17:18:33 +00:00
exit 0
fi
exit 1;;
esac
}
handle_fallback() {
2021-05-22 03:04:59 +00:00
if [ "$GUI" -ne 0 ]; then
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
2019-12-07 17:18:33 +00:00
fi
echo '----- File details -----' && file --dereference --brief -- "${FPATH}"
exit 1
}
handle_blocked() {
case "${MIMETYPE}" in
application/x-sharedlib)
exit 0;;
application/x-shared-library-la)
exit 0;;
application/x-executable)
exit 0;;
application/x-shellscript)
exit 0;;
2020-05-09 20:48:02 +00:00
application/octet-stream)
exit 0;;
esac
}
2021-07-20 23:12:37 +00:00
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)? "
2021-07-20 23:12:37 +00:00
read -r answer
case "$answer" in
[Yy]* ) exec "${FPATH}";;
[Aa]* )
printf "args: "
read -r args
exec "${FPATH}" "$args";;
2021-07-20 23:12:37 +00:00
[Nn]* ) exit;;
esac
esac
}
2021-03-21 14:03:43 +00:00
MIMETYPE="$( file -bL --mime-type -- "${FPATH}" )"
2019-12-07 17:18:33 +00:00
handle_extension
handle_multimedia "${MIMETYPE}"
handle_mime "${MIMETYPE}"
2021-07-20 23:12:37 +00:00
[ "$BIN" -ne 0 ] && [ -x "${FPATH}" ] && handle_bin
2020-05-16 19:16:49 +00:00
handle_blocked "${MIMETYPE}"
2019-12-07 17:18:33 +00:00
handle_fallback
exit 1