move lf previewer from dotfiles repo to this repo

This commit is contained in:
2024-03-09 23:47:31 +00:00
parent b8fbaabdf5
commit b05eaf664c

43
file_previewer.sh Executable file
View File

@ -0,0 +1,43 @@
#!/bin/sh
# File preview handler for lf.
preview_filepath="/tmp/lf_preview_image.png"
case "$(file --dereference --brief --mime-type -- "$1")" in
# gzip or doc or ISO or docx
application/gzip | application/msword | application/x-iso9660-image | application/vnd.openxmlformats-officedocument.wordprocessingml.document)
exiftool "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
application/json)
jq --color-output . "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
application/pdf)
pdftoppm -jpeg "$1" -singlefile | chafa --size="$(($2-4))"x"$3" --animate off
pdfinfo "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
application/vnd.sqlite3)
sqlite3 "$1" "SELECT * FROM sqlite_master WHERE type = 'table';" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
application/x-pcapng | application/octet-stream) # `mimetype` gives the first mimetype while `file` gives the second
tshark -r "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;; # the --read-file option does not seem to work
application/zip)
unzip -lv "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
audio/*)
exiftool -Picture -b "$1" | chafa --size="$(($2-4))"x"$3" --animate off || chafa cover.* --size="$(($2-4))"x"$3" --animate off
exiftool "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
video/*)
ffmpeg -ss 00:00:00 -i "$1" -frames:v 1 "$preview_filepath"
chafa "$preview_filepath" --size="$(($2-4))"x"$3"
rm "$preview_filepath" # removing preview file so that lf looks for a new preview image instead of going for an old one
exiftool "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
image/*)
chafa "$1" --size="$(($2-4))"x"$3" --animate off
exiftool "$1" | bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization;;
*)
bat --theme='base16' --terminal-width "$(($2-4))" --force-colorization "$1"
esac