scripts: further improve preview script

Switch to bash to utilize pattern matching.
This commit is contained in:
Hoang Nguyen 2024-02-08 00:00:00 +07:00
parent 9822f8b08a
commit 55dc973260
Signed by: folliehiyuki
GPG Key ID: B0567C20730E9B11
2 changed files with 167 additions and 70 deletions

View File

@ -7,6 +7,9 @@ set vicmd=nvim
set quickview
set previewoptions=graphicsdelay:50000,hardgraphicsclear
" Don't wrap text in quickview
set nowrap
" This makes vifm perform file operations on its own instead of relying on
" standard utilities like `cp`. While using `cp` and alike is a more universal
" solution, it's also much slower when processing large amounts of files and
@ -317,13 +320,18 @@ filetype *.mbox aerc %f
" ------------------------------------------------------------------------------
{% set preview_cmd = xdg_dir.libexec_dir ~ '/preview %c:p %pw %ph' -%}
" Overwrite the image previewer
fileviewer <image/vnd.djvu>,<image/x-djvu>
\ {{ preview_cmd }} 2>/dev/null
" https://www.arewesixelyet.com/
if $TERM != 'alacritty'
fileviewer <image/*>,<video/*>,<application/x-font*>,<font/*>
\ {{ xdg_dir.libexec_dir }}/preview %c:p %pw %ph %pd %N 2>/dev/null
\ {{ preview_cmd }} %pd %N 2>/dev/null
endif
fileviewer <*> {{ xdg_dir.libexec_dir }}/preview %c:p %pw %ph 2>/dev/null
fileviewer <*> {{ preview_cmd }} 2>/dev/null
" ------------------------------------------------------------------------------

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
_cache_thumbnail() {
thumbnail_dir="${XDG_CACHE_HOME:-$HOME/.cache}/thumbnails"
@ -11,7 +11,16 @@ _cache_thumbnail() {
ffmpegthumbnailer -i "$2" -o "$tmpfile" -s 0
;;
font)
font_preview_text=$(printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s" 'ABCDEFGHIJKLM' 'NOPQRSTUVWXYZ' 'abcdefghijklm' 'nopqrstuvwxyz' '1234567890' '!@$\%<>(){}[]' '== != => <=' '愛してるおかえりなさい')
read -r -d '' font_preview_text <<-EOF
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
abcdefghijklm
nopqrstuvwxyz
1234567890
!@$\%<>(){}[]
== != => <=
愛してるおかえりなさい
EOF
convert -size 900x900 xc:"{{ colors.background }}" \
-gravity center \
-pointsize 40 \
@ -28,11 +37,11 @@ _cache_thumbnail() {
}
_preview_markdown() {
exec mdcat "$1" || glow -s ~/.config/glow/styles/{{ theme }}.json "$1" || lowdown -t term "$1"
mdcat "$1" || glow -s ~/.config/glow/styles/{{ theme }}.json "$1" || lowdown -t term "$1"
}
_preview_image() {
exec chafa --animate=off --polite=on --size="$2"x"$3" "$1" || exiftool "$1"
chafa --animate=off --polite=on --size="$2"x"$3" "$1" || exiftool "$1"
}
_preview_video() {
@ -49,67 +58,147 @@ WIDTH="$2"
HEIGHT="$3"
mimetype=$(file --dereference --brief --mime-type -- "$FILE")
case "$mimetype" in
inode/directory)
# Don't preview the inode ../
if [ -z "${FILE##*/..*}" ]; then
echo ""
else
lsd -1FAL --group-dirs first --icon always --color always "$FILE"
fi
;;
image/vnd.djvu*)
djvutxt "$FILE" || exiftool "$FILE" ;;
image/*)
_preview_image "$FILE" "$WIDTH" "$HEIGHT" ;;
video/*)
_preview_video "$FILE" "$WIDTH" "$HEIGHT" ;;
audio/*)
exiftool "$FILE" || fprobe -hide_banner -pretty "$FILE" 2>&1 ;;
font/*|application/x-font*)
_preview_font "$FILE" "$WIDTH" "$HEIGHT" ;;
application/pdf)
mutool draw -F txt -i -- "$FILE" 1-10 || pdftotext -l 10 -nopgbrk -q -- "$FILE" - || exiftool "$FILE" ;;
application/postscript)
pstotext "$FILE" || ps2ascii "$FILE" ;;
application/x-troff-man)
man "$FILE" | col -b ;;
application/vnd.oasis.opendocument.text*)
odt2txt "$FILE" || (pandoc -s -t markdown -- "$FILE" | _preview_markdown -) ;;
application/epub+zip|application/x-fictionbook+xml|application/vnd.openxmlformats-officedocument.wordprocessingml.document)
pandoc -s -t markdown -- "$FILE" | _preview_markdown - ;;
application/x-bittorrent)
dumptorrent -v "$FILE" || transmission-show -- "$FILE" || exiftool "$FILE" ;;
application/zip|application/zlib|application/java-archive)
zip -sf "$FILE" || zipinfo "$FILE" || bsdtar -tf "$FILE" ;;
application/x-tar*|application/*-compressed-tar)
tar -tvf "$FILE" || bsdtar -tf "$FILE" ;;
application/x-xz*|application/*-xz-compressed)
xz --list "$FILE" || bsdtar -tf "$FILE" ;;
application/vnd.rar|application/vnd.comicbook-rar)
unrar lt -p- -- "$FILE" || bsdtar -tf "$FILE" ;;
application/x-7z-compressed)
7z l -p -- "$FILE" || bsdtar -tf "$FILE" ;;
application/vnd.efi.iso|application/x-*-rom|application/x-compressed-iso)
isoinfo -l -i "$FILE" || bsdtar -tf "$FILE" ;;
application/zstd)
zstd -l "$FILE" ;;
application/x-cpio*)
cpio -tF "$FILE" || bsdtar -tf "$FILE" ;;
application/gzip)
gzip -l "$FILE" ;;
application/x-archive|application/x-xar|application/vnd.ms-cab-compressed|application/x-bzip*\
|application/x-lzma|application/x-lzip|application/x-lha|application/x-lhz\
|application/x-lzop|application/x-lz4|application/x-lrzip)
bsdtar -tf "$FILE" ;;
application/json*|application/geo+json|application/schema+json|application/ld+json)
gojq -C . "$FILE" || jaq --color=always . "$FILE" || bat --style=plain --color=always "$FILE" || python3 -m json.tool -- "$FILE" ;;
application/x-ipynb+json)
notedown --from notebook "$FILE" --to markdown | _preview_markdown - ;;
text/markdown)
_preview_markdown "$FILE" ;;
text/*|application/javascript|application/xhtml+xml)
bat --style plain --color=always "$FILE" ;;
*)
exiftool "$FILE" || (echo '----- File Type Classification -----' && file --dereference --brief -- "$FILE") ;;
esac
filename="$(basename "$FILE")"
if [[ $mimetype == inode/directory ]]; then
# Don't preview the inode ../
if [ -z "${FILE##*/..*}" ]; then
echo ""
else
lsd -1FAL --group-dirs first --icon always --color always "$FILE"
fi
exit
fi
if [[ $filename == *.djvu ]] || [[ $mimetype == @(image/vnd.djvu*|image/x-djvu) ]]; then
djvutxt "$FILE" || exiftool "$FILE"
exit
fi
if [[ $mimetype == image/* ]]; then
_preview_image "$FILE" "$WIDTH" "$HEIGHT"; exit
fi
if [[ $mimetype == video/* ]]; then
_preview_video "$FILE" "$WIDTH" "$HEIGHT"; exit
fi
if [[ $mimetype == audio/* ]]; then
fprobe -hide_banner -pretty "$FILE" 2>&1 || exiftool "$FILE"
exit
fi
if [[ $mimetype == @(font/*|application/x-font*|application/vnd.ms-opentype) ]]; then
_preview_font "$FILE" "$WIDTH" "$HEIGHT"; exit
fi
if [[ $mimetype == application/pdf ]]; then
mutool draw -F txt -i -- "$FILE" 1-10 || pdftotext -l 10 -nopgbrk -q -- "$FILE" - || exiftool "$FILE"
exit
fi
if [[ $mimetype == application/postscript ]]; then
pstotext "$FILE" || ps2ascii "$FILE" || exiftool "$FILE"
exit
fi
if [[ $mimetype == @(text/?(x-)troff|application/x-troff-*) ]]; then
man "$FILE" | col -b
exit
fi
if [[ $mimetype == application/vnd.oasis.opendocument.text* ]]; then
odt2txt "$FILE" || (pandoc -s -t markdown -- "$FILE" | _preview_markdown -)
exit
fi
if [[ $mimetype == application/epub+zip ]]; then
epub2txt "$FILE" || exiftool "$FILE"
exit
fi
if [[ $mimetype == @(application/x-fictionbook+xml|application/vnd.openxmlformats-officedocument.wordprocessingml.document) ]]; then
pandoc -s -t markdown -- "$FILE" | _preview_markdown -
exit
fi
if [[ $mimetype == application/x-bittorrent ]]; then
dumptorrent -v "$FILE" || transmission-show -- "$FILE" || exiftool "$FILE"
exit
fi
if [[ $mimetype == @(application/zip|application/zlib|application/java-archive|application/vnd.android.package-archive|application/x-zip*) ]]; then
zip -sf "$FILE" || zipinfo "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $filename == *.tar.zst ]] || [[ $mimetype == @(application/x-?(g)tar*|application/*-compressed-tar) ]]; then
tar -tvf "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $mimetype == @(application/zstd) ]]; then
zstd -l "$FILE"; exit
fi
if [[ $mimetype == @(application/x-xz*|application/*-xz-compressed) ]]; then
xz --list "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $mimetype == @(application/vnd.rar|application/vnd.comicbook-rar|application/x-rar*) ]]; then
unrar lt -p- -- "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $mimetype == application/x-7z-compressed ]]; then
7z l -p -- "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $mimetype == @(application/vnd.efi.iso|application/x-*-rom|application/x-compressed-iso|application/x*-iso?(9660)-?(app)image) ]]; then
isoinfo -l -i "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $mimetype == application/x-cpio* ]]; then
cpio -tF "$FILE" || bsdtar -tf "$FILE"
exit
fi
if [[ $mimetype == application/?(x-)gzip ]]; then
gzip -l "$FILE"; exit
fi
if [[ $mimetype == @(application/vnd.ms-cab-compressed|application/x-archive|application/?(x-)bzip*\
|application/x-lzma|application/x-lzip|application/x-lha|application/x-lzh-compressed|application/x-lhz\
|application/x-lzop|application/x-lz4|application/x-lrzip) ]]; then
bsdtar -tf "$FILE"; exit
fi
if [[ $mimetype == application/x-ipynb+json ]]; then
notedown --from notebook "$FILE" --to markdown | _preview_markdown -
exit
fi
if [[ $mimetype == @(application/json*|application/*+json) ]]; then
gojq -C . "$FILE" || jaq --color=always . "$FILE" || bat --style=plain --color=always "$FILE" || python3 -m json.tool -- "$FILE"
exit
fi
if [[ $mimetype == text/plain ]] && [[ $filename == *.@(md|mkd|markdown) ]] || [[ $mimetype == text/markdown ]]; then
_preview_markdown "$FILE"; exit
fi
if [[ $mimetype == @(text/*|application/javascript|application/xhtml+xml) ]]; then
bat --style plain --color=always "$FILE"; exit
fi
# Try to get something out of exiftool
# NOTE: exiftool always prints what it can retrieve to stdout, so suppress the message here
metadata="$(exiftool "$FILE")"
if ! (echo "$metadata" | awk -F':' '{print $1}' | grep -q '^Error'); then
echo "$metadata"
else
echo '----- File Type Classification -----' && file --dereference --brief -- "$FILE"
fi