2024-10-13 19:49:29 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
set -C -f
|
|
|
|
|
|
|
|
f="$(realpath "$1")" w=$2 h=$3 # x=$4 y=$5
|
|
|
|
|
|
|
|
RED="\033[31m"
|
|
|
|
RESET="\033[0m"
|
|
|
|
RS="$(printf "\037")"
|
|
|
|
CACHE="${XDG_CACHE_HOME}/lf/$(stat --printf "%n\0%i\0%F\0%s\0%W\0%Y" -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
|
2024-10-23 17:12:22 +02:00
|
|
|
BAT="bat --style=plain --color=always --italic-text=always --pager=never --tabs=4 --theme=base16 --line-range=:$h --terminal-width=$((w - 2))"
|
2024-10-13 19:49:29 +02:00
|
|
|
CHAFA="chafa -f sixel -s ${w}x${h} --polite on --animate false"
|
|
|
|
|
|
|
|
run() {
|
|
|
|
#shellcheck disable=SC2068
|
|
|
|
if ! command -v "$1" >/dev/null 2>&1; then
|
|
|
|
printf "%b" "${RED}Cannot execute ${1}${RESET}"
|
|
|
|
elif "$@"; then
|
|
|
|
:
|
|
|
|
else
|
|
|
|
printf "%b" "${RED}Failed to run command: ${RESET}"
|
|
|
|
for l in "$@"; do
|
|
|
|
printf "%b" "\n${RED} ${l}${RESET}"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
cached_text() {
|
|
|
|
if [ -f "$CACHE" ]; then
|
|
|
|
# shellcheck disable=SC2086
|
2024-10-23 17:12:22 +02:00
|
|
|
$BAT "$CACHE"
|
2024-10-13 19:49:29 +02:00
|
|
|
else
|
|
|
|
# shellcheck disable=SC2086
|
2024-10-23 17:12:22 +02:00
|
|
|
run "$@" | tee -p "$CACHE" | $BAT
|
2024-10-13 19:49:29 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
cached_image() {
|
|
|
|
if [ -f "$CACHE" ]; then
|
|
|
|
$CHAFA "$CACHE" && exit 1
|
|
|
|
else
|
|
|
|
run "$@" | tee -p "$CACHE" | $CHAFA && exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
mimetest() {
|
|
|
|
case "$1" in
|
|
|
|
# */pgp-encrypted) run gpg -d -- "$f" ;;
|
|
|
|
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet)
|
|
|
|
if [ -f "$CACHE" ]; then
|
|
|
|
$BAT "$CACHE"
|
|
|
|
else
|
|
|
|
ssconvert --export-type=Gnumeric_stf:stf_assistant -O separator="$RS" "$f" "fd://1" |
|
|
|
|
column -t -s "$RS" | tee -p "$CACHE"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*/vnd.sqlite3)
|
|
|
|
if [ -f "$CACHE" ]; then
|
|
|
|
$BAT "$CACHE"
|
|
|
|
else
|
|
|
|
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" taskchampion.sqlite3
|
|
|
|
done |
|
|
|
|
column -t -s "$RS" | tee -p "$CACHE"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip)
|
|
|
|
if [ -f "$CACHE" ]; then
|
|
|
|
$BAT "$CACHE"
|
|
|
|
else
|
|
|
|
run 7z l "$f" | sed 1,11d | tee -p "$CACHE" | $BAT
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*/epub+zip) cached_image gnome-epub-thumbnailer -s 1024 "$f" "$CACHE" ;;
|
|
|
|
image/svg+xml) cached_image inkscape --convert-dpi-method=none -o "${CACHE}.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$f" ;;
|
|
|
|
image/*) $CHAFA "$f" && exit 1 ;;
|
|
|
|
*/pdf) cached_image pdftoppm -jpeg -f 1 -singlefile "$f" ;;
|
|
|
|
video/*) cached_image ffmpegthumbnailer -s 0 -i "$f" -o "/dev/stdout" ;;
|
|
|
|
*/vnd.oasis.opendocument.text) cached_text odt2txt "$f" ;;
|
|
|
|
*/vnd.openxmlformats-officedocument.wordprocessingml.document) cached_text docx2txt "$f" - ;;
|
|
|
|
*/markdown) cached_text mdless "$f" ;;
|
|
|
|
*/html) cached_text lynx -dump "$f" ;;
|
|
|
|
*/*json) cached_text jq -C . "$f" ;;
|
|
|
|
*/msword) cached_text catdoc "$f" ;;
|
|
|
|
*/x-bittorrent) cached_text transmission-show "$f" ;;
|
|
|
|
*/x-cd-image) cached_text iso-info --no-header -l "$f" ;;
|
|
|
|
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) cached_text als -- "$f" ;;
|
|
|
|
*/*lua) $BAT --language=lua "$f" ;;
|
|
|
|
*/*yaml) $BAT --language=yaml "$f" ;;
|
|
|
|
*text*) $BAT "$f" ;;
|
|
|
|
*) return 1 ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# Special cases
|
|
|
|
[ "$(dirname "$(realpath "$f")" | rev | cut -d/ -f1 | rev)" = "factscache" ] &&
|
|
|
|
[ -f "$(dirname "$(dirname "$(realpath "$f")")")/ansible.cfg" ] &&
|
|
|
|
jq -C <"$f" && exit
|
|
|
|
|
|
|
|
giorun="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
|
|
|
|
mimetest "$giorun" && exit
|
|
|
|
|
|
|
|
filerun="$(file --brief --dereference --mime-type "$f")"
|
|
|
|
mimetest "$filerun" && exit
|
|
|
|
|
|
|
|
printf "%b" "\$(file --mime-type)\t\t\t : $filerun\n"
|
|
|
|
[ -n "$giorun" ] && printf "%b" "\$(gio info)\t\t\t\t : $giorun\n"
|
|
|
|
mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General"
|