scripts: improve preview script

* Use the same script to preview everything for both fzf and vifm.
* Image preview works with fzf now.
* Solely rely on mimetypes instead of file extensions.
* Merge genfontimage script into the preview script
This commit is contained in:
Hoang Nguyen 2024-02-07 00:00:00 +07:00
parent ac32f4d838
commit 9822f8b08a
Signed by: folliehiyuki
GPG Key ID: B0567C20730E9B11
9 changed files with 131 additions and 221 deletions

View File

@ -35,7 +35,7 @@ Plans for the future of this dotfiles.
- [ ] [vivid](https://github.com/sharkdp/vivid) themes
- [ ] waybar: pulseaudio module -> wireplumber module (wait for more features, e.g. scrolling)
- [ ] html2text, html2org
- [ ] Build podman container images with ansible-bender
- [ ] Build podman container images with ansible-bender or stacker
- [ ] Add `target-determinator` and `aspect-cli` to packages (or containers, with underlying `bazel`)
## Sandboxing

View File

@ -1,55 +0,0 @@
#!/bin/sh
# https://github.com/eylles/vifm-sixel-preview/pull/2
# https://github.com/vifm/vifm/issues/419#issuecomment-485918513
# require ImageMagick, chafa, ffmpegthumbnailer
thumbnail_dir="${XDG_CACHE_HOME:-$HOME/.cache}/vifm/thumbnails"
[ ! -d "${thumbnail_dir}" ] && mkdir -p "${thumbnail_dir}"
WIDTH="$2"
HEIGHT="$3"
TMP="${thumbnail_dir}/thumbnail.$(stat -c '%n%i%F%s%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
_show_graphics() {
exec chafa --animate off -s "${WIDTH}x${HEIGHT}" "$1"
}
_preview_video() {
[ ! -f "${TMP}.jpg" ] && ffmpegthumbnailer -i "$1" -o "${TMP}.jpg" -s 0
_show_graphics "${TMP}.jpg"
}
_preview_image() {
[ ! -f "${TMP}.jpg" ] && convert -- "$1"'[0]' "${TMP}.jpg"
_show_graphics "${TMP}.jpg"
}
_preview_font() {
[ ! -f "${TMP}.jpg" ] && ~/.local/libexec/genfontimage "$1" "${TMP}.jpg"
_show_graphics "${TMP}.jpg"
}
case "$1" in
# First check common extensions
*.png|*.PNG|*.jpg|*.JPG|*.bmp|*.jpeg|*.gif|*.xpm|*.webp|*.WEBP) _show_graphics "$1" ;;
*.avi|*.mp4|*.wmv|*.dat|*.3gp|*.vob|*.ogv|*.mkv|*.mpg|*.mpeg|*.fli|*.flc|*.flv|*.m2v|*.webm|*.mts|*.qt|*.divx|*.asf|*.asx|*.m4v|*.mov)
_preview_video "$1" ;;
# Then fallback to mimetypes
*)
mimetype=$(file --dereference --brief --mime-type -- "$1")
case "$mimetype" in
image/vnd.djvu*)
djvutxt "$1" || exiftool "$1" ;;
image/*)
_preview_image "$1" ;;
application/x-font*|font/*)
_preview_font "$1" ;;
video/*)
_preview_video "$1" ;;
*)
echo '----- File Type Classification -----' && file --dereference --brief -- "$1" ;;
esac ;;
esac

View File

@ -5,15 +5,15 @@
state: directory
mode: '755'
- name: vifm | Copy config
- name: vifm | Synchronize scripts and colors
ansible.posix.synchronize:
src: vifm/
dest: '{{ xdg_dir.config_home }}/vifm/'
recursive: true
delete: true
- name: vifm | Copy preview script
- name: vifm | Copy vifmrc
template:
src: vifm/preview_vifm.j2
dest: '{{ xdg_dir.config_home }}/vifm/scripts/preview_vifm'
mode: '755'
src: vifm/vifmrc.j2
dest: '{{ xdg_dir.config_home }}/vifm/vifmrc'
mode: '644'

View File

@ -159,7 +159,7 @@ set -gx FZF_DEFAULT_OPTS "--multi --layout=reverse --inline-info --cycle --separ
--color separator:{{ colors.highlight }},label:{{ colors.white2 }},border:{{ colors.highlight }}"
{% endif %}
set -gx FZF_DEFAULT_COMMAND "fd --type f --follow --hidden --exclude .git --color never"
set -gx FZF_CTRL_T_OPTS "--no-height --preview '$HOME/.local/libexec/preview {} 2>/dev/null'"
set -gx FZF_CTRL_T_OPTS "--no-height --preview '{{ xdg_dir.libexec_dir }}/preview {} \$FZF_PREVIEW_COLUMNS \$FZF_PREVIEW_LINES 2>/dev/null'"
set -gx FZF_CTRL_T_COMMAND "fd . \$dir --follow --hidden --exclude .git --color never"
set -gx FZF_ALT_C_OPTS "--preview 'lsd -1FAL --group-dirs first --icon always --color always {} 2>/dev/null'"
set -gx FZF_ALT_C_COMMAND "fd --type d --follow --hidden --exclude .git --color never"

View File

@ -1,46 +0,0 @@
#!/bin/sh
case "$1" in
# First check common extensions
*.wav|*.mp3|*.flac|*.m4a|*.ape|*.ac3|*.oga|*.ogg|*.ogx|*.spx|*.dsf|*.opus|*.dff|*.wma|*.wvc|*.wv) exiftool "$1" ;;
*.pdf) mutool draw -F txt -i -- "$1" 1-10 || pdftotext -l 10 -nopgbrk -q -- "$1" - || exiftool "$1" ;;
*.ps) pstotext "$1" || ps2ascii "$1" ;;
*.epub|*.fb2) pandoc -s -t markdown -- "$1" ;;
*.djvu) djvutxt "$1" || exiftool "$1" ;;
*.html|*.xhtml|*.htm) pandoc -s -t markdown -- "$1" || lynx -dump -- "$1" ;;
*.mkd|*.md|*.markdown) glow -s ~/.config/glow/styles/{{ theme }}.json "$1" || lowdown -t term "$1" || mdcat "$1" ;;
*.ipynb) notedown --from notebook "$1" --to markdown | pandoc -f markdown -t plain ;;
*.torrent) dumptorrent -v "$1" || transmission-show -- "$1" ;;
*.zip|*.war|*.ear|*.oxt|*.tar|*.tgz|*.tar.gz|*.tbz2|*.tar.bz2|*.tar.txz|*.txz|*.rar|*.7z|*.ace|*.rpm|*.deb|*.xbps|*.Z|*.lzo|*.lzma|*.lha|*.cpio|*.jar|*.lz|*.lzh)
bsdtar -tf "$1" ;;
*.iso) isoinfo -l -i "$1" ;;
*.doc) catdoc -- "$1" || pandoc -s -t markdown -- "$1" ;;
*.docx) pandoc -s -t markdown -- "$1" ;;
*.odt|*.ott|*.stw|*.sxc) pandoc -s -t markdown -- "$1" || odt2txt "$1" ;;
*.json|*.jsonc) gojq -C . "$1" || jaq --color always . "$1" || jq . "$1" || python3 -m json.tool -- "$1" ;;
*.[1-8]) man "$1" | col -b ;;
# Then fallback to mimetypes
*)
mimetype=$(file --dereference --brief --mime-type -- "$1")
case "$mimetype" in
inode/directory)
# Don't preview the inode ../
if [ -z "${1##*/..*}" ]; then
echo ""
else
lsd -1FAL --group-dirs first --icon always --color always "$1"
fi ;;
application/zip|application/x-tar*|application/*-compressed-tar\
|application/vnd.rar|application/x-7z-compressed|application/x-xz*\
|application/x-bzip|application/gzip|application/zstd|application/zlib\
|application/x-lzma|application/x-lzip|application/x-lha|application/x-lhz\
|application/x-lzop|application/x-lz4|application/x-lrzip)
bsdtar -tf "$1" ;;
application/json)
gojq -C . "$1" || jaq --color always . "$1" || jq . "$1" || python3 -m json.tool -- "$1" || bat --style=plain --color=always "$1" ;;
text/*|application/javascript)
bat --style=plain --color=always "$1" ;;
*)
echo '----- File Type Classification -----' && file --dereference --brief -- "$1" ;;
esac ;;
esac

View File

@ -131,19 +131,16 @@ command! df df -h %m 2> /dev/null
command! diff nvim -d %f %F
command! gzip tar -cvzf %a %f
command! xz bsdtar -cvJf %a %f
command! zstd zstd -zo %a %f
command! extract bsdtar -xvf %c
command! shred shred -fu %f
command! run !! ./%f
command! make !!make %a
command! mkcd :mkdir %a | cd %a
command! vgrep nvim "+grep %a"
command! reload :write | restart
command! exif exiftool -all= -overwrite_original %f
" fzf file
command! FZFfile : set noquickview
\| let $FZF_RES = term('fzf --prompt "File: " --no-multi --preview "$HOME/.local/libexec/preview {} 2>/dev/null" 2>&0')
\| let $FZF_RES = term('fzf --prompt "File: " --no-multi --preview "{{ xdg_dir.libexec_dir }}/preview {} \$FZF_PREVIEW_COLUMNS \$FZF_PREVIEW_LINES 2>/dev/null" 2>&0')
\| if $FZF_RES != ''
\| execute 'goto' fnameescape($FZF_RES)
\| endif
@ -173,8 +170,8 @@ mark D ~/Downloads
mark P ~/Pictures
mark V ~/Videos
mark U ~/Music
mark f ~/.config
mark l ~/.local/share
mark f {{ xdg_dir.config_home }}
mark l {{ xdg_dir.data_home }}
mark e /etc
mark u /usr/share
mark o /opt
@ -202,27 +199,23 @@ filextype {*.ps,*.eps,*.ps.gz},<application/postscript>
filextype {*.mobi,*.pdb}
\ ebook-viewer %f,
\ FBReader %f,
filextype {*.djvu,*.epub,*.cbr,*.cbz,*.cbt,*.cb7},<image/vnd.djvu>
filextype {*.djvu,*.epub,*.cbr,*.cbz,*.cbt,*.cb7}
\ zathura %f,
" Audio
filetype {*.wav,*.mp3,*.flac,*.m4a,*.wma,*.ape,*.ac3,*.og[agx],*.spx,*.opus},<audio/*>
\ ffplay -nodisp -autoexit %c,
\ ffplay -hide_banner -nodisp -autoexit %c,
\ mplayer %c,
\ mpv --no-video %c %s,
" Video
filextype {*.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,
\*.fl[icv],*.m2v,*.mov,*.webm,,*.m4v,*.r[am],*.divx,
\*.as[fx]},<video/*>
filextype {*.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,*.fl[icv],*.m2v,*.mov,*.webm,,*.m4v,*.r[am],*.divx,*.as[fx]},<video/*>
\ mpv %c %f,
\ ffplay -fs -hide_banner -autoexit %f,
\ ffplay -hide_banner -autoexit %f,
\ mplayer %f,
" Web
filetype {*.html,*.htm,*.xhtml},<text/html>
\ nvim %f,
filextype {*.html,*.htm,*.xhtml},<text/html>
filextype {*.html,*.htm,*.xhtml}
\ firefox %f &,
\ qutebrowser %f %i &,
\ librewolf %f &,
@ -233,7 +226,7 @@ filextype {*.html,*.htm,*.xhtml},<text/html>
filetype {*.o},<application/x-object> nm %f | less
" Man page
filetype {*.[1-8]},<text/troff> man ./%c
filetype {*.[1-8]} man ./%c
" Images
filextype {*.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm},<image/*>
@ -267,13 +260,6 @@ filetype *.sha256
filetype *.sha512
\ sha512sum -c %f %S,
" GPG signature
filetype {*.asc},<application/pgp-signature>
\ !!gpg --verify %c,
" Torrent
filetype {*.torrent},<application/x-bittorrent> rtorrent %f &
" FuseZipMount
filetype {*.zip,*.jar,*.war,*.ear,*.oxt,*.apkg},<application/zip,application/java-archive>
\ {Mount with fuse-zip}
@ -332,21 +318,12 @@ filetype *.mbox aerc %f
" ------------------------------------------------------------------------------
" https://www.arewesixelyet.com/
if $TERM == 'alacritty'
let $GRAPHICS_PREVIEW_MODE = 'ansi'
else
let $GRAPHICS_PREVIEW_MODE = 'graphics'
if $TERM != 'alacritty'
fileviewer <image/*>,<video/*>,<application/x-font*>,<font/*>
\ {{ xdg_dir.libexec_dir }}/preview %c:p %pw %ph %pd %N 2>/dev/null
endif
if $GRAPHICS_PREVIEW_MODE == 'graphics'
fileviewer <image/*>,<video/*>,<application/x-font*>,<font/*>
\ preview_graphics %c:p %pw %ph %pd %N 2>/dev/null
else
fileviewer <image/*>,<video/*>,<application/x-font*>,<font/*>
\ preview_graphics %c:p %pw %ph 2>/dev/null
endif
fileviewer <*> preview_vifm %c:p 2>/dev/null
fileviewer <*> {{ xdg_dir.libexec_dir }}/preview %c:p %pw %ph 2>/dev/null
" ------------------------------------------------------------------------------

View File

@ -56,7 +56,6 @@
force: true
mode: '755'
loop:
- genfontimage
- preview
- name: Install external scripts with curl

View File

@ -1,25 +0,0 @@
#!/bin/sh
PREVIEW_TEXT="ABCDEFGHIJKLM
NOPQRSTUVWXYZ
abcdefghijklm
nopqrstuvwxyz
1234567890
!@$\%<>(){}[]
== != => <=
愛してるおかえりなさい"
BG="{{ colors.background }}"
FG="{{ colors.white2 }}"
if [ "$#" -ne 2 ]; then
echo "Usage: ${0##*/} filename output_image"
exit 1
fi
convert -size 800x800 xc:"$BG" \
-gravity center \
-pointsize 40 \
-font "$1" \
-fill "$FG" \
-annotate +0+0 "$PREVIEW_TEXT" \
-flatten "$2"

View File

@ -1,55 +1,115 @@
#!/bin/sh
case "$1" in
# First check common extensions
*.png|*.PNG|*.jpg|*.JPG|*.bmp|*.jpeg|*.gif|*.xpm|*.webp|*.WEBP) exiftool "$1" || identify "$1" ;;
*.wav|*.mp3|*.flac|*.m4a|*.ape|*.ac3|*.oga|*.ogg|*.ogx|*.spx|*.dsf|*.opus|*.dff|*.wma|*.wv|*.wvc) exiftool "$1" ;;
*.avi|*.mp4|*.wmv|*.dat|*.3gp|*.vob|*.ogv|*.mkv|*.mpeg|*.mpg|*.fli|*.flc|*.flv|*.m2v|*.webm|*.mts|*.qt|*.divx|*.asf|*.asx|*.m4v|*.mov)
exiftool "$1" || fprobe -pretty "$1" 2>&1 ;;
*.pdf) mutool draw -F txt -i -- "$1" 1-10 || pdftotext -l 10 -nopgbrk -q -- "$1" - || exiftool "$1" ;;
*.ps) pstotext "$1" || ps2ascii "$1" ;;
*.epub|*.fb2) pandoc -s -t markdown -- "$1" ;;
*.djvu) djvutxt "$1" || exiftool "$1" ;;
*.html|*.xhtml|*.htm) pandoc -s -t markdown -- "$1" || lynx -dump -- "$1" ;;
*.mkd|*.md|*.markdown) mdcat "$1" || lowdown -t term "$1" || glow -s ~/.config/glow/styles/{{ theme }}.json "$1" ;;
*.ipynb) notedown --from notebook "$1" --to markdown | pandoc -f markdown -t plain ;;
*.torrent) dumptorrent -v "$1" || transmission-show -- "$1" ;;
*.zip|*.jar|*.war|*.ear|*.oxt) zip -sf "$1" || zipinfo "$1" || bsdtar -tf "$1" ;;
*.tar) tar -tvf "$1" || bsdtar -tf "$1" ;;
*.tgz|*.tar.gz) tar -tvf "$1" || bsdtar -tf "$1" ;;
*.tbz2|*.tar.bz2) tar -tvjf "$1" || bsdtar -tf "$1" ;;
*.tar.txz|*.txz) xz --list "$1" || bsdtar -tf "$1" ;;
*.rar) unrar lt -p- -- "$1" || bsdtar -tf "$1" ;;
*.7z) 7z l -p -- "$1" || bsdtar -tf "$1" ;;
*.iso) isoinfo -l -i "$1" ;;
*.doc) catdoc -- "$1" || pandoc -s -t markdown -- "$1" ;;
*.docx) pandoc -s -t markdown -- "$1" ;;
*.odt|*.ott|*.stw|*.sxc) pandoc -s -t markdown -- "$1" || odt2txt "$1" ;;
*.json|*.jsonc) gojq -C . "$1" || jaq --color always . "$1" || jq . "$1" || python3 -m json.tool -- "$1" ;;
*.[1-8]) man "$1" | col -b ;;
# Then fallback to mimetypes
_cache_thumbnail() {
thumbnail_dir="${XDG_CACHE_HOME:-$HOME/.cache}/thumbnails"
[ ! -d "${thumbnail_dir}" ] && mkdir -p "${thumbnail_dir}"
tmpfile="${thumbnail_dir}/$(stat -c '%n%i%F%s%Y' -- "$(readlink -f "$2")" | sha256sum | awk '{print $1}').jpg"
if [ ! -f "$tmpfile" ]; then
case "$1" in
video)
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' '!@$\%<>(){}[]' '== != => <=' '愛してるおかえりなさい')
convert -size 900x900 xc:"{{ colors.background }}" \
-gravity center \
-pointsize 40 \
-font "$2" \
-fill "{{ colors.white2 }}" \
-annotate +0+0 "$font_preview_text" \
-flatten "$tmpfile"
;;
esac
fi
# Return the location
printf "%s" "$tmpfile"
}
_preview_markdown() {
exec 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"
}
_preview_video() {
_preview_image "$(_cache_thumbnail video "$1")" "$2" "$3" || exiftool "$1" || fprobe -hide_banner -pretty "$1" 2>&1
}
_preview_font() {
_preview_image "$(_cache_thumbnail font "$1")" "$2" "$3" || exiftool "$1"
}
# Take 2 additional arguments as width and height (rows/cols)
FILE="$1"
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" ;;
*)
mimetype=$(file --dereference --brief --mime-type -- "$1")
case "$mimetype" in
inode/directory)
lsd -1FAL --group-dirs first --icon always --color always "$1" ;;
application/zip|application/x-tar*|application/*-compressed-tar\
|application/vnd.rar|application/x-7z-compressed|application/x-xz*\
|application/x-bzip|application/gzip|application/zstd|application/zlib\
|application/x-lzma|application/x-lzip|application/x-lha|application/x-lhz\
|application/x-lzop|application/x-lz4|application/x-lrzip)
bsdtar -tf "$1" ;;
image/vnd.djvu*)
djvutxt "$1" || exiftool "$1" ;;
image/*)
exiftool "$1" || identify "$1" ;;
video/*)
exiftool "$1" || fprobe -pretty "$1" 2>&1 ;;
application/json)
gojq -C . "$1" || jaq --color always . "$1" || jq . "$1" || python3 -m json.tool -- "$1" || bat --style=plain --color=always "$1" ;;
text/*|application/javascript)
bat --style plain --color=always "$1" ;;
*)
echo '----- File Type Classification -----' && file --dereference --brief -- "$1" ;;
esac ;;
exiftool "$FILE" || (echo '----- File Type Classification -----' && file --dereference --brief -- "$FILE") ;;
esac