vifmimg.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. readonly ID_PREVIEW="preview"
  4. #AUTO_REMOVE="yes"
  5. # By enabling this option the script will remove the preview file after it is drawn
  6. # and by doing so the preview will always be up-to-date with the file.
  7. # This however, requires more CPU and therefore affects the overall performance.
  8. if [ -e "$FIFO_UEBERZUG" ]; then
  9. if [[ "$1" == "draw" ]]; then
  10. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  11. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  12. [path]="${PWD}/$6") \
  13. > "$FIFO_UEBERZUG"
  14. elif [[ "$1" == "videopreview" ]]; then
  15. echo -e "Loading preview..\nFile: $6"
  16. [[ ! -d "/tmp${PWD}/$6/" ]] && mkdir -p "/tmp${PWD}/$6/"
  17. [[ ! -f "/tmp${PWD}/$6.png" ]] && ffmpegthumbnailer -i "${PWD}/$6" -o "/tmp${PWD}/$6.png" -s 0 -q 10
  18. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  19. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  20. [path]="/tmp${PWD}/$6.png") \
  21. > "$FIFO_UEBERZUG"
  22. elif [[ "$1" == "gifpreview" ]]; then
  23. echo -e "Loading preview..\nFile: $6"
  24. [[ ! -d "/tmp${PWD}/$6/" ]] && mkdir -p "/tmp${PWD}/$6/" && convert -coalesce "${PWD}/$6" "/tmp${PWD}/$6/$6.png"
  25. for frame in $(ls -1 /tmp${PWD}/$6/$6*.png | sort -V); do
  26. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  27. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  28. [path]="$frame") \
  29. > "$FIFO_UEBERZUG"
  30. # Sleep between frames to make the animation smooth.
  31. sleep .07
  32. done
  33. elif [[ "$1" == "pdfpreview" ]]; then
  34. echo -e "Loading preview..\nFile: $6"
  35. [[ ! -d "/tmp${PWD}/$6/" ]] && mkdir -p "/tmp${PWD}/$6/"
  36. [[ ! -f "/tmp${PWD}/$6.png" ]] && pdftoppm -png -singlefile "$6" "/tmp${PWD}/$6"
  37. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  38. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  39. [path]="/tmp${PWD}/$6.png") \
  40. > "$FIFO_UEBERZUG"
  41. elif [[ "$1" == "clear" ]]; then
  42. declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
  43. > "$FIFO_UEBERZUG"
  44. [[ ! -z $AUTO_REMOVE ]] && [[ -f "/tmp${PWD}/$6.png" ]] && rm -f "/tmp${PWD}/$6.png"
  45. [[ ! -z $AUTO_REMOVE ]] && [[ -d "/tmp${PWD}/$6/" ]] && rm -rf "/tmp${PWD}/$6/"
  46. fi
  47. fi