nnn/plugins/nuke

456 lines
15 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
2019-12-08 14:57:06 +00:00
# 3. nuke can use nnn plugins (e.g. mocplay 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:
# i. by extension
# ii. by mime (image, video, audio, pdf)
# iii. by mime (other file types)
#
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' in handle_fallback() as last resort.
#
# Feel free to change the utilities to your favourites and add more mimes.
#
2019-12-08 19:04:53 +00:00
# Defaults:
2019-12-07 17:18:33 +00:00
# By extension (only the enbaled ones):
# most archives: list with atool, bsdtar
# rar: list with unrar
# 7-zip: list with 7z
# pdf: zathura (GUI), pdftotext, mutool, exiftool
# audio: mocplay (nnn plugin using MOC), mpv, mediainfo, exiftool
# avi|dat|mkv|mp4: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool
# torrent: rtorrent, transmission-show
2019-12-07 17:18:33 +00:00
# odt|ods|odp|sxw: odt2txt
2020-01-22 17:50:31 +00:00
# md: glow (https://github.com/charmbracelet/glow)
2019-12-07 17:18:33 +00:00
# htm|html|xhtml: w3m, lynx, elinks
# json: jq, python (json.tool module)
# Multimedia by mime:
# image/*: sxiv (GUI), viu, img2txt, exiftool
# video/*: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool
# audio/*: mocplay (nnn plugin using MOC), mpv, mediainfo, exiftool
# 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
#
# ToDo:
# 1. Adapt, test and enable all mimes
# 2. Clean-up unnecessary the exit codes
# #############################################################################
# set to 1 to enable GUI apps
GUI=0
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")
ext="${FNAME##*.}"
if ! [ -z "$ext" ]; then
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
fi
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
## Preview as text conversion
pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | less -R
exit 0
elif which mutool >/dev/null 2>&1; then
mutool draw -F txt -i -- "${FPATH}" 1-10
exit 0
elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | less -R
exit 0
fi
}
handle_audio() {
if which mocp >/dev/null 2>&1; then
mocplay "${FPATH}" >/dev/null 2>&1
exit 0
elif which mpv >/dev/null 2>&1; then
mpv "${FPATH}" >/dev/null 2>&1 &
exit 0
elif which mediainfo >/dev/null 2>&1; then
mediainfo "${FPATH}" | less -R
exit 0
elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}"| less -R
exit 0
fi
}
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
# 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" | less -R
exit 0
elif which mediainfo >/dev/null 2>&1; then
mediainfo "${FPATH}" | less -R
exit 0
elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}"| less -R
exit 0
fi
}
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 which atool >/dev/null 2>&1; then
atool --list -- "${FPATH}" | less -R
exit 0
elif which bsdtar >/dev/null 2>&1; then
bsdtar --list --file "${FPATH}" | less -R
exit 0
fi
exit 1;;
rar)
if which unrar >/dev/null 2>&1; then
## Avoid password prompt by providing empty password
unrar lt -p- -- "${FPATH}" | less -R
fi
exit 1;;
7z)
if which 7z >/dev/null 2>&1; then
## Avoid password prompt by providing empty password
7z l -p -- "${FPATH}" | less -R
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
avi|dat|mkv|mp4)
handle_video
2019-12-07 17:18:33 +00:00
exit 1;;
## BitTorrent
torrent)
if which rtorrent >/dev/null 2>&1; then
rtorrent "${FPATH}"
exit 0
elif which 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 which odt2txt >/dev/null 2>&1; then
## Preview as text conversion
odt2txt "${FPATH}" | less -R
exit 0
fi
exit 1;;
2020-01-22 17:50:31 +00:00
## Markdown
md)
if which glow >/dev/null 2>&1; then
glow -sdark "${FPATH}" | less -R
exit 0
fi
;;
2019-12-07 17:18:33 +00:00
## HTML
htm|html|xhtml)
## Preview as text conversion
if which w3m >/dev/null 2>&1; then
w3m -dump "${FPATH}" | less -R
exit 0
elif which lynx >/dev/null 2>&1; then
lynx -dump -- "${FPATH}" | less -R
exit 0
elif which elinks >/dev/null 2>&1; then
elinks -dump "${FPATH}" | less -R
exit 0
fi
;;
## JSON
json)
if which jq >/dev/null 2>&1; then
jq --color-output . "${FPATH}" | less -R
exit 0
elif which python >/dev/null 2>&1; then
python -m json.tool -- "${FPATH}" | less -R
exit 0
fi
;;
esac
}
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/*)
if [ $GUI -ne 0 ] && which sxiv >/dev/null 2>&1; then
2019-12-13 03:03:02 +00:00
sxiv -q "${FPATH}" &
2019-12-07 17:18:33 +00:00
exit 0
elif which viu >/dev/null 2>&1; then
viu -n "${FPATH}" | less -R
exit 0
elif which img2txt >/dev/null 2>&1; then
img2txt --gamma=0.6 -- "${FPATH}" | less -R
exit 0
elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | less -R
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 overriden
## 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)
vi "${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 which djvutxt >/dev/null 2>&1; then
## Preview as text conversion (requires djvulibre)
djvutxt "${FPATH}" | less -R
exit 0
elif which exiftool >/dev/null 2>&1; then
exiftool "${FPATH}" | less -R
exit 0
fi
exit 1;;
esac
}
handle_fallback() {
if [ $GUI -ne 0 ]; then
xdg-open "${FPATH}" >/dev/null 2>&1 &
exit 0
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;;
esac
}
2019-12-07 17:18:33 +00:00
MIMETYPE="$( file --dereference --brief --mime-type -- "${FPATH}" )"
handle_blocked "${MIMETYPE}"
2019-12-07 17:18:33 +00:00
handle_extension
handle_multimedia "${MIMETYPE}"
handle_mime "${MIMETYPE}"
handle_fallback
exit 1