summaryrefslogtreecommitdiff
path: root/mac/.config/lf/scope
diff options
context:
space:
mode:
Diffstat (limited to 'mac/.config/lf/scope')
-rwxr-xr-xmac/.config/lf/scope154
1 files changed, 92 insertions, 62 deletions
diff --git a/mac/.config/lf/scope b/mac/.config/lf/scope
index fd3bae7..2a2970d 100755
--- a/mac/.config/lf/scope
+++ b/mac/.config/lf/scope
@@ -1,89 +1,119 @@
#!/bin/bash
-# File preview handler for lf.
-
set -C -f
IFS="$(printf '%b_' '\n')"
IFS="${IFS%_}"
-image() {
+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/null >/dev/tty 2>/dev/null
+}
+
+draw_image() {
if [ -f "$1" ] && command -v kitty >/dev/null 2>&1; then
- file=$1
- w=$2
- h=$3
- x=$4
- y=$5
- kitty +kitten icat --stdin no --transfer-mode file --place "${w}x${h}@${x}x${y}" "$file" </dev/null >/dev/tty
- mediainfo --Output="Video;%Duration/String%" "$6"
+ kitty_clear_all
+ sleep 0.01
+ kitty +kitten icat --silent --stdin no --transfer-mode file \
+ --place "${2}x${3}@${4}x${5}" "$1" </dev/null >/dev/tty
else
- chafa "$1" -s "$4x"
- mediainfo "$6"
+ chafa "$1" -s "${2}x${3}"
fi
}
-# 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.
+CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/lf"
+mkdir -p "$CACHE_DIR"
+mime="$(file --dereference --brief --mime-type -- "$1")"
-case "$(file --dereference --brief --mime-type -- "$1")" in
+case "$mime" in
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)"
- [ ! -f "$CACHE" ] && magick "$1" "$CACHE.jpg"
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
- ;;
-image/vnd.djvu)
- 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" ] && djvused "$1" -e 'select 1; save-page-with /dev/stdout' | magick -density 200 - "$CACHE.jpg" >/dev/null 2>&1
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
- ;;
-image/svg+xml | image/png)
- 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" ] && inkscape --convert-dpi-method=none -o "$CACHE.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$1"
- image "$CACHE.png" "$2" "$3" "$4" "$5" "$1"
- ;;
-image/x-xcf | image/jpg | image/jpeg)
- 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}')"
- [ ! -f "$CACHE" ] && magick "$1[0]" "$CACHE.jpg"
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
- ;;
-image/*) image "$1" "$2" "$3" "$4" "$5" "$1" ;;
-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 ;;
+ 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)
- # 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.jpg" -s 0 -q 5
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
+ 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)
- 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" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
+ 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)
- 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" ] && unoconv -f jpg -o "$CACHE.jpg" "$1"
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
+ 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)
- 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)"
- OUT_DIR="$(dirname "$CACHE")"
- [ ! -f "$CACHE" ] && libreoffice --headless --convert-to jpg --outdir "$OUT_DIR" "$1" && mv "$OUT_DIR/$(basename "$1" .hwp).jpg" "$CACHE.jpg"
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
+ 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*)
- 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" ] && gnome-epub-thumbnailer "$1" "$CACHE.jpg"
- image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
+ 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/*zip) atool --list -- "$1" ;;
-*opendocument*) odt2txt "$1" ;;
-application/pgp-encrypted) gpg -d -- "$1" ;;
application/vnd.openxmlformats-officedocument.wordprocessingml.document)
- 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.txt" ] && pandoc "$1" -t plain -o "$CACHE.txt"
- bat -p --theme ansi --terminal-width "$(($4 - 2))" -f "$CACHE.txt"
+ 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