scope.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. ## Uses: https://github.com/dilshod/xlsx2csv
  81. xlsx2csv -- "${FILE_PATH}" && exit 5
  82. exit 1;;
  83. ## HTML
  84. htm|html|xhtml)
  85. ## Preview as text conversion
  86. w3m -dump "${FILE_PATH}" && exit 5
  87. lynx -dump -- "${FILE_PATH}" && exit 5
  88. elinks -dump "${FILE_PATH}" && exit 5
  89. pandoc -s -t markdown -- "${FILE_PATH}" && exit 5
  90. ;;
  91. ## JSON
  92. json)
  93. jq --color-output . "${FILE_PATH}" && exit 5
  94. python -m json.tool -- "${FILE_PATH}" && exit 5
  95. ;;
  96. ## Direct Stream Digital/Transfer (DSDIFF) and wavpack aren't detected
  97. ## by file(1).
  98. dff|dsf|wv|wvc)
  99. mediainfo "${FILE_PATH}" && exit 5
  100. exiftool "${FILE_PATH}" && exit 5
  101. ;; # Continue with next handler on failure
  102. lua)
  103. ## Syntax highlight
  104. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  105. exit 2
  106. fi
  107. if [[ "$( tput colors )" -ge 256 ]]; then
  108. local pygmentize_format='terminal256'
  109. local highlight_format='xterm256'
  110. else
  111. local pygmentize_format='terminal'
  112. local highlight_format='ansi'
  113. fi
  114. env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
  115. --out-format="${highlight_format}" \
  116. --force -- "${FILE_PATH}" && exit 5
  117. env COLORTERM=8bit bat --color=always --style="plain" \
  118. -- "${FILE_PATH}" && exit 5
  119. pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
  120. -- "${FILE_PATH}" && exit 5
  121. exit 2;;
  122. esac
  123. }
  124. handle_image() {
  125. ## Size of the preview if there are multiple options or it has to be
  126. ## rendered from vector graphics. If the conversion program allows
  127. ## specifying only one dimension while keeping the aspect ratio, the width
  128. ## will be used.
  129. local DEFAULT_SIZE="1920x1080"
  130. local mimetype="${1}"
  131. case "${mimetype}" in
  132. ## SVG
  133. # image/svg+xml|image/svg)
  134. # convert -- "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
  135. # exit 1;;
  136. ## DjVu
  137. # image/vnd.djvu)
  138. # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
  139. # - "${IMAGE_CACHE_PATH}" < "${FILE_PATH}" \
  140. # && exit 6 || exit 1;;
  141. ## Image
  142. image/*)
  143. local orientation
  144. orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
  145. ## If orientation data is present and the image actually
  146. ## needs rotating ("1" means no rotation)...
  147. if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  148. ## ...auto-rotate the image according to the EXIF data.
  149. convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  150. fi
  151. ## `w3mimgdisplay` will be called for all images (unless overriden
  152. ## as above), but might fail for unsupported types.
  153. exit 7;;
  154. ## Video
  155. # video/*)
  156. # # Thumbnail
  157. # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
  158. # exit 1;;
  159. ## PDF
  160. # application/pdf)
  161. # pdftoppm -f 1 -l 1 \
  162. # -scale-to-x "${DEFAULT_SIZE%x*}" \
  163. # -scale-to-y -1 \
  164. # -singlefile \
  165. # -jpeg -tiffcompression jpeg \
  166. # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
  167. # && exit 6 || exit 1;;
  168. ## ePub, MOBI, FB2 (using Calibre)
  169. # application/epub+zip|application/x-mobipocket-ebook|\
  170. # application/x-fictionbook+xml)
  171. # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
  172. # epub-thumbnailer "${FILE_PATH}" "${IMAGE_CACHE_PATH}" \
  173. # "${DEFAULT_SIZE%x*}" && exit 6
  174. # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FILE_PATH}" \
  175. # >/dev/null && exit 6
  176. # exit 1;;
  177. ## Font
  178. application/font*|application/*opentype)
  179. preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
  180. if fontimage -o "${preview_png}" \
  181. --pixelsize "120" \
  182. --fontname \
  183. --pixelsize "80" \
  184. --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
  185. --text " abcdefghijklmnopqrstuvwxyz " \
  186. --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
  187. --text " The quick brown fox jumps over the lazy dog. " \
  188. "${FILE_PATH}";
  189. then
  190. convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
  191. && rm "${preview_png}" \
  192. && exit 6
  193. else
  194. exit 1
  195. fi
  196. ;;
  197. ## Preview archives using the first image inside.
  198. ## (Very useful for comic book collections for example.)
  199. # application/zip|application/x-rar|application/x-7z-compressed|\
  200. # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
  201. # local fn=""; local fe=""
  202. # local zip=""; local rar=""; local tar=""; local bsd=""
  203. # case "${mimetype}" in
  204. # application/zip) zip=1 ;;
  205. # application/x-rar) rar=1 ;;
  206. # application/x-7z-compressed) ;;
  207. # *) tar=1 ;;
  208. # esac
  209. # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
  210. # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
  211. # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
  212. # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
  213. #
  214. # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
  215. # [ print(l, end='') for l in sys.stdin if \
  216. # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
  217. # sort -V | head -n 1)
  218. # [ "$fn" = "" ] && return
  219. # [ "$bsd" ] && fn=$(printf '%b' "$fn")
  220. #
  221. # [ "$tar" ] && tar --extract --to-stdout \
  222. # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
  223. # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
  224. # [ "$bsd" ] && bsdtar --extract --to-stdout \
  225. # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
  226. # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
  227. # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
  228. # "${IMAGE_CACHE_PATH}" && exit 6
  229. # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
  230. # "${IMAGE_CACHE_PATH}" && exit 6
  231. # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
  232. # ;;
  233. esac
  234. # openscad_image() {
  235. # TMPPNG="$(mktemp -t XXXXXX.png)"
  236. # openscad --colorscheme="${OPENSCAD_COLORSCHEME}" \
  237. # --imgsize="${OPENSCAD_IMGSIZE/x/,}" \
  238. # -o "${TMPPNG}" "${1}"
  239. # mv "${TMPPNG}" "${IMAGE_CACHE_PATH}"
  240. # }
  241. # case "${FILE_EXTENSION_LOWER}" in
  242. # ## 3D models
  243. # ## OpenSCAD only supports png image output, and ${IMAGE_CACHE_PATH}
  244. # ## is hardcoded as jpeg. So we make a tempfile.png and just
  245. # ## move/rename it to jpg. This works because image libraries are
  246. # ## smart enough to handle it.
  247. # csg|scad)
  248. # openscad_image "${FILE_PATH}" && exit 6
  249. # ;;
  250. # 3mf|amf|dxf|off|stl)
  251. # openscad_image <(echo "import(\"${FILE_PATH}\");") && exit 6
  252. # ;;
  253. # esac
  254. }
  255. handle_mime() {
  256. local mimetype="${1}"
  257. case "${mimetype}" in
  258. ## RTF and DOC
  259. text/rtf|*msword)
  260. ## Preview as text conversion
  261. ## note: catdoc does not always work for .doc files
  262. ## catdoc: http://www.wagner.pp.ru/~vitus/software/catdoc/
  263. catdoc -- "${FILE_PATH}" && exit 5
  264. exit 1;;
  265. ## DOCX, ePub, FB2 (using markdown)
  266. ## You might want to remove "|epub" and/or "|fb2" below if you have
  267. ## uncommented other methods to preview those formats
  268. *wordprocessingml.document|*/epub+zip|*/x-fictionbook+xml)
  269. ## Preview as markdown conversion
  270. pandoc -s -t markdown -- "${FILE_PATH}" && exit 5
  271. exit 1;;
  272. ## XLS
  273. *ms-excel)
  274. ## Preview as csv conversion
  275. ## xls2csv comes with catdoc:
  276. ## http://www.wagner.pp.ru/~vitus/software/catdoc/
  277. xls2csv -- "${FILE_PATH}" && exit 5
  278. exit 1;;
  279. ## Text
  280. text/* | */xml)
  281. ## Syntax highlight
  282. if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  283. exit 2
  284. fi
  285. if [[ "$( tput colors )" -ge 256 ]]; then
  286. local pygmentize_format='terminal256'
  287. local highlight_format='xterm256'
  288. else
  289. local pygmentize_format='terminal'
  290. local highlight_format='ansi'
  291. fi
  292. env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
  293. --out-format="${highlight_format}" \
  294. --force -- "${FILE_PATH}" && exit 5
  295. env COLORTERM=8bit bat --color=always --style="plain" \
  296. -- "${FILE_PATH}" && exit 5
  297. pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
  298. -- "${FILE_PATH}" && exit 5
  299. exit 2;;
  300. ## DjVu
  301. image/vnd.djvu)
  302. ## Preview as text conversion (requires djvulibre)
  303. djvutxt "${FILE_PATH}" | fmt -w "${PV_WIDTH}" && exit 5
  304. exiftool "${FILE_PATH}" && exit 5
  305. exit 1;;
  306. ## Image
  307. image/*)
  308. ## Preview as text conversion
  309. # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
  310. exiftool "${FILE_PATH}" && exit 5
  311. exit 1;;
  312. ## Video and audio
  313. video/* | audio/*)
  314. mediainfo "${FILE_PATH}" && exit 5
  315. exiftool "${FILE_PATH}" && exit 5
  316. exit 1;;
  317. esac
  318. }
  319. handle_fallback() {
  320. echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
  321. exit 1
  322. }
  323. MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
  324. if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
  325. handle_image "${MIMETYPE}"
  326. fi
  327. handle_extension
  328. handle_mime "${MIMETYPE}"
  329. handle_fallback
  330. exit 1