💾 Archived View for ait.place › dot › nnn › .config › nnn › plugins › nuke.txt captured on 2021-12-03 at 14:04:38.
-=-=-=-=-=-=-
#!/usr/bin/env sh # ############################################################################# # Description: Sample script to play files in apps by file type or mime # # Shell: POSIX compliant # Usage: nuke filepath # # Integration with nnn: # 1. Export the required config: # export NNN_OPENER=/absolute/path/to/nuke # # Otherwise, if nuke is in $PATH # # export NNN_OPENER=nuke # 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. # # Details: # Inspired by ranger's scope.sh, modified for usage with nnn. # # 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) # # Modification tips: # 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. # # Defaults: # By extension (only the enabled 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|mkv|mp4: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool # log: vi # torrent: rtorrent, transmission-show # odt|ods|odp|sxw: odt2txt # md: glow (https://github.com/charmbracelet/glow), lowdown (https://kristaps.bsd.lv/lowdown) # htm|html|xhtml: w3m, lynx, elinks # json: jq, python (json.tool module) # Multimedia by mime: # image/*: sxiv (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 # application/pdf: zathura (GUI), pdftotext, mutool, exiftool # Other mimes: # text/troff: man -l # text/* | */xml: vi # image/vnd.djvu): djvutxt, exiftool # # ToDo: # 1. Adapt, test and enable all mimes # 2. Clean-up the unnecessary exit codes # ############################################################################# # set to 1 to enable GUI apps GUI="${GUI:-1}" 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="${EDITOR:-vi}" PAGER="${PAGER:-less -R}" ext="${FNAME##*.}" if ! [ -z "$ext" ]; then ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')" fi handle_pdf() { if [ "$GUI" -ne 0 ] && which zathura 2>&1; then zathura "${FPATH}" exit 0 elif which pdftotext 2>&1; then ## Preview as text conversion pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER" exit 0 elif which mutool 2>&1; then mutool draw -F txt -i -- "${FPATH}" 1-10 exit 0 elif which exiftool 2>&1; then exiftool "${FPATH}" | eval "$PAGER" exit 0 fi } handle_audio() { if which mpv 2>&1; then mpv "${FPATH}" 2>&1 & exit 0 elif which mediainfo 2>&1; then mediainfo "${FPATH}" | eval "$PAGER" exit 0 elif which exiftool 2>&1; then exiftool "${FPATH}"| eval "$PAGER" exit 0 fi } handle_video() { if [ "$GUI" -ne 0 ] && which mpv 2>&1; then mpv "${FPATH}" exit 0 fi } # 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 2>&1; then atool --list -- "${FPATH}" | eval "$PAGER" exit 0 elif which bsdtar 2>&1; then bsdtar --list --file "${FPATH}" | eval "$PAGER" exit 0 fi exit 1;; rar) if which unrar 2>&1; then ## Avoid password prompt by providing empty password unrar lt -p- -- "${FPATH}" | eval "$PAGER" fi exit 1;; 7z) if which 7z 2>&1; then ## Avoid password prompt by providing empty password 7z l -p -- "${FPATH}" | eval "$PAGER" exit 0 fi exit 1;; ## PDF pdf) handle_pdf exit 1;; ## Audio aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|wma) handle_audio exit 1;; ## Video avi|mkv|mp4) handle_video exit 1;; ## Log files log) "$EDITOR" "${FPATH}" exit 0;; ## BitTorrent torrent) if which rtorrent 2>&1; then rtorrent "${FPATH}" exit 0 elif which transmission-show 2>&1; then transmission-show -- "${FPATH}" exit 0 fi exit 1;; ## OpenDocument odt|ods|odp|sxw) if which odt2txt 2>&1; then ## Preview as text conversion odt2txt "${FPATH}" | eval "$PAGER" exit 0 fi exit 1;; ## Markdown md) if which mdcat 2>&1; then mdcat "${FPATH}" | eval "$PAGER" exit 0 fi exit 1;; ## HTML htm|html|xhtml) ## Preview as text conversion if which w3m 2>&1; then w3m -dump "${FPATH}" | eval "$PAGER" exit 0 elif which lynx 2>&1; then lynx -dump -- "${FPATH}" | eval "$PAGER" exit 0 elif which elinks 2>&1; then elinks -dump "${FPATH}" | eval "$PAGER" exit 0 fi ;; ## JSON json) if which jq 2>&1; then jq --color-output . "${FPATH}" | eval "$PAGER" exit 0 elif which python 2>&1; then python -m json.tool -- "${FPATH}" | eval "$PAGER" exit 0 fi ;; esac } abspath() { case "$1" in /*) printf "%s\n" "$1";; *) printf "%s\n" "$PWD/$1";; esac } listimages() { find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \ '.*\(jpe?g\|bmp\|png\|gif\)