1
0
Fork 0
dotfiles/common/.config/lf/preview
2025-03-31 11:09:29 +02:00

218 lines
4.8 KiB
Bash
Executable file

#!/bin/sh
# shellcheck disable=SC2016
# Preview functions for each filetype must print their results to stdout.
# The preview functions are called by the "run" wrapper function, which handles caching.
# This wrapper function can take "-s" as an argument to indicate that the
# preview should be regenerated if the terminal is resized.
f="$(realpath "$1")" w=$2 h=$3 _x=$4 _y=$5
rs="$(printf "\037")"
CACHE_DIR="${XDG_CACHE_HOME:-"${HOME}/.cache"}/lf"
BAT="bat \
--style=plain \
--color=always \
--italic-text=always \
--pager=never \
--tabs=4 \
--theme=base16
"
CHAFA="chafa \
-f sixel \
-s ${w}x${h} \
--polite on \
--animate false
"
archive() {
ouch list -Htyq "$f" | tail -n +2
}
csv() {
f=${1:-$f}
mlr --icsv --omd cat "$f" |
CLICOLOR_FORCE=1 COLORTERM=truecolor \
glow -w "$w" -s tokyo-night
}
docx() {
docx2txt "$f" -
}
epub() {
gnome-epub-thumbnailer -s 1024 "$f" /dev/stdout | $CHAFA
}
html() {
lynx -dump "$f"
}
image() {
$CHAFA "$f"
}
iso() {
iso-info --no-header -l "$f"
}
json() {
jq -C . "$f"
}
lua() {
$BAT --language=lua "$f"
}
markdown() {
CLICOLOR_FORCE=1 COLORTERM=truecolor \
glow -w "$w" -s tokyo-night "$f"
}
word() {
catdoc "$f"
}
ods() {
td=$(mktemp -d)
libreoffice --headless --convert-to csv "$f" --outdir "$td" >/dev/null
b=$(basename "$f")
csv "${td}/${b%.*}.csv"
rm -rf "$td"
}
odt() {
odt2txt "$f"
}
pdf() {
pdftoppm -jpeg -f 1 -singlefile "$f" | $CHAFA
}
sqlite() {
printf ".headers on\n.mode list\nSELECT name FROM sqlite_master WHERE type='table';\n" |
sqlite3 "$f" |
grep -v name |
while read -r table; do
[ "$table" != "name" ] &&
printf "SELECT * FROM %s;" "$table" |
sqlite3 -header -separator "$rs" "$f"
done |
column -t -s "$rs"
}
svg() {
inkscape \
--convert-dpi-method=none \
--export-filename=- \
--export-overwrite \
--export-area-drawing \
--export-png-color-mode=RGBA_16 "$f" |
$CHAFA
}
text() {
$BAT "$f"
}
torrent() {
transmission-show "$f"
}
video() {
ffmpegthumbnailer -s 0 -i "$f" -o "/dev/stdout" | $CHAFA
}
yaml() {
$BAT --language=yaml "$f"
}
gz_compressed() {
gzip -l "$f"
}
xz_compressed() {
xz -l "$f"
}
run() {
if [ "$1" = "-s" ]; then
cache="${CACHE_DIR}/$(
stat --printf "%n %i %F %s %W %Y ${w} ${h}" -- "$(readlink -f "$f")" |
sha256sum | awk '{print $1}'
)"
shift
else
cache="${CACHE_DIR}/$(
stat --printf "%n %i %F %s %W %Y" -- "$(readlink -f "$f")" |
sha256sum | awk '{print $1}'
)"
fi
if [ -r "$cache" ]; then
cat "$cache"
else
if out=$("$@"); then
printf "%s" "$out" | tee "$cache"
else
printf "%s" "$out"
fi
fi
}
preview() {
mimetype=$1
case "$mimetype" in
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet) run -s ods ;;
*/vnd.sqlite3) run sqlite ;;
*/gzip) run gz_compressed ;;
*/x-xz) run xz_compressed ;;
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run archive ;;
*/epub+zip) run -s epub ;;
image/svg+xml) run -s svg ;;
image/*) run -s image ;;
*/pdf) run -s pdf ;;
video/*) run -s video ;;
*/vnd.oasis.opendocument.text) run odt ;;
*/vnd.openxmlformats-officedocument.wordprocessingml.document) run docx ;;
*/csv) run -s csv ;;
*/markdown) run -s markdown ;;
*/html) run html ;;
*/*json) run json ;;
*/msword) run word ;;
*/x-bittorrent) run torrent ;;
*/x-cd-image) run iso ;;
*/*lua) run lua ;;
*/*yaml) run yaml ;;
*text*) run text ;;
*) return 1 ;;
esac
}
extension_override() {
case "${f##*.}" in
heex) printf "%s" "text/plain" ;;
*) return 1 ;;
esac
}
# We don't want to reprocess files in the cache, as this will generate a bunch of duplicate cache entries
[ "$(readlink -f "$f" | xargs dirname)" = "${CACHE_DIR}" ] && {
stat -c "%w" "$f"
exit
}
mimetype_override="$(extension_override "$f")"
preview "$mimetype_override" && exit
mimetype_gio="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
preview "$mimetype_gio" && exit
mimetype_file="$(file --brief --dereference --mime-type "$f")"
preview "$mimetype_file" && exit
[ -n "$mimetype_gio" ] && printf '$(gio info)\t\t\t\t : %s\n' "${mimetype_gio}"
[ -n "$mimetype_file" ] && printf '$(file --mime-type)\t\t\t : %s\n' "${mimetype_file}"
mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General"