#!/bin/sh # File preview handler for lf. # requirements: # - horriblename/lf (https://github.com/horriblename/lf) # - chafa # - gnome-kra-ora-thumbnailer # - gnome-epub-thumbnailer # - webp-pixbuf-loader # - bat # - mediainfo # - ffmpegthumbnailer # - poppler # - atool # - odt2txt # - gpg set -C -f IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # Note that the cache file name is a function of file information, meaning if # an image appears in multiple places across the machine, it will not have to # be regenerated once seen. case "$(printf "%s\n" "$1" | awk '{print tolower($0)}')" in *.kra|*.krz) CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE.jpg" ] && gnome-kra-thumbnailer -s 1024 "$1" "$CACHE.jpg" chafa "$CACHE.jpg" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' exit 0 ;; *.ora) CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE.jpg" ] && gnome-openraster-thumbnailer -s 1024 "$1" "$CACHE.jpg" chafa "$CACHE.jpg" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' exit 0 ;; *.cbz|*.cbr|*.cbt) CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE.jpg" ] && comicthumb "$1" "$CACHE.jpg" 768 chafa "$CACHE.jpg" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' exit 0 ;; esac case "$(file --dereference --brief --mime-type -- "$1")" in image/gif|image/webp) CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE.jpg" ] && gdk-pixbuf-thumbnailer -s 1024 "$1" "$CACHE.jpg" chafa "$CACHE.jpg" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' mediainfo "$1";; image/*) chafa "$1" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' mediainfo "$1";; text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;; text/troff) man ./ "$1" | col -b ;; text/* | */xml | application/json) highlight --out-format=ansi --width "$(($4-2))" "$1" ;; #text/* | */xml | application/json) bat -p --terminal-width "$(($4-2))" -f "$1" ;; audio/* | application/octet-stream) mediainfo "$1" || exit 1 ;; video/* ) CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE" ] && ffmpegthumbnailer -i "$1" -o "$CACHE" -s 0 chafa "$CACHE" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' mediainfo "$1" ;; */pdf) CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE" chafa "$CACHE.jpg" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' ;; */epub+zip|*/mobi*) mediainfo "$1" CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)" [ ! -f "$CACHE.jpg" ] && gnome-epub-thumbnailer "$1" "$CACHE.jpg" chafa "$CACHE.jpg" -f sixel -s "$(($2-2))x$(($3-2))" | sed 's/#/\n#/g' ;; application/*zip) atool --list -- "$1" ;; *opendocument*) odt2txt "$1" ;; application/pgp-encrypted) gpg -d -- "$1" ;; esac exit 0