Improve file-preview implementation

This commit is contained in:
ItsDrike 2021-07-26 20:04:29 +02:00
parent 5d13b073ad
commit 72023e777e
No known key found for this signature in database
GPG key ID: B5F6B41F708C3ADD

View file

@ -67,24 +67,51 @@ draw_image() {
exit 1 exit 1
} }
image_handle() { media_handle() {
# Control function for showing/cleaning image previews # Handle media type files (videos, photos). These types of
# files are usually not stored in any form of textually readable
# format and they require a special way of displaying them.
# This mostly uses ueberzug (if available) for this.
draw_script="${XDG_CONFIG_HOME:-$HOME/.config}/lf/draw_img.sh" draw_script="${XDG_CONFIG_HOME:-$HOME/.config}/lf/draw_img.sh"
file="$1" file="$1"
shift shift
if [ -n "$FIFO_UEBERZUG" ] && [ -f "$draw_script" ]; then # Set ENABLED=1 if ueberzug is enabled
draw_image $draw_script $file [ -n "$FIFO_UEBERZUG" ] && [ -f "$draw_script" ] && ENABLED=1
case "$file" in
*.bmp|*.jpg|*.jpeg|*.png|*.xpm)
if [ -n "$ENABLED" ]; then
draw_image $draw_script "$file"
else else
# Fallback to mediainfo preview
echo "@@PREVIEW FALLBACK: Using mediainfo, ueberzug isn't aviable." echo "@@PREVIEW FALLBACK: Using mediainfo, ueberzug isn't aviable."
run_cmd mediainfo "$file" run_cmd mediainfo "$file"
fi fi
;;
*.avi|*.mp4|*.wmv|*.dat|*.3gp|*.ogv|*.mkv|*.mpg|*.mpeg|*.vob|*.fl[icv]|*.m2v|\
*.mov|*.webm|*.ts|*.mts|*.m4v|*.r[am]|*.qt|*.divx)
if [ -n "$ENABLED" ]; then
cache="$(mktemp /tmp/thumbcache.XXXXX)"
ffmpegthumbnailer -i "$file" -o "$cache" -s 0
draw_image $draw_script "$cache"
else
echo "@@PREVIEW FALLBACK: Using exiftool, ueberzug isn't aviable."
run_cmd exiftool "$file"
fi
;;
*.wav|*.mp3|*.flac|*.m4a|*.wma|*.ape|*.ac3|*.og[agx]|*.spx|*.opus|*.as[fx]|*.flac)
# These types can't make use of ueberzug easily, so simply use eixftool
run_cmd exiftool "$file"
;;
*)
echo "@@PREVIEW FALLBACK: Unrecognized media file, falling back to text handle."
text_handle "$file"
;;
esac
} }
text_handle() { text_handle() {
width="$(tput cols)"
echo "Limiting to $width"
# Handle all other formats as text and cat them # Handle all other formats as text and cat them
# if highlighting tools are aviable, try to use them # if highlighting tools are aviable, try to use them
if command -v bat > /dev/null; then if command -v bat > /dev/null; then
@ -117,13 +144,10 @@ elif [ -f "$1" ]; then
*.docx) run_cmd docx2txt "$1" - ;; *.docx) run_cmd docx2txt "$1" - ;;
*.torrent) run_cmd transmission-show "$1";; *.torrent) run_cmd transmission-show "$1";;
*.pdf) run_cmd pdftotext "$1";; *.pdf) run_cmd pdftotext "$1";;
*.bmp|*.jpg|*.jpeg|*.png|*.xpm) image_handle "$1" ;; *.wav|*.mp3|*.flac|*.m4a|*.wma|*.ape|*.ac3|*.og[agx]|*.spx|*.opus|*.as[fx]|*.flac|\
*.wav|*.mp3|*.flac|*.m4a|*.wma|*.ape|*.ac3|*.og[agx]|*.spx|*.opus|*.as[fx]|*.flac) run_cmd exiftool "$1";; *.avi|*.mp4|*.wmv|*.dat|*.3gp|*.ogv|*.mkv|*.mpg|*.mpeg|*.vob|*.fl[icv]|*.m2v|*.mov|\
*.avi|*.mp4|*.wmv|*.dat|*.3gp|*.ogv|*.mkv|*.mpg|*.mpeg|*.vob|*.fl[icv]|*.m2v|*.mov|*.webm|*.ts|*.mts|*.m4v|*.r[am]|*.qt|*.divx) *.webm|*.ts|*.mts|*.m4v|*.r[am]|*.qt|*.divx|\
cache="$(mktemp /tmp/thumbcache.XXXXX)" *.bmp|*.jpg|*.jpeg|*.png|*.xpm) media_handle "$1" ;;
ffmpegthumbnailer -i "$1" -o "$cache" -s 0
image_handle "$cache"
;;
*) text_handle "$1" ;; *) text_handle "$1" ;;
esac esac
fi fi