scope.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. ## Because of some automated testing we do on the script #'s for comments need
  11. ## to be doubled up. Code that is commented out, because it's an alternative for
  12. ## example, gets only one #.
  13. ## Meanings of exit codes:
  14. ## code | meaning | action of ranger
  15. ## -----+------------+-------------------------------------------
  16. ## 0 | success | Display stdout as preview
  17. ## 1 | no preview | Display no preview at all
  18. ## 2 | plain text | Display the plain content of the file
  19. ## 3 | fix width | Don't reload when width changes
  20. ## 4 | fix height | Don't reload when height changes
  21. ## 5 | fix both | Don't ever reload
  22. ## 6 | image | Display the image `$IMAGE_CACHE_PATH` points to as an image preview
  23. ## 7 | image | Display the file directly as an image
  24. ## Script arguments
  25. FILE_PATH="${1}" # Full path of the highlighted file
  26. PV_WIDTH="${2}" # Width of the preview pane (number of fitting characters)
  27. ## shellcheck disable=SC2034 # PV_HEIGHT is provided for convenience and unused
  28. PV_HEIGHT="${3}" # Height of the preview pane (number of fitting characters)
  29. IMAGE_CACHE_PATH="${4}" # Full path that should be used to cache image preview
  30. PV_IMAGE_ENABLED="${5}" # 'True' if image previews are enabled, 'False' otherwise.
  31. FILE_EXTENSION="${FILE_PATH##*.}"
  32. FILE_EXTENSION_LOWER="$(printf "%s" "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')"
  33. ## Settings
  34. HIGHLIGHT_SIZE_MAX=262143 # 256KiB
  35. HIGHLIGHT_TABWIDTH=${HIGHLIGHT_TABWIDTH:-8}
  36. HIGHLIGHT_STYLE=${HIGHLIGHT_STYLE:-pablo}
  37. HIGHLIGHT_OPTIONS="--replace-tabs=${HIGHLIGHT_TABWIDTH} --style=${HIGHLIGHT_STYLE} ${HIGHLIGHT_OPTIONS:-}"
  38. PYGMENTIZE_STYLE=${PYGMENTIZE_STYLE:-autumn}
  39. OPENSCAD_IMGSIZE=${RNGR_OPENSCAD_IMGSIZE:-1000,1000}
  40. OPENSCAD_COLORSCHEME=${RNGR_OPENSCAD_COLORSCHEME:-Tomorrow Night}
  41. handle_extension() {
  42. case "${FILE_EXTENSION_LOWER}" in
  43. ## Archive
  44. a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
  45. rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  46. atool --list -- "${FILE_PATH}" && exit 5
  47. bsdtar --list --file "${FILE_PATH}" && exit 5
  48. exit 1;;
  49. rar)
  50. ## Avoid password prompt by providing empty password
  51. unrar lt -p- -- "${FILE_PATH}" && exit 5
  52. exit 1;;
  53. 7z)
  54. ## Avoid password prompt by providing empty password
  55. 7z l -p -- "${FILE_PATH}" && exit 5
  56. exit 1;;
  57. ## PDF
  58. pdf)
  59. ## Preview as text conversion
  60. pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | \
  61. fmt -w "${PV_WIDTH}" && exit 5
  62. mutool draw -F txt -i -- "${FILE_PATH}" 1-10 | \
  63. fmt -w "${PV_WIDTH}" && exit 5
  64. exiftool "${FILE_PATH}" && exit 5
  65. exit 1;;
  66. ## BitTorrent
  67. torrent)
  68. transmission-show -- "${FILE_PATH}" && exit 5
  69. exit 1;;
  70. ## OpenDocument
  71. odt|ods|odp|sxw)
  72. ## Preview as text conversion
  73. odt2txt "${FILE_PATH}" && exit 5
  74. ## Preview as markdown conversion
  75. pandoc -s -t markdown -- "${FILE_PATH}" && exit 5
  76. exit 1;;
  77. ## XLSX
  78. xlsx)
  79. ## Preview as csv conversion
  80. xlsx2csv -- "${FILE_PATH}" && exit 5
  81. exit 1;;
  82. ## HTML
  83. htm|html|xhtml)
  84. ## Preview as text conversion
  85. w3m -dump "${FILE_PATH}" && exit 5
  86. lynx -dump -- "${FILE_PATH}" && exit 5
  87. elinks -dump "${FILE_PATH}" && exit 5
  88. pandoc -s -t markdown -- "${FILE_PATH}" && exit 5
  89. ;;
  90. ## JSON
  91. json)
  92. jq --color-output . "${FILE_PATH}" && exit 5
  93. python -m json.tool -- "${FILE_PATH}" && exit 5
  94. ;;
  95. ## Direct Stream Digital/Transfer (DSDIFF) and wavpack aren't detected
  96. ## by file(1).
  97. dff|dsf|wv|wvc)
  98. mediainfo "${FILE_PATH}" && exit 5
  99. exiftool "${FILE_PATH}" && exit 5
  100. ;; # Continue with next handler on failure
  101. esac
  102. }
  103. handle_image() {
  104. ## Size of the preview if there are multiple options or it has to be
  105. ## rendered from vector graphics. If the conversion program allows
  106. ## specifying only one dimension while keeping the aspect ratio, the width
  107. ## will be used.
  108. local DEFAULT_SIZE="1920x1080"
  109. local mimetype="${1}"
  110. case "${mimetype}" in
  111. ## SVG
  112. # image/svg+xml|image/svg)
  113. # convert -- "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
  114. # exit 1;;
  115. ## DjVu
  116. # image/vnd.djvu)
  117. # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
  118. # - "${IMAGE_CACHE_PATH}" < "${FILE_PATH}" \
  119. # && exit 6 || exit 1;;
  120. ## Image
  121. image/*)
  122. local orientation
  123. orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
  124. ## If orientation data is present and the image actually
  125. ## needs rotating ("1" means no rotation)...
  126. if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  127. ## ...auto-rotate the image according to the EXIF data.
  128. convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  129. fi
  130. ## `w3mimgdisplay` will be called for all images (unless overriden
  131. ## as above), but might fail for unsupported types.
  132. exit 7;;
  133. ## Video
  134. # video/*)
  135. # # Thumbnail
  136. # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
  137. # exit 1;;
  138. ## PDF
  139. # application/pdf)
  140. # pdftoppm -f 1 -l 1 \
  141. # -scale-to-x "${DEFAULT_SIZE%x*}" \
  142. # -scale-to-y -1 \
  143. # -singlefile \
  144. # -jpeg -tiffcompression jpeg \
  145. # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
  146. # && exit 6 || exit 1;;
  147. ## ePub, MOBI, FB2 (using Calibre)
  148. # application/epub+zip|application/x-mobipocket-ebook|\
  149. # application/x-fictionbook+xml)
  150. # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
  151. # epub-thumbnailer "${FILE_PATH}" "${IMAGE_CACHE_PATH}" \
  152. # "${DEFAULT_SIZE%x*}" && exit 6
  153. # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FILE_PATH}" \
  154. # >/dev/null && exit 6
  155. # exit 1;;
  156. ## Font
  157. application/font*|application/*opentype)
  158. preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
  159. if fontimage -o "${preview_png}" \
  160. --pixelsize "120" \
  161. --fontname \
  162. --pixelsize "80" \
  163. --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
  164. --text " abcdefghijklmnopqrstuvwxyz " \
  165. --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
  166. --text " The quick brown fox jumps over the lazy dog. " \
  167. "${FILE_PATH}";
  168. then
  169. convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
  170. && rm "${preview_png}" \
  171. && exit 6
  172. else
  173. exit 1
  174. fi
  175. ;;
  176. ## Preview archives using the first image inside.
  177. ## (Very useful for comic book collections for example.)
  178. # application/zip|application/x-rar|application/x-7z-compressed|\
  179. # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
  180. # local fn=""; local fe=""
  181. # local zip=""; local rar=""; local tar=""; local bsd=""
  182. # case "${mimetype}" in
  183. # application/zip) zip=1 ;;
  184. # application/x-rar) rar=1 ;;
  185. # application/x-7z-compressed) ;;
  186. # *) tar=1 ;;
  187. # esac
  188. # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
  189. # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
  190. # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
  191. # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
  192. #
  193. # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
  194. # [ print(l, end='') for l in sys.stdin if \
  195. # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
  196. # sort -V | head -n 1)
  197. # [ "$fn" = "" ] && return
  198. # [ "$bsd" ] && fn=$(printf '%b' "$fn")
  199. #
  200. # [ "$tar" ] && tar --extract --to-stdout \
  201. # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
  202. # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
  203. # [ "$bsd" ] && bsdtar --extract --to-stdout \
  204. # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
  205. # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
  206. # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
  207. # "${IMAGE_CACHE_PATH}" && exit 6
  208. # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
  209. # "${IMAGE_CACHE_PATH}" && exit 6
  210. # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
  211. # ;;
  212. esac
  213. # openscad_image() {
  214. # TMPPNG="$(mktemp -t XXXXXX.png)"
  215. # openscad --colorscheme="${OPENSCAD_COLORSCHEME}" \
  216. # --imgsize="${OPENSCAD_IMGSIZE/x/,}" \
  217. # -o "${TMPPNG}" "${1}"
  218. # mv "${TMPPNG}" "${IMAGE_CACHE_PATH}"
  219. # }
  220. # case "${FILE_EXTENSION_LOWER}" in
  221. # ## 3D models
  222. # ## OpenSCAD only supports png image output, and ${IMAGE_CACHE_PATH}
  223. # ## is hardcoded as jpeg. So we make a tempfile.png and just
  224. # ## move/rename it to jpg. This works because image libraries are
  225. # ## smart enough to handle it.
  226. # csg|scad)
  227. # openscad_image "${FILE_PATH}" && exit 6
  228. # ;;
  229. # 3mf|amf|dxf|off|stl)
  230. # openscad_image <(echo "import(\"${FILE_PATH}\");") && exit 6
  231. # ;;
  232. # esac
  233. }
  234. handle_mime() {
  235. local mimetype="${1}"
  236. case "${mimetype}" in
  237. ## RTF and DOC
  238. text/rtf|*msword)
  239. ## Preview as text conversion
  240. ## note: catdoc does not always work for .doc files
  241. ## catdoc: http://www.wagner.pp.ru/~vitus/software/catdoc/
  242. catdoc -- "${FILE_PATH}" && exit 5
  243. exit 1;;
  244. ## DOCX, ePub, FB2 (using markdown)
  245. ## You might want to remove "|epub" and/or "|fb2" below if you have
  246. ## uncommented other methods to preview those formats
  247. *wordprocessingml.document|*/epub+zip|*/x-fictionbook+xml)
  248. ## Preview as markdown conversion
  249. pandoc -s -t markdown -- "${FILE_PATH}" && exit 5
  250. exit 1;;
  251. ## XLS
  252. *ms-excel)
  253. ## Preview as csv conversion
  254. ## xls2csv comes with catdoc:
  255. ## http://www.wagner.pp.ru/~vitus/software/catdoc/
  256. xls2csv -- "${FILE_PATH}" && exit 5
  257. exit 1;;
  258. ## Text
  259. text/* | */xml)
  260. ## Syntax highlight
  261. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  262. exit 2
  263. fi
  264. if [[ "$( tput colors )" -ge 256 ]]; then
  265. local pygmentize_format='terminal256'
  266. local highlight_format='xterm256'
  267. else
  268. local pygmentize_format='terminal'
  269. local highlight_format='ansi'
  270. fi
  271. env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
  272. --out-format="${highlight_format}" \
  273. --force -- "${FILE_PATH}" && exit 5
  274. env COLORTERM=8bit bat --color=always --style="plain" \
  275. -- "${FILE_PATH}" && exit 5
  276. pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
  277. -- "${FILE_PATH}" && exit 5
  278. exit 2;;
  279. ## DjVu
  280. image/vnd.djvu)
  281. ## Preview as text conversion (requires djvulibre)
  282. djvutxt "${FILE_PATH}" | fmt -w "${PV_WIDTH}" && exit 5
  283. exiftool "${FILE_PATH}" && exit 5
  284. exit 1;;
  285. ## Image
  286. image/*)
  287. ## Preview as text conversion
  288. # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
  289. exiftool "${FILE_PATH}" && exit 5
  290. exit 1;;
  291. ## Video and audio
  292. video/* | audio/*)
  293. mediainfo "${FILE_PATH}" && exit 5
  294. exiftool "${FILE_PATH}" && exit 5
  295. exit 1;;
  296. esac
  297. }
  298. handle_fallback() {
  299. echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
  300. exit 1
  301. }
  302. MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
  303. if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
  304. handle_image "${MIMETYPE}"
  305. fi
  306. handle_extension
  307. handle_mime "${MIMETYPE}"
  308. handle_fallback
  309. exit 1