diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2020-10-03 19:27:15 -0400 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2020-10-03 19:27:15 -0400 |
commit | e371d9edd474bcf89cf5d462eaccb8638900b390 (patch) | |
tree | ed07118f5c514a55f23a779a1507640e46fd9ed2 /.config/vifm/scripts/vifmimg |
Initial commit
Diffstat (limited to '.config/vifm/scripts/vifmimg')
-rwxr-xr-x | .config/vifm/scripts/vifmimg | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/.config/vifm/scripts/vifmimg b/.config/vifm/scripts/vifmimg new file mode 100755 index 0000000..e45a903 --- /dev/null +++ b/.config/vifm/scripts/vifmimg @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +readonly ID_PREVIEW="preview" + +# Preview an image file directly +function draw() { + declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" + [x]="$2" [y]="$3" [width]="$4" [height]="$5" \ + [path]="${PWD}/$6") \ + > "$FIFO_UEBERZUG" +} + +function font_preview() { + # if no preview found, generate one + if [ ! -f "/tmp${PWD}/$6.png" ]; then + fontpreview -i "$6" -o "/tmp${PWD}/$6.png" + fi + declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" + [x]="$2" [y]="$3" [width]="$4" [height]="$5" \ + [path]="/tmp${PWD}/$6.png") \ + > "$FIFO_UEBERZUG" +} + +function pdf_preview() { + # if no preview found, generate one + if [ ! -f "/tmp${PWD}/$6.png" ]; then + pdftoppm -png -singlefile "$6" "/tmp${PWD}/$6" + fi + declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" + [x]="$2" [y]="$3" [width]="$4" [height]="$5" \ + [path]="/tmp${PWD}/$6.png") \ + > "$FIFO_UEBERZUG" +} + +function audio_preview() { + # if no preview found, generate one + if [ ! -f "/tmp${PWD}/$6.png" ]; then + ffmpeg -i "$6" "/tmp${PWD}/$6.png" -y &> /dev/null + fi + declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" + [x]="$2" [y]="$3" [width]="$4" [height]="$5" \ + [path]="/tmp${PWD}/$6.png") \ + > "$FIFO_UEBERZUG" +} + +function video_preview() { + # if no preview found, generate one + if [ ! -f "/tmp${PWD}/$6.png" ]; then + ffmpegthumbnailer -i "${PWD}/$6" -o "/tmp${PWD}/$6.png" -s 0 -q 10 + fi + declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" + [x]="$2" [y]="$3" [width]="$4" [height]="$5" \ + [path]="/tmp${PWD}/$6.png") \ + > "$FIFO_UEBERZUG" +} + +function epub_preview() { + if [ ! -f "/tmp$PWD/$6.png" ]; then + epub-thumbnailer "$6" "/tmp$PWD/$6.png" 1024 + fi + declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" + [x]="$2" [y]="$3" [width]="$4" [height]="$5" \ + [path]="/tmp$PWD/$6.png") \ + > "$FIFO_UEBERZUG" +} + +# clear preview image +function clear_preview() { + declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \ + > "$FIFO_UEBERZUG" +} + +# Check all the dependencies that are needed to show all the file previews. +# The reason why we are sending a notification incase a dependency does not +# exist is because that is the easiest way to get the user's attention. +dependencies=(ffmpeg ffmpegthumbnailer fontpreview pdftoppm epub-thumbnailer) +for dependency in "${dependencies[@]}"; do + type -p "$dependency" &>/dev/null || { + notify-send "vifm" "Could not find '${dependency}', is it installed?" + exit 1 + } +done + + +# Make sure ueberzug's fifo exists +if [ -e "$FIFO_UEBERZUG" ]; then + # Make a temp dir for rendering if not exists + [ ! -d "/tmp${PWD}/" ] && mkdir -p "/tmp${PWD}/" + case "$1" in + draw) draw "$@" ;; + font) font_preview "$@" ;; + pdf) pdf_preview "$@" ;; + video) video_preview "$@" ;; + clear) clear_preview ;; + audio) audio_preview "$@" ;; + epub) epub_preview "$@" ;; + esac +fi |