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.

233 lines
8.6 KiB

6 years ago
6 years ago
  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}" - | fmt -w ${PV_WIDTH} && exit 5
  54. mutool draw -F txt -i -- "${FILE_PATH}" 1-10 | fmt -w ${PV_WIDTH} && exit 5
  55. exiftool "${FILE_PATH}" && exit 5
  56. exit 1;;
  57. # BitTorrent
  58. torrent)
  59. transmission-show -- "${FILE_PATH}" && exit 5
  60. exit 1;;
  61. # OpenDocument
  62. odt|ods|odp|sxw)
  63. # Preview as text conversion
  64. odt2txt "${FILE_PATH}" && exit 5
  65. exit 1;;
  66. # HTML
  67. # htm|html|xhtml)
  68. # # Preview as text conversion
  69. # w3m -dump "${FILE_PATH}" && exit 5
  70. # lynx -dump -- "${FILE_PATH}" && exit 5
  71. # elinks -dump "${FILE_PATH}" && exit 5
  72. # ;; # Continue with next handler on failure
  73. # htm|html|xhtml)
  74. # # Preview as text conversion
  75. # w3m -dump "${FILE_PATH}" && exit 5
  76. # lynx -dump -- "${FILE_PATH}" && exit 5
  77. # elinks -dump "${FILE_PATH}" && exit 5
  78. # ;; # Continue with next handler on failure
  79. # JSON
  80. json)
  81. jq --color-output '.' "${FILE_PATH}" && exit 5
  82. python -m json.tool -- "${FILE_PATH}" && exit 5
  83. # txt)
  84. # cat "${FILE_PATH}" && exit 5
  85. esac
  86. }
  87. handle_image() {
  88. local mimetype="${1}"
  89. case "${mimetype}" in
  90. # SVG
  91. # image/svg+xml)
  92. # convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
  93. # exit 1;;
  94. # Image
  95. image/*)
  96. local orientation
  97. orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
  98. # If orientation data is present and the image actually
  99. # needs rotating ("1" means no rotation)...
  100. if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  101. # ...auto-rotate the image according to the EXIF data.
  102. convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  103. fi
  104. # `w3mimgdisplay` will be called for all images (unless overriden as above),
  105. # but might fail for unsupported types.
  106. exit 7;;
  107. # Video
  108. # video/*)
  109. # # Thumbnail
  110. # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
  111. # exit 1;;
  112. # PDF
  113. # application/pdf)
  114. # pdftoppm -f 1 -l 1 \
  115. # -scale-to-x 1920 \
  116. # -scale-to-y -1 \
  117. # -singlefile \
  118. # -jpeg -tiffcompression jpeg \
  119. # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
  120. # && exit 6 || exit 1;;
  121. # Preview archives using the first image inside.
  122. # (Very useful for comic book collections for example.)
  123. # application/zip|application/x-rar|application/x-7z-compressed|\
  124. # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
  125. # local fn=""; local fe=""
  126. # local zip=""; local rar=""; local tar=""; local bsd=""
  127. # case "${mimetype}" in
  128. # application/zip) zip=1 ;;
  129. # application/x-rar) rar=1 ;;
  130. # application/x-7z-compressed) ;;
  131. # *) tar=1 ;;
  132. # esac
  133. # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
  134. # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
  135. # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
  136. # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
  137. #
  138. # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
  139. # [ print(l, end='') for l in sys.stdin if \
  140. # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
  141. # sort -V | head -n 1)
  142. # [ "$fn" = "" ] && return
  143. # [ "$bsd" ] && fn=$(printf '%b' "$fn")
  144. #
  145. # [ "$tar" ] && tar --extract --to-stdout \
  146. # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
  147. # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
  148. # [ "$bsd" ] && bsdtar --extract --to-stdout \
  149. # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
  150. # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
  151. # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
  152. # "${IMAGE_CACHE_PATH}" && exit 6
  153. # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
  154. # "${IMAGE_CACHE_PATH}" && exit 6
  155. # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
  156. # ;;
  157. esac
  158. }
  159. handle_mime() {
  160. local mimetype="${1}"
  161. case "${mimetype}" in
  162. # Text
  163. text/* | */xml)
  164. # Syntax highlight
  165. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  166. exit 2
  167. fi
  168. if [[ "$( tput colors )" -ge 256 ]]; then
  169. local pygmentize_format='terminal256'
  170. local highlight_format='xterm256'
  171. else
  172. local pygmentize_format='terminal'
  173. local highlight_format='ansi'
  174. fi
  175. # local pygmentize_format='terminal'
  176. # local highlight_format='ansi'
  177. highlight --replace-tabs="${HIGHLIGHT_TABWIDTH}" --out-format="${highlight_format}" \
  178. --style="${HIGHLIGHT_STYLE}" --force -- "${FILE_PATH}" && exit 5
  179. # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}" -- "${FILE_PATH}" && exit 5
  180. exit 2;;
  181. # Image
  182. image/*)
  183. # Preview as text conversion
  184. # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
  185. exiftool "${FILE_PATH}" && exit 5
  186. exit 1;;
  187. # Video and audio
  188. video/* | audio/*)
  189. mediainfo "${FILE_PATH}" && exit 5
  190. exiftool "${FILE_PATH}" && exit 5
  191. exit 1;;
  192. esac
  193. }
  194. handle_fallback() {
  195. echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
  196. exit 1
  197. }
  198. MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
  199. if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
  200. handle_image "${MIMETYPE}"
  201. fi
  202. handle_extension
  203. handle_mime "${MIMETYPE}"
  204. handle_fallback
  205. exit 1