| 1 | #!/bin/sh |
| 2 | # lf previewer: $1 file, $2 width, $3 height (cells). Exit 0 = lf may cache the output (set preload). |
| 3 | |
| 4 | cache="${XDG_CACHE_HOME:-$HOME/.cache}/lf" |
| 5 | # thumbnail path for $1: changes when the file does |
| 6 | thumb() { |
| 7 | mkdir -p "$cache" |
| 8 | printf '%s/thumb.%s' "$cache" "$(stat -L --printf '%n\0%i\0%s\0%Y' -- "$1" | sha256sum | cut -d' ' -f1)" |
| 9 | } |
| 10 | image() { |
| 11 | chafa -f sixel -s "${2}x${3}" --animate off --polite on -t 1 --bg black -- "$1" |
| 12 | } |
| 13 | |
| 14 | case "$(file -Lb --mime-type -- "$1")" in |
| 15 | image/svg+xml) |
| 16 | t=$(thumb "$1").png |
| 17 | [ -s "$t" ] || magick -background none -density 192 "$1" -resize 1024x1024 "$t" |
| 18 | image "$t" "$2" "$3" |
| 19 | ;; |
| 20 | image/*) |
| 21 | image "$1" "$2" "$3" |
| 22 | ;; |
| 23 | video/*) |
| 24 | t=$(thumb "$1").jpg |
| 25 | [ -s "$t" ] || ffmpegthumbnailer -i "$1" -o "$t" -s 1024 -q 6 >/dev/null 2>&1 |
| 26 | image "$t" "$2" "$3" |
| 27 | ;; |
| 28 | application/pdf) |
| 29 | t=$(thumb "$1") |
| 30 | [ -s "$t.jpg" ] || pdftoppm -jpeg -f 1 -l 1 -singlefile -scale-to 1024 -- "$1" "$t" |
| 31 | image "$t.jpg" "$2" "$3" |
| 32 | ;; |
| 33 | audio/*) |
| 34 | ffprobe -v error -show_entries format=duration,bit_rate:format_tags -of default=nw=1 -- "$1" | |
| 35 | sed 's/^TAG://' |
| 36 | ;; |
| 37 | application/zip|application/x-tar|application/gzip|application/x-7z-compressed|application/vnd.rar|\ |
| 38 | application/x-rar|application/x-xz|application/x-bzip2|application/zstd|application/x-zstd|\ |
| 39 | application/java-archive|application/x-iso9660-image|application/x-cpio|application/vnd.debian.binary-package) |
| 40 | bsdtar -tf "$1" |
| 41 | ;; |
| 42 | text/*|application/json|application/*xml|application/javascript|application/x-subrip|application/x-ndjson|\ |
| 43 | application/toml|application/x-wine-extension-ini|application/pgp-keys|inode/x-empty) |
| 44 | bat --color=always --style=numbers --paging=never --terminal-width "$2" -- "$1" |
| 45 | ;; |
| 46 | *) |
| 47 | file -Lb -- "$1" |
| 48 | ;; |
| 49 | esac |