1
0
Fork 0

lf: refactor, add jq, improve mimetype detection

This commit is contained in:
Luca Bilke 2024-06-12 22:08:36 +02:00
parent 1f7c1b0508
commit e4b3da5033
No known key found for this signature in database
GPG Key ID: C9E851809C1A5BDE
1 changed files with 38 additions and 68 deletions

View File

@ -43,94 +43,64 @@ run() {
mimetest() {
case "$1" in
*/pdf)
[ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$f" "$CACHE"
image "${CACHE}.jpg"
*/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" | text --language=csv ;;
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv ;;
*/pgp-encrypted) run gpg -d -- "$f" ;;
*/pkix-cert) run openssl x509 -text -noout -in "$f" ;;
*opendocument*) run odt2txt "$f" ;;
*/markdown)
[ ! -f "${CACHE}.txt" ] && run glow -s "$XDG_CONFIG_HOME/glow/style.json" "$f" --width "$w" >"${CACHE}.txt"
text "${CACHE}.txt"
;;
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar)
run als -- "$f"
exit 1
*/html) run lynx -dump "$f" ;;
*/*json)
[ ! -f "${CACHE}.txt" ] && run jq -C <"$f" >"${CACHE}.txt"
text "${CACHE}.txt"
;;
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip)
run 7z l "$f" | sed 1,11d
exit 1
;;
*/x-cd-image)
run iso-info --no-header -l "$f"
exit 1
;;
*/x-bittorrent)
run transmission-show "$f"
exit 1
;;
*/vnd.sun.xml.writer)
run odt2txt "$f"
exit 1
;;
*/msword)
run catdoc "$f"
exit 1
;;
*/vnd.openxmlformats-officedocument.wordprocessingml.document)
run docx2txt <"$f"
exit 1
;;
*/vnd.ms-excel)
run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv
exit 1
;;
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv
exit 1
*/*yaml)
[ ! -f "${CACHE}.txt" ] && run yq -C <"$f" >"${CACHE}.txt"
text "${CACHE}.txt"
;;
*/*lua) text ;;
*/epub+zip)
[ ! -f "$CACHE" ] && run epub-thumbnailer "$f" "$CACHE" 1024
image "$CACHE"
;;
*/pgp-encrypted)
run gpg -d -- "$f"
exit 1
;;
*/pkix-cert)
run openssl x509 -text -noout -in "$f"
exit 1
;;
image/svg+xml)
[ ! -f "${CACHE}.png" ] && run inkscape --convert-dpi-method=none -o "${CACHE}.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$f"
image "${CACHE}.png"
;;
image/*) image ;;
*/pdf)
[ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$f" "$CACHE"
image "${CACHE}.jpg"
;;
video/*) video ;;
*opendocument*)
run odt2txt "$f"
exit 1
;;
*/markdown)
run glow -s "$XDG_CONFIG_HOME/glow/style.json" "$f" --width "$w"
exit 1
;;
*/html)
run lynx -dump "$f"
exit 1
;;
*text*)
text
exit
;;
image/*) image ;;
*text*) text ;;
*) return 1 ;;
esac
}
CACHE="${XDG_CACHE_HOME}/lf/thumbnail-$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
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}')"
# Special cases
[ "$(dirname "$(realpath "$f")" | rev | cut -d/ -f1 | rev)" = "factscache" ] &&
[ -f "$(dirname "$(dirname "$(realpath "$f")")")/ansible.cfg" ] &&
jq -C <"$f" && exit 1
giorun="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
mimetest "$giorun" && exit 1
filerun="$(file --brief --dereference --mime-type "$f")"
mimetest "$filerun" && exit 1
if command -v gio 1>/dev/null 2>&1; then
giorun="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
mimetest "$giorun" && exit 1
fi
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"