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.

179 lines
6.1 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. # pdftoppm -jpeg -singlefile "$path" "${cached//.jpg}" && exit 6 || exit 1;;
  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. esac
  74. }
  75. handle_image() {
  76. local mimetype="${1}"
  77. case "${mimetype}" in
  78. # SVG
  79. # image/svg+xml)
  80. # convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
  81. # exit 1;;
  82. # Image
  83. image/*)
  84. local orientation
  85. orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
  86. # If orientation data is present and the image actually
  87. # needs rotating ("1" means no rotation)...
  88. if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  89. # ...auto-rotate the image according to the EXIF data.
  90. convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  91. fi
  92. # `w3mimgdisplay` will be called for all images (unless overriden as above),
  93. # but might fail for unsupported types.
  94. exit 7;;
  95. # Video
  96. # video/*)
  97. # # Thumbnail
  98. # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
  99. # exit 1;;
  100. # PDF
  101. # application/pdf)
  102. # pdftoppm -f 1 -l 1 \
  103. # -scale-to-x 1920 \
  104. # -scale-to-y -1 \
  105. # -singlefile \
  106. # -jpeg -tiffcompression jpeg \
  107. # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
  108. # && exit 6 || exit 1;;
  109. esac
  110. }
  111. handle_mime() {
  112. local mimetype="${1}"
  113. case "${mimetype}" in
  114. # Text
  115. text/* | */xml)
  116. # Syntax highlight
  117. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  118. exit 2
  119. fi
  120. if [[ "$( tput colors )" -ge 256 ]]; then
  121. local pygmentize_format='terminal256'
  122. local highlight_format='xterm256'
  123. else
  124. local pygmentize_format='terminal'
  125. local highlight_format='ansi'
  126. fi
  127. highlight --replace-tabs="${HIGHLIGHT_TABWIDTH}" --out-format="${highlight_format}" \
  128. --style="${HIGHLIGHT_STYLE}" --force -- "${FILE_PATH}" && exit 5
  129. # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}" -- "${FILE_PATH}" && exit 5
  130. exit 2;;
  131. # Image
  132. image/*)
  133. # Preview as text conversion
  134. # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
  135. exiftool "${FILE_PATH}" && exit 5
  136. exit 1;;
  137. # Video and audio
  138. video/* | audio/*)
  139. mediainfo "${FILE_PATH}" && exit 5
  140. exiftool "${FILE_PATH}" && exit 5
  141. exit 1;;
  142. esac
  143. }
  144. handle_fallback() {
  145. echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
  146. exit 1
  147. }
  148. MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
  149. if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
  150. handle_image "${MIMETYPE}"
  151. fi
  152. handle_extension
  153. handle_mime "${MIMETYPE}"
  154. handle_fallback
  155. exit 1