dotfiles

Torpy's handcrafted dootfiles.
Log | Files | Refs | README

scope (3214B)


      1 #!/bin/sh
      2 
      3 # File preview handler for lf.
      4 
      5 set -C -f
      6 IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"
      7 
      8 image() {
      9 	if [ -f "$1" ] && [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1; then
     10 		printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y": "%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' "$4" "$5" "$(($2-1))" "$(($3-1))" "$1" > "$FIFO_UEBERZUG"
     11 	else
     12 		mediainfo "$6"
     13 	fi
     14 }
     15 
     16 # Note that the cache file name is a function of file information, meaning if
     17 # an image appears in multiple places across the machine, it will not have to
     18 # be regenerated once seen.
     19 
     20 case "$(file --dereference --brief --mime-type -- "$1")" in
     21 	image/avif) 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)"
     22 		[ ! -f "$CACHE" ] && magick "$1" "$CACHE.jpg"
     23 		image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1" ;;
     24 	image/vnd.djvu)
     25 		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)"
     26 		[ ! -f "$CACHE" ] && djvused "$1" -e 'select 1; save-page-with /dev/stdout' | magick -density 200 - "$CACHE.jpg" > /dev/null 2>&1
     27 		image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1" ;;
     28 image/svg+xml)
     29 	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)"
     30 	[ ! -f "$CACHE" ] && inkscape --convert-dpi-method=none -o "$CACHE.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$1"
     31 	image "$CACHE.png" "$2" "$3" "$4" "$5" "$1"
     32 	;;
     33   image/x-xcf)
     34     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 | awk '{print $1}')"
     35     [ ! -f "$CACHE.jpg" ] && magick "$1[0]" "$CACHE.jpg"
     36     image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
     37   ;;
     38 	image/*) image "$1" "$2" "$3" "$4" "$5" "$1" ;;
     39 	text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;;
     40 	text/troff) man ./ "$1" | col -b ;;
     41 	text/* | */xml | application/json | application/x-ndjson) bat -p --theme ansi --terminal-width "$(($4-2))" -f "$1" ;;
     42 	audio/* | application/octet-stream) mediainfo "$1" || exit 1 ;;
     43 	video/* )
     44 		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)"
     45 		[ ! -f "$CACHE" ] && ffmpegthumbnailer -i "$1" -o "$CACHE" -s 0
     46 		image "$CACHE" "$2" "$3" "$4" "$5" "$1"
     47 		;;
     48 	*/pdf)
     49 		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)"
     50 		[ ! -f "$CACHE.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"
     51 		image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
     52 		;;
     53 	*/epub+zip|*/mobi*)
     54 		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)"
     55 		[ ! -f "$CACHE.jpg" ] && gnome-epub-thumbnailer "$1" "$CACHE.jpg"
     56 		image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
     57 		;;
     58 	application/*zip) atool --list -- "$1" ;;
     59 	*opendocument*) odt2txt "$1" ;;
     60 	application/pgp-encrypted) gpg -d -- "$1" ;;
     61 esac
     62 exit 1