| 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 | ifub() { |
| 17 | [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1 |
| 18 | } |
| 19 | |
| 20 | # Note that the cache file name is a function of file information, meaning if |
| 21 | # an image appears in multiple places across the machine, it will not have to |
| 22 | # be regenerated once seen. |
| 23 | |
| 24 | case "$(file --dereference --brief --mime-type -- "$1")" in |
| 25 | 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)" |
| 26 | [ ! -f "$CACHE" ] && convert "$1" "$CACHE.jpg" |
| 27 | image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1" ;; |
| 28 | image/*) image "$1" "$2" "$3" "$4" "$5" "$1" ;; |
| 29 | text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;; |
| 30 | text/troff) man ./ "$1" | col -b ;; |
| 31 | text/* | */xml | application/json) bat --terminal-width "$(($4-2))" -f "$1" ;; |
| 32 | application/zip) atool --list -- "$1" ;; |
| 33 | audio/* | application/octet-stream) mediainfo "$1" || exit 1 ;; |
| 34 | video/* ) |
| 35 | 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)" |
| 36 | [ ! -f "$CACHE" ] && ffmpegthumbnailer -i "$1" -o "$CACHE" -s 0 |
| 37 | image "$CACHE" "$2" "$3" "$4" "$5" "$1" |
| 38 | ;; |
| 39 | */pdf) |
| 40 | 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)" |
| 41 | [ ! -f "$CACHE.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE" |
| 42 | image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1" |
| 43 | ;; |
| 44 | *opendocument*) odt2txt "$1" ;; |
| 45 | application/pgp-encrypted) gpg -d -- "$1" ;; |
| 46 | esac |
| 47 | exit 1 |