Dotfiles for my tiling window manager + terminal workflow.
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.

178 lines
6.0 KiB

  1. #!/usr/bin/env bash
  2. set -o noclobber -o noglob -o nounset -o pipefail
  3. IFS=$'\n'
  4. # If the option `use_preview_script` is set to `true`,
  5. # then this script will be called and its output will be displayed in ranger.
  6. # ANSI color codes are supported.
  7. # STDIN is disabled, so interactive scripts won't work properly
  8. # This script is considered a configuration file and must be updated manually.
  9. # It will be left untouched if you upgrade ranger.
  10. # Meanings of exit codes:
  11. # code | meaning | action of ranger
  12. # -----+------------+-------------------------------------------
  13. # 0 | success | Display stdout as preview
  14. # 1 | no preview | Display no preview at all
  15. # 2 | plain text | Display the plain content of the file
  16. # 3 | fix width | Don't reload when width changes
  17. # 4 | fix height | Don't reload when height changes
  18. # 5 | fix both | Don't ever reload
  19. # 6 | image | Display the image `$IMAGE_CACHE_PATH` points to as an image preview
  20. # 7 | image | Display the file directly as an image
  21. # Script arguments
  22. FILE_PATH="${1}" # Full path of the highlighted file
  23. PV_WIDTH="${2}" # Width of the preview pane (number of fitting characters)
  24. PV_HEIGHT="${3}" # Height of the preview pane (number of fitting characters)
  25. IMAGE_CACHE_PATH="${4}" # Full path that should be used to cache image preview
  26. PV_IMAGE_ENABLED="${5}" # 'True' if image previews are enabled, 'False' otherwise.
  27. FILE_EXTENSION="${FILE_PATH##*.}"
  28. FILE_EXTENSION_LOWER=$(echo ${FILE_EXTENSION} | tr '[:upper:]' '[:lower:]')
  29. # Settings
  30. HIGHLIGHT_SIZE_MAX=262143 # 256KiB
  31. HIGHLIGHT_TABWIDTH=8
  32. HIGHLIGHT_STYLE='pablo'
  33. PYGMENTIZE_STYLE='autumn'
  34. handle_extension() {
  35. case "${FILE_EXTENSION_LOWER}" in
  36. # Archive
  37. a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
  38. rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  39. atool --list -- "${FILE_PATH}" && exit 5
  40. bsdtar --list --file "${FILE_PATH}" && exit 5
  41. exit 1;;
  42. rar)
  43. # Avoid password prompt by providing empty password
  44. unrar lt -p- -- "${FILE_PATH}" && exit 5
  45. exit 1;;
  46. 7z)
  47. # Avoid password prompt by providing empty password
  48. 7z l -p -- "${FILE_PATH}" && exit 5
  49. exit 1;;
  50. # PDF
  51. pdf)
  52. # Preview as text conversion
  53. pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - && exit 5
  54. exiftool "${FILE_PATH}" && exit 5
  55. exit 1;;
  56. # BitTorrent
  57. torrent)
  58. transmission-show -- "${FILE_PATH}" && exit 5
  59. exit 1;;
  60. # OpenDocument
  61. odt|ods|odp|sxw)
  62. # Preview as text conversion
  63. odt2txt "${FILE_PATH}" && exit 5
  64. exit 1;;
  65. # HTML
  66. htm|html|xhtml)
  67. # Preview as text conversion
  68. w3m -dump "${FILE_PATH}" && exit 5
  69. lynx -dump -- "${FILE_PATH}" && exit 5
  70. elinks -dump "${FILE_PATH}" && exit 5
  71. ;; # Continue with next handler on failure
  72. esac
  73. }
  74. handle_image() {
  75. local mimetype="${1}"
  76. case "${mimetype}" in
  77. # SVG
  78. # image/svg+xml)
  79. # convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
  80. # exit 1;;
  81. # Image
  82. image/*)
  83. local orientation
  84. orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
  85. # If orientation data is present and the image actually
  86. # needs rotating ("1" means no rotation)...
  87. if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  88. # ...auto-rotate the image according to the EXIF data.
  89. convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  90. fi
  91. # `w3mimgdisplay` will be called for all images (unless overriden as above),
  92. # but might fail for unsupported types.
  93. exit 7;;
  94. # Video
  95. # video/*)
  96. # # Thumbnail
  97. # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
  98. # exit 1;;
  99. # PDF
  100. # application/pdf)
  101. # pdftoppm -f 1 -l 1 \
  102. # -scale-to-x 1920 \
  103. # -scale-to-y -1 \
  104. # -singlefile \
  105. # -jpeg -tiffcompression jpeg \
  106. # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
  107. # && exit 6 || exit 1;;
  108. esac
  109. }
  110. handle_mime() {
  111. local mimetype="${1}"
  112. case "${mimetype}" in
  113. # Text
  114. text/* | */xml)
  115. # Syntax highlight
  116. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  117. exit 2
  118. fi
  119. if [[ "$( tput colors )" -ge 256 ]]; then
  120. local pygmentize_format='terminal256'
  121. local highlight_format='xterm256'
  122. else
  123. local pygmentize_format='terminal'
  124. local highlight_format='ansi'
  125. fi
  126. highlight --replace-tabs="${HIGHLIGHT_TABWIDTH}" --out-format="${highlight_format}" \
  127. --style="${HIGHLIGHT_STYLE}" --force -- "${FILE_PATH}" && exit 5
  128. # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}" -- "${FILE_PATH}" && exit 5
  129. exit 2;;
  130. # Image
  131. image/*)
  132. # Preview as text conversion
  133. # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
  134. exiftool "${FILE_PATH}" && exit 5
  135. exit 1;;
  136. # Video and audio
  137. video/* | audio/*)
  138. mediainfo "${FILE_PATH}" && exit 5
  139. exiftool "${FILE_PATH}" && exit 5
  140. exit 1;;
  141. esac
  142. }
  143. handle_fallback() {
  144. echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
  145. exit 1
  146. }
  147. MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
  148. if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
  149. handle_image "${MIMETYPE}"
  150. fi
  151. handle_extension
  152. handle_mime "${MIMETYPE}"
  153. handle_fallback
  154. exit 1