#!/bin/bash set -C -f IFS="$(printf '%b_' '\n')" IFS="${IFS%_}" cache_key() { stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}' } kitty_clear_all() { printf '\033_Ga=d,d=a\033\\' >/dev/tty 2>/dev/null kitty +kitten icat --silent --stdin no --transfer-mode file --clear /dev/tty 2>/dev/null } draw_image() { if [ -f "$1" ] && command -v kitty >/dev/null 2>&1; then kitty_clear_all sleep 0.01 kitty +kitten icat --silent --stdin no --transfer-mode file \ --place "${2}x${3}@${4}x${5}" "$1" /dev/tty else chafa "$1" -s "${2}x${3}" fi } CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/lf" mkdir -p "$CACHE_DIR" mime="$(file --dereference --brief --mime-type -- "$1")" case "$mime" in image/avif) key=$(cache_key "$1") out="$CACHE_DIR/thumb.$key.jpg" [ -f "$out" ] || magick "$1" "$out" >/dev/null 2>&1 draw_image "$out" "$2" "$3" "$4" "$5" ;; image/svg+xml) key=$(cache_key "$1") out="$CACHE_DIR/thumb.$key.png" [ -f "$out" ] || inkscape --convert-dpi-method=none -o "$out" --export-overwrite -D "$1" >/dev/null 2>&1 draw_image "$out" "$2" "$3" "$4" "$5" ;; image/png | image/jpg | image/jpeg) draw_image "$1" "$2" "$3" "$4" "$5" ;; image/x-xcf) key=$(cache_key "$1") out="$CACHE_DIR/thumb.$key.jpg" [ -f "$out" ] || magick "$1[0]" "$out" >/dev/null 2>&1 draw_image "$out" "$2" "$3" "$4" "$5" ;; image/*) draw_image "$1" "$2" "$3" "$4" "$5" ;; text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;; text/troff) man ./ "$1" | col -b ;; text/* | */xml | application/json | application/x-ndjson) bat -p --theme ansi --terminal-width "$(($4 - 2))" -f "$1" ;; audio/*) mediainfo "$1" || exit 1 ;; video/* | application/octet-stream | application/vnd.rn-realmedia) key=$(cache_key "$1") jpg="$CACHE_DIR/thumb.$key.jpg" [ -f "$jpg" ] || ffmpegthumbnailer -i "$1" -o "$jpg" -s 0 -q 5 >/dev/null 2>&1 draw_image "$jpg" "$2" "$3" "$4" "$5" # 필요 시 메타 한 줄: # mediainfo --Output="Video;%Duration/String%" "$1" ;; */pdf) key=$(cache_key "$1") base="$CACHE_DIR/thumb.$key" [ -f "$base.jpg" ] || pdftoppm -jpeg -f 1 -singlefile "$1" "$base" >/dev/null 2>&1 draw_image "$base.jpg" "$2" "$3" "$4" "$5" ;; application/vnd.openxmlformats-officedocument.presentationml.presentation) key=$(cache_key "$1") jpg="$CACHE_DIR/thumb.$key.jpg" [ -f "$jpg" ] || unoconv -f jpg -o "$jpg" "$1" >/dev/null 2>&1 draw_image "$jpg" "$2" "$3" "$4" "$5" ;; application/x-hwp) key=$(cache_key "$1") jpg="$CACHE_DIR/thumb.$key.jpg" outdir="$(dirname "$jpg")" [ -f "$jpg" ] || { libreoffice --headless --convert-to jpg --outdir "$outdir" "$1" >/dev/null 2>&1 && mv "$outdir/$(basename "$1" .hwp).jpg" "$jpg"; } draw_image "$jpg" "$2" "$3" "$4" "$5" ;; */epub+zip | */mobi*) key=$(cache_key "$1") jpg="$CACHE_DIR/thumb.$key.jpg" [ -f "$jpg" ] || gnome-epub-thumbnailer "$1" "$jpg" >/dev/null 2>&1 draw_image "$jpg" "$2" "$3" "$4" "$5" ;; application/*zip) atool --list -- "$1" ;; *opendocument*) odt2txt "$1" ;; application/pgp-encrypted) gpg -d -- "$1" ;; application/vnd.openxmlformats-officedocument.wordprocessingml.document) key=$(cache_key "$1") txt="$CACHE_DIR/thumb.$key.txt" [ -f "$txt" ] || pandoc "$1" -t plain -o "$txt" >/dev/null 2>&1 bat -p --theme ansi --terminal-width "$(($4 - 2))" -f "$txt" ;; esac exit 1