dotfiles/terminal/.config/vifm/scripts/preview
2020-05-23 02:31:50 +02:00

114 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
X="$2"
Y="$3"
WIDTH="$4"
HEIGHT="$5"
TMP="/tmp/vifm-preview"
# Create temporary working directory if the directory structure doesn't exist
function maketemp() {
if [ ! -d "$(dirname "$TMP$1")" ]; then
mkdir -p "$(dirname "$TMP$1")"
fi
}
function fileclean() {
if [ -f "$TMP$1.png" ]; then
rm -f "$TMP$1.png"
elif [ -d "$TMP$1/" ]; then
rm -rf "$TMP$1/"
fi
}
function previewclear() {
kitty +kitten icat --transfer-mode=file --silent --clear
}
function text() {
bat --pager=never --wrap never --style="changes" --color="always" "$1" -p
}
function torrent() {
aria2c --show-files "$1"
}
function archive() {
atool -l -q "$1" | tail -n +2 | awk -F' ' '{print $NF}'
}
function draw() {
kitty +kitten icat --transfer-mode=file --silent --align=left --place=${WIDTH}x${HEIGHT}@${X}x${Y} "$1"
}
function image() {
draw "$1"
}
function audio() {
maketemp "$1"
if [ ! -f "$TMP$1.png" ]; then
# https://ffmpeg.org/ffmpeg-filters.html#showspectrum-1
ffmpeg -loglevel 0 -y -i "$1" -lavfi "showspectrumpic=s=hd480:legend=0:gain=5:color=intensity" "$TMP$1.png"
fi
draw "$TMP$1.png"
}
function video() {
maketemp "$1"
if [ ! -f "$TMP$1.png" ]; then
ffmpegthumbnailer -i "$1" -o "$TMP$1.png" -s 1024 -q 10
fi
draw "$TMP$1.png"
}
function pdf() {
maketemp "$1"
if [ ! -f "$TMP$1.png" ]; then
pdftoppm -png -singlefile "$1" "$TMP$1" -scale-to 1024
fi
draw "$TMP$1.png"
}
function epub() {
maketemp "$1"
if [ ! -f "$TMP$1.png" ]; then
epub-thumbnailer "$1" "$TMP$1.png" 1024
fi
draw "$TMP$1.png"
}
function main() {
mimetype=$(file -b --mime-type "$1")
previewclear
if [ $mimetype != "inode/directory" ]; then
fileclean "$1"
fi
case $mimetype in
application/epub*)
epub "$1";;
application/pdf)
pdf "$1";;
application/x-bittorrent)
torrent "$1";;
application/zip | application/x-tar | *rar )
archive "$1";;
audio/*)
audio "$1";;
image/*)
image "$1";;
text/*)
text "$1";;
video/*)
video "$1";;
inode/directory)
exa --color always "$1";;
*);;
esac
}
main "$1"