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.

167 lines
5.1 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. # by cirala, checkout github.com/cirala/vifmimg
  13. PDF_PAGE_CONFIG="$HOME/.config/vifm/vifmimgpdfpage"
  14. PDF_FILE_CONFIG="$HOME/.config/vifm/vifmimgpdffile"
  15. PDF_PAGE=1
  16. PDF_FILE=""
  17. # Initialize the variables and required files
  18. [[ -f "$PDF_PAGE_CONFIG" ]] && PDF_PAGE=$(cat $PDF_PAGE_CONFIG) || touch $PDF_PAGE_CONFIG
  19. [[ -f "$PDF_FILE_CONFIG" ]] && PDF_FILE=$(cat $PDF_FILE_CONFIG) || touch $PDF_FILE_CONFIG
  20. # Create temporary working directory if the directory structure doesn't exist
  21. if [[ ! -d "/tmp$PWD/" ]]; then
  22. mkdir -p "/tmp$PWD/"
  23. fi
  24. function inc() {
  25. VAL="$(cat $PDF_PAGE_CONFIG)"
  26. echo "$(expr $VAL + 1)" > $PDF_PAGE_CONFIG
  27. }
  28. function dec() {
  29. VAL="$(cat $PDF_PAGE_CONFIG)"
  30. echo "$(expr $VAL - 1)" > $PDF_PAGE_CONFIG
  31. if [[ $VAL -le 0 ]]; then
  32. echo 0 > $PDF_PAGE_CONFIG
  33. fi
  34. }
  35. function previewclear() {
  36. declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
  37. > "$FIFO_UEBERZUG"
  38. }
  39. function fileclean() {
  40. if [[ -f "/tmp$PWD/$6.png" ]]; then
  41. rm -f "/tmp$PWD/$6.png"
  42. elif [[ -d "/tmp$PWD/$6/" ]]; then
  43. rm -rf "/tmp$PWD/$6/"
  44. fi
  45. }
  46. function preview() {
  47. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  48. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  49. [path]="$PWD/$6") \
  50. > "$FIFO_UEBERZUG"
  51. }
  52. function previewvideo() {
  53. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  54. ffmpegthumbnailer -i "$PWD/$6" -o "/tmp$PWD/$6.png" -s 0 -q 10
  55. fi
  56. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  57. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  58. [path]="/tmp$PWD/$6.png") \
  59. > "$FIFO_UEBERZUG"
  60. }
  61. function previewepub() {
  62. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  63. epub-thumbnailer "$6" "/tmp$PWD/$6.png" 1024
  64. fi
  65. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  66. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  67. [path]="/tmp$PWD/$6.png") \
  68. > "$FIFO_UEBERZUG"
  69. }
  70. function previewaudio() {
  71. if [[ ! -f "/tmp${PWD}/$6.png" ]]; then
  72. ffmpeg -i "$6" "/tmp${PWD}/$6.png" -y &> /dev/null
  73. fi
  74. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  75. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  76. [path]="/tmp${PWD}/$6.png") \
  77. > "$FIFO_UEBERZUG"
  78. }
  79. function previewgif() {
  80. if [[ ! -d "/tmp$PWD/$6/" ]]; then
  81. mkdir -p "/tmp$PWD/$6/"
  82. convert -coalesce "$PWD/$6" "/tmp$PWD/$6/$6.png"
  83. fi
  84. if [[ ! -z "$PLAY_GIF" ]]; then
  85. for frame in $(ls -1 /tmp$PWD/$6/$6*.png | sort -V); do
  86. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  87. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  88. [path]="$frame") \
  89. > "$FIFO_UEBERZUG"
  90. # Sleep between frames to make the animation smooth.
  91. sleep .07
  92. done
  93. else
  94. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  95. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  96. [path]="/tmp$PWD/$6/$6-0.png") \
  97. > "$FIFO_UEBERZUG"
  98. fi
  99. }
  100. function previewpdf() {
  101. if [[ ! "$6" == "$PDF_FILE" ]]; then
  102. PDF_PAGE=1
  103. echo 1 > $PDF_PAGE_CONFIG
  104. rm -f "/tmp$PWD/$6.png"
  105. fi
  106. if [[ ! "$PDF_PAGE" == "1" ]] && [[ -f "/tmp$PWD/$6.png" ]]; then
  107. rm -f "/tmp$PWD/$6.png"
  108. fi
  109. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  110. pdftoppm -png -f $PDF_PAGE -singlefile "$6" "/tmp$PWD/$6"
  111. fi
  112. echo "$6" > $PDF_FILE_CONFIG
  113. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  114. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  115. [path]="/tmp$PWD/$6.png") \
  116. > "$FIFO_UEBERZUG"
  117. }
  118. function previewmagick() {
  119. if [[ ! -f "/tmp$PWD/$6.png" ]]; then
  120. convert -thumbnail $(identify -format "%wx%h" "$6") "$PWD/$6" "/tmp$PWD/$6.png"
  121. fi
  122. declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
  123. [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
  124. [path]="/tmp$PWD/$6.png") \
  125. > "$FIFO_UEBERZUG"
  126. }
  127. function main() {
  128. case "$1" in
  129. "inc") inc "$@" ;;
  130. "dec") dec "$@" ;;
  131. "clear") previewclear "$@" ;;
  132. "clean") fileclean "$@" ;;
  133. "draw") preview "$@" ;;
  134. "videopreview") previewvideo "$@" ;;
  135. "epubpreview") previewepub "$@" ;;
  136. "gifpreview") previewgif "$@" ;;
  137. "pdfpreview") previewpdf "$@" ;;
  138. "magickpreview") previewmagick "$@" ;;
  139. "audiopreview") previewaudio "$@" ;;
  140. "*") echo "Unknown command: '$@'" ;;
  141. esac
  142. }
  143. main "$@"