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.

scope.sh 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env bash
  2. set -o noclobber -o noglob -o nounset -o pipefail
  3. IFS=$'\n'
  4. # code | meaning | action of ranger
  5. # -----+------------+-------------------------------------------
  6. # 0 | success | Display stdout as preview
  7. # 1 | no preview | Display no preview at all
  8. # 2 | plain text | Display the plain content of the file
  9. # 3 | fix width | Don't reload when width changes
  10. # 4 | fix height | Don't reload when height changes
  11. # 5 | fix both | Don't ever reload
  12. # 6 | image | Display the image `$IMAGE_CACHE_PATH` points to as an image preview
  13. # 7 | image | Display the file directly as an image
  14. # Script arguments
  15. FILE_PATH="${1}" # Full path of the highlighted file
  16. PV_WIDTH="${2}" # Width of the preview pane (number of fitting characters)
  17. PV_HEIGHT="${3}" # Height of the preview pane (number of fitting characters)
  18. IMAGE_CACHE_PATH="${4}" # Full path that should be used to cache image preview
  19. PV_IMAGE_ENABLED="${5}" # 'True' if image previews are enabled, 'False' otherwise.
  20. FILE_EXTENSION="${FILE_PATH##*.}"
  21. FILE_EXTENSION_LOWER=$(echo "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')
  22. # Settings
  23. PYGMENTIZE_STYLE='autumn'
  24. handle_extension() {
  25. case "${FILE_EXTENSION_LOWER}" in
  26. # Preview archive files by showing their first containing image.
  27. a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
  28. rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  29. atool --list -- "${FILE_PATH}" && exit 5
  30. bsdtar --list --file "${FILE_PATH}" && exit 5
  31. exit 1;;
  32. rar)
  33. # Avoid password prompt by providing empty password
  34. unrar lt -p- -- "${FILE_PATH}" && exit 5
  35. exit 1;;
  36. 7z)
  37. # Avoid password prompt by providing empty password
  38. 7z l -p -- "${FILE_PATH}" && exit 5
  39. exit 1;;
  40. # PDF
  41. pdf)
  42. # Preview as text conversion
  43. pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | fmt -w "${PV_WIDTH}" && exit 5
  44. mutool draw -F txt -i -- "${FILE_PATH}" 1-10 | fmt -w "${PV_WIDTH}" && exit 5
  45. exiftool "${FILE_PATH}" && exit 5
  46. exit 1;;
  47. # BitTorrent
  48. torrent)
  49. transmission-show -- "${FILE_PATH}" && exit 5
  50. exit 1;;
  51. # OpenDocument
  52. odt|ods|odp|sxw)
  53. # Preview as text conversion
  54. odt2txt "${FILE_PATH}" && exit 5
  55. exit 1;;
  56. # HTML
  57. htm|html|xhtml)
  58. # Preview as text conversion
  59. w3m -dump "${FILE_PATH}" && exit 5
  60. lynx -dump -- "${FILE_PATH}" && exit 5
  61. elinks -dump "${FILE_PATH}" && exit 5
  62. ;; # Continue with next handler on failure
  63. esac
  64. }
  65. handle_image() {
  66. local mimetype="${1}"
  67. case "${mimetype}" in
  68. # SVG
  69. # image/svg+xml)
  70. # convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
  71. # exit 1;;
  72. # Image
  73. image/*)
  74. local orientation
  75. orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
  76. # If orientation data is present and the image actually
  77. # needs rotating ("1" means no rotation)...
  78. if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  79. # ...auto-rotate the image according to the EXIF data.
  80. convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  81. fi
  82. # `w3mimgdisplay` will be called for all images (unless overriden as above),
  83. # but might fail for unsupported types.
  84. exit 7;;
  85. # Video
  86. video/*)
  87. # Thumbnail
  88. ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
  89. exit 1;;
  90. # PDF
  91. # application/pdf)
  92. # pdftoppm -f 1 -l 1 \
  93. # -scale-to-x 1920 \
  94. # -scale-to-y -1 \
  95. # -singlefile \
  96. # -jpeg -tiffcompression jpeg \
  97. # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
  98. # && exit 6 || exit 1;;
  99. # Preview archive files by showing their first containing image.
  100. # application/zip|application/x-rar|application/x-7z-compressed|\
  101. # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
  102. # local fn=""; local fe=""
  103. # local zip=""; local rar=""; local tar=""; local bsd=""
  104. # case "${mimetype}" in
  105. # application/zip) zip=1 ;;
  106. # application/x-rar) rar=1 ;;
  107. # application/x-7z-compressed) ;;
  108. # *) tar=1 ;;
  109. # esac
  110. # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
  111. # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
  112. # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
  113. # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
  114. #
  115. # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
  116. # [ print(l, end='') for l in sys.stdin if \
  117. # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
  118. # sort -V | head -n 1)
  119. # [ "$fn" = "" ] && return
  120. # [ "$bsd" ] && fn=$(printf '%b' "$fn")
  121. #
  122. # [ "$tar" ] && tar --extract --to-stdout \
  123. # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
  124. # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
  125. # [ "$bsd" ] && bsdtar --extract --to-stdout \
  126. # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
  127. # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
  128. # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
  129. # "${IMAGE_CACHE_PATH}" && exit 6
  130. # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
  131. # "${IMAGE_CACHE_PATH}" && exit 6
  132. # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
  133. # ;;
  134. esac
  135. }
  136. handle_mime() {
  137. local mimetype="${1}"
  138. case "${mimetype}" in
  139. # Text
  140. text/* | */xml)
  141. # Syntax highlight
  142. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  143. exit 2
  144. fi
  145. if [[ "$( tput colors )" -ge 256 ]]; then
  146. local pygmentize_format='terminal256'
  147. else
  148. local pygmentize_format='terminal'
  149. fi
  150. pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}" -- "${FILE_PATH}" && exit 5
  151. exit 2;;
  152. # Image
  153. image/*)
  154. # Preview as text conversion
  155. # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
  156. exiftool "${FILE_PATH}" && exit 5
  157. exit 1;;
  158. # Video and audio
  159. video/* | audio/*)
  160. mediainfo "${FILE_PATH}" && exit 5
  161. exiftool "${FILE_PATH}" && exit 5
  162. exit 1;;
  163. esac
  164. }
  165. handle_fallback() {
  166. echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
  167. exit 1
  168. }
  169. MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
  170. if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
  171. handle_image "${MIMETYPE}"
  172. fi
  173. handle_extension
  174. handle_mime "${MIMETYPE}"
  175. handle_fallback
  176. exit 1