Dotfiles for different machines on different branches.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.0 KiB

  1. #!/usr/bin/env bash
  2. readonly ID_PREVIEW="preview"
  3. #PLAY_GIF="yes"
  4. # By enabling this option the GIF will be animated, by leaving it commented like it
  5. # is now will make the gif previews behave the same way as video previews.
  6. #AUTO_REMOVE="yes"
  7. # By enabling this option the script will remove the preview file after it is drawn
  8. # and by doing so the preview will always be up-to-date with the file.
  9. # This however, requires more CPU and therefore affects the overall performance.
  10. # The messy code below is for moving pages in pdf files in the vifm file preview by
  11. # utilizing the < and > keys which will be bound to `vifmimg inc` or `vifmimg dec`.
  12. PDF_PAGE_CONFIG="$HOME/.config/vifm/vifmimgpdfpage"
  13. PDF_FILE_CONFIG="$HOME/.config/vifm/vifmimgpdffile"
  14. PDF_PAGE=1
  15. PDF_FILE=""
  16. # Initialize the variables and required files
  17. [[ -f "$PDF_PAGE_CONFIG" ]] && PDF_PAGE=$(cat $PDF_PAGE_CONFIG) || touch $PDF_PAGE_CONFIG
  18. [[ -f "$PDF_FILE_CONFIG" ]] && PDF_FILE=$(cat $PDF_FILE_CONFIG) || touch $PDF_FILE_CONFIG
  19. # Create temporary working directory if the directory structure doesn't exist
  20. if [[ ! -d "/tmp$PWD/" ]]; then
  21. mkdir -p "/tmp$PWD/"
  22. fi
  23. function inc() {
  24. VAL="$(cat $PDF_PAGE_CONFIG)"
  25. echo "$(expr $VAL + 1)" > $PDF_PAGE_CONFIG
  26. }
  27. function dec() {
  28. VAL="$(cat $PDF_PAGE_CONFIG)"
  29. echo "$(expr $VAL - 1)" > $PDF_PAGE_CONFIG
  30. if [[ $VAL -le 0 ]]; then
  31. echo 0 > $PDF_PAGE_CONFIG
  32. fi
  33. }
  34. function previewclear() {
  35. declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
  36. > "$FIFO_UEBERZUG"
  37. }
  38. function fileclean() {
  39. if [[ -f "/tmp$PWD/$6.png" ]]; then
  40. rm -f "/tmp$PWD/$6.png"
  41. elif [[ -d "/tmp$PWD/$6/" ]]; then
  42. rm -rf "/tmp$PWD/$6/"
  43. fi
  44. }
  45. function preview() {
  46. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  47. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  48. [path]="$PWD/$6") \
  49. > "$FIFO_UEBERZUG"
  50. }
  51. function previewvideo() {
  52. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  53. ffmpegthumbnailer -i "$PWD/$6" -o "/tmp$PWD/$6.png" -s 0 -q 10
  54. fi
  55. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  56. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  57. [path]="/tmp$PWD/$6.png") \
  58. > "$FIFO_UEBERZUG"
  59. }
  60. function previewepub() {
  61. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  62. epub-thumbnailer "$6" "/tmp$PWD/$6.png" 1024
  63. fi
  64. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  65. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  66. [path]="/tmp$PWD/$6.png") \
  67. > "$FIFO_UEBERZUG"
  68. }
  69. function previewaudio() {
  70. if [[ ! -f "/tmp${PWD}/$6.png" ]]; then
  71. ffmpeg -i "$6" "/tmp${PWD}/$6.png" -y &> /dev/null
  72. fi
  73. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  74. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  75. [path]="/tmp${PWD}/$6.png") \
  76. > "$FIFO_UEBERZUG"
  77. }
  78. function previewgif() {
  79. if [[ ! -d "/tmp$PWD/$6/" ]]; then
  80. mkdir -p "/tmp$PWD/$6/"
  81. convert -coalesce "$PWD/$6" "/tmp$PWD/$6/$6.png"
  82. fi
  83. if [[ ! -z "$PLAY_GIF" ]]; then
  84. for frame in $(ls -1 /tmp$PWD/$6/$6*.png | sort -V); do
  85. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  86. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  87. [path]="$frame") \
  88. > "$FIFO_UEBERZUG"
  89. # Sleep between frames to make the animation smooth.
  90. sleep .07
  91. done
  92. else
  93. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  94. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  95. [path]="/tmp$PWD/$6/$6-0.png") \
  96. > "$FIFO_UEBERZUG"
  97. fi
  98. }
  99. function previewpdf() {
  100. if [[ ! "$6" == "$PDF_FILE" ]]; then
  101. PDF_PAGE=1
  102. echo 1 > $PDF_PAGE_CONFIG
  103. rm -f "/tmp$PWD/$6.png"
  104. fi
  105. if [[ ! "$PDF_PAGE" == "1" ]] && [[ -f "/tmp$PWD/$6.png" ]]; then
  106. rm -f "/tmp$PWD/$6.png"
  107. fi
  108. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  109. pdftoppm -png -f $PDF_PAGE -singlefile "$6" "/tmp$PWD/$6"
  110. fi
  111. echo "$6" > $PDF_FILE_CONFIG
  112. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  113. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  114. [path]="/tmp$PWD/$6.png") \
  115. > "$FIFO_UEBERZUG"
  116. }
  117. function previewmagick() {
  118. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  119. convert -thumbnail $(identify -format "%wx%h" "$6") "$PWD/$6" "/tmp$PWD/$6.png"
  120. fi
  121. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  122. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  123. [path]="/tmp$PWD/$6.png") \
  124. > "$FIFO_UEBERZUG"
  125. }
  126. function main() {
  127. case "$1" in
  128. "inc") inc "$@" ;;
  129. "dec") dec "$@" ;;
  130. "clear") previewclear "$@" ;;
  131. "clean") fileclean "$@" ;;
  132. "draw") preview "$@" ;;
  133. "videopreview") previewvideo "$@" ;;
  134. "epubpreview") previewepub "$@" ;;
  135. "gifpreview") previewgif "$@" ;;
  136. "pdfpreview") previewpdf "$@" ;;
  137. "magickpreview") previewmagick "$@" ;;
  138. "audiopreview") previewaudio "$@" ;;
  139. "*") echo "Unknown command: '$@'" ;;
  140. esac
  141. }
  142. main "$@"