Lemur Pro nixos dotfiles
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.

350 lines
13 KiB

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