From de17c084e8cd0257006d3cfc85966a34ed60858e Mon Sep 17 00:00:00 2001 From: Luca Bilke Date: Wed, 24 Jan 2024 09:42:18 +0100 Subject: [PATCH] improve preview script --- .config/lf/preview | 103 +++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 45 deletions(-) diff --git a/.config/lf/preview b/.config/lf/preview index cf62f0fc..7bc9d61c 100755 --- a/.config/lf/preview +++ b/.config/lf/preview @@ -5,12 +5,18 @@ RESET="\033[0m" set -C -f +f=$1 +w=$2 +h=$3 +x=$4 +y=$5 + image() { - f=$1 - w=$2 - h=$3 - x=$4 - y=$5 + f=$1 + w=$2 + h=$3 + x=$4 + y=$5 if [ -f "$1" ] && [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1; then printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y": "%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' "$(($4 + 3))" "$(($5 + 1))" "$(($2 - 6))" "$(($3 - 2))" "$1" >"$FIFO_UEBERZUG" fi @@ -18,11 +24,11 @@ image() { } video() { - f=$1 - w=$2 - h=$3 - x=$4 - y=$5 + f=$1 + w=$2 + h=$3 + x=$4 + y=$5 thumb="$(vidthumb "$f")" || ( printf "%b" "\033[31mvidthumb script not in path\033[0m" return 1 @@ -35,8 +41,8 @@ batorcat() { w=$2 command -v bat >/dev/null 2>&1 && bat --color=always --style=plain --pager=never --terminal-width "$((w - 2))" "$f" && exit 0 command -v batcat >/dev/null 2>&1 && batcat --color=always --style=plain --pager=never --terminal-width "$((w - 2))" "$f" && exit 0 - cat "$f" && exit 0 - exit 1 + cat "$f" + exit 0 } run() { @@ -52,42 +58,49 @@ run() { fi } +mimetest() { + case "$1" in + text/*) batorcat "$f" "$w" ;; + */pdf) + [ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$f" "$CACHE" + image "${CACHE}.jpg" "$w" "$h" "$x" "$y" + ;; + */x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run als -- "$f" ;; + */x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip) run 7z l "$f" | sed 1,11d ;; + */x-cd-image) run iso-info --no-header -l "$f" ;; + */x-bittorrent) run transmission-show "$f" ;; + */vnd.sun.xml.writer) run odt2txt "$f" ;; + */msword) run catdoc "$f" ;; + */vnd.openxmlformats-officedocument.wordprocessingml.document) run docx2txt <"$f" ;; + */vnd.ms-excel) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | batorcat --language=csv ;; + */vnd.openxmlformats-officedocument.spreadsheetml.sheet) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | batorcat --language=csv ;; + */epub+zip) + [ ! -f "$CACHE" ] && run epub-thumbnailer "$f" "$CACHE" 1024 + image "$CACHE" "$w" "$h" "$x" "$y" + ;; + */pgp-encrypted) run gpg -d -- "$f" ;; + */pkix-cert) run openssl x509 -text -noout -in "$f" ;; + image/svg+xml) + [ ! -f "$CACHE" ] && run inkscape --convert-dpi-method=none -o "$CACHE" --export-overwrite -D --export-png-color-mode=RGBA_16 "$f" + image "$CACHE" "$w" "$h" "$x" "$y" + ;; + image/*) image "$f" "$w" "$h" "$x" "$y" ;; + video/*) video "$f" "$w" "$h" "$x" "$y" ;; + *opendocument*) run odt2txt "$f" ;; + */markdown) run lowdown -Tterm "$f" ;; + */html) run lynx -dump "$f" ;; + *) return 1 ;; + esac +} + CACHE="$HOME/.cache/lf/thumbnail-$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')" - -# We set the desktop to gnome to force usage of `gio` over `file` for better detection -case "$(env XDG_CURRENT_DESKTOP=GNOME xdg-mime query filetype "$1")" in -*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run als -- "$1" ;; -*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz) run 7z l "$1" | sed 1,11d ;; -*/x-cd-image) run iso-info --no-header -l "$1" ;; -*/x-bittorrent) run transmission-show "$1" ;; -*/vnd.sun.xml.writer) run odt2txt "$1" ;; -*/msword) run catdoc "$1" ;; -*/vnd.openxmlformats-officedocument.wordprocessingml.document) run docx2txt <"$1" ;; -*/vnd.ms-excel) run ssconvert --export-type=Gnumeric_stf:stf_csv "$1" "fd://1" | batorcat --language=csv ;; -*/vnd.openxmlformats-officedocument.spreadsheetml.sheet) run ssconvert --export-type=Gnumeric_stf:stf_csv "$1" "fd://1" | batorcat --language=csv ;; -*/epub+zip) - [ ! -f "$CACHE" ] && run epub-thumbnailer "$1" "$CACHE" 1024 - image "$CACHE" "$2" "$3" "$4" "$5" - ;; -*/pgp-encrypted) run gpg -d -- "$1" ;; -*/pkix-cert) run openssl x509 -text -noout -in "$1" ;; -image/svg+xml) - [ ! -f "$CACHE" ] && run inkscape --convert-dpi-method=none -o "$CACHE" --export-overwrite -D --export-png-color-mode=RGBA_16 "$1" - image $CACHE "$2" "$3" "$4" "$5" - ;; -image/*) image "$1" "$2" "$3" "$4" "$5" ;; -video/*) video "$1" "$2" "$3" "$4" "$5" ;; -*opendocument*) run odt2txt "$1" ;; -*/markdown) run lowdown -Tterm "$1" ;; -*/html) run lynx -dump "$1" ;; -text/* | */json | */schema+json | */xml | */javascript) batorcat "$1" "$2" ;; -*/pdf) - [ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE" - image "${CACHE}.jpg" "$2" "$3" "$4" "$5" - ;; +case $(file -b "$f") in + *ascii* | *utf*) batorcat "$f" "$w";; esac +mimetest "$(env XDG_CURRENT_DESKTOP=GNOME xdg-mime query filetype "$f")" && exit +mimetest "$(xdg-mime query filetype "$f")" && exit 0 -xdg-mime query filetype "$1" +env XDG_CURRENT_DESKTOP=GNOME xdg-mime query filetype "$1" mediainfo "$1" exit 1