scope.sh 8.2 KB

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