dotfiles/.local/share/lf/preview

41 lines
869 B
Bash
Executable File

#!/bin/bash
CACHE_DIR=${XDG_CACHE_DIR:-${HOME}/.cache}/lf
[ -d "$CACHE_DIR" ] || mkdir -p "$CACHE_DIR"
cat() {
f=$1
shift
if command -v bat >/dev/null 2>&1; then
bat --color=always --style=numbers --pager=never "$f" $@
else
cat "$f"
fi
}
image() {
if [ -n "$WT_SESSION" ] || [ -n "$TMUX" ]; then
chafa -f symbols -s "$2x$3" --animate off --polite on "$1"
else
chafa -f sixels -s "$2x$3" --animate off --polite on "$1"
fi
}
case "$(file -Lb --mime-type -- "$1")" in
image/*)
image "$1" "$2" "$3" "$4" "$5"
exit 1
;;
application/pdf)
CACHE="${CACHE_DIR}/thumbnail.$(sha256sum "$1" | awk '{print $1}')"
[ ! -f "${CACHE}.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"
image "${CACHE}.jpg" "$2" "$3" "$4" "$5"
;;
text/*|application/json)
cat "$1"
;;
*)
lesspipe.sh "$1" || cat "$1" || "Could not preview file."
;;
esac