Skip to content

Instantly share code, notes, and snippets.

@ret2src
Created August 24, 2023 09:31
Show Gist options
  • Save ret2src/e0477a56ee272743aea9f47945073dcc to your computer and use it in GitHub Desktop.
Save ret2src/e0477a56ee272743aea9f47945073dcc to your computer and use it in GitHub Desktop.
vifm previewer script for images, PDF, ePub, audio, video
fileviewer <image/*>
\ visualpreview image %pw %ph %px %py %c %N
\ %pc
\ visualpreview clear
fileviewer <video/*>
\ visualpreview video %pw %ph %px %py %c %N
\ %pc
\ visualpreview clear
fileviewer <font/*>
\ visualpreview font %pw %ph %px %py %c %N
\ %pc
\ visualpreview clear
fileviewer {*.pdf}
\ visualpreview pdf %pw %ph %px %py %c %N
\ %pc
\ visualpreview clear
fileviewer {*.epub,*.mobi,*.azw,*.azw[0-9]},<application/epub+zip>,<application/x-mobipocket-ebook>,<application/vnd.amazon.ebook>
\ visualpreview epub %pw %ph %px %py %c %N
\ %pc
\ visualpreview clear
#!/usr/bin/env bash
[ -d "$HOME/.cache/vifm" ] || mkdir -p "$HOME/.cache/vifm"
action="${1}"
width="${2}"
height="${3}"
xpos="${4}"
ypos="${5}"
input_file="${6}"
no_detach="${7}"
thumbnail_ext="jpg"
thumbnail_id="$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "${PWD}/${input_file}")" | sha256sum | awk '{print $1}')"
thumbnail_path="${HOME}/.cache/vifm"
thumbnail_file="${thumbnail_path}/${thumbnail_id}.${thumbnail_ext}"
close_preview() {
kitten icat --clear --silent >/dev/tty </dev/tty "${no_detach}"
exit 0
}
show_preview() {
input_file="$1"
width="$2"
height="$3"
xpos="$4"
ypos="$5"
kitten icat --silent --transfer-mode=file --place=${width}x${height}@${xpos}x${ypos} --scale-up "${input_file}" >/dev/tty </dev/tty "${no_detach}"
exit 0
}
case "$action" in
clear)
close_preview
;;
image)
if [ ! -f "${thumbnail_file}" ]; then
imgwidth=$(identify -format '%w' "${input_file}[0]")
if [ "${imgwidth}" -lt 1024 ]; then
convert "${input_file}[0]" -strip -quality 50 -density 72 "${thumbnail_file}"
elif [ "${imgwidth}" -le 1920 ]; then
convert "${input_file}[0]" -thumbnail '50%' -strip -quality 35 -density 72 "${thumbnail_file}"
else
convert "${input_file}[0]" -thumbnail 1024 -strip -quality 35 -density 72 "${thumbnail_file}"
fi
fi
show_preview "${thumbnail_file}" "${width}" "${height}" "${xpos}" "${ypos}"
;;
video)
if [ ! -f "${thumbnail_file}" ]; then
ffmpegthumbnailer -i "${input_file}" -o "${thumbnail_file}" -s 0 -q 5
fi
show_preview "${thumbnail_file}" "${width}" "${height}" "${xpos}" "${ypos}"
;;
epub)
if [ ! -f "${thumbnail_file}" ]; then
epub-thumbnailer "${input_file}" "${thumbnail_file}" 1024
fi
show_preview "${thumbnail_file}" "${width}" "${height}" "${xpos}" "${ypos}"
;;
pdf)
if [ ! -f "${thumbnail_file}" ]; then
pdftoppm -jpeg -f 1 -singlefile "${input_file}" "${thumbnail_file%.${thumbnail_ext}}"
fi
show_preview "${thumbnail_file}" "${width}" "${height}" "${xpos}" "${ypos}"
;;
audio)
if [ ! -f "${thumbnail_file}" ]; then
ffmpeg -i "${input_file}" "${thumbnail_file}" -y >/dev/null
fi
show_preview "${thumbnail_file}" "${width}" "${height}" "${xpos}" "${ypos}"
;;
font)
if [ ! -f "${thumbnail_file}" ]; then
fontpreview -i "${input_file}" -o "${thumbnail_file}"
fi
show_preview "${thumbnail_file}" "${width}" "${height}" "${xpos}" "${ypos}"
;;
*)
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment