3 set -o noclobber
-o noglob
-o nounset
-o pipefail
6 ## If the option `use_preview_script` is set to `true`,
7 ## then this script will be called and its output will be displayed in ranger.
8 ## ANSI color codes are supported.
9 ## STDIN is disabled, so interactive scripts won't work properly
11 ## This script is considered a configuration file and must be updated manually.
12 ## It will be left untouched if you upgrade ranger.
14 ## Because of some automated testing we do on the script #'s for comments need
15 ## to be doubled up. Code that is commented out, because it's an alternative for
16 ## example, gets only one #.
18 ## Meanings of exit codes:
19 ## code | meaning | action of ranger
20 ## -----+------------+-------------------------------------------
21 ## 0 | success | Display stdout as preview
22 ## 1 | no preview | Display no preview at all
23 ## 2 | plain text | Display the plain content of the file
24 ## 3 | fix width | Don't reload when width changes
25 ## 4 | fix height | Don't reload when height changes
26 ## 5 | fix both | Don't ever reload
27 ## 6 | image | Display the image `$IMAGE_CACHE_PATH` points to as an image preview
28 ## 7 | image | Display the file directly as an image
31 FILE_PATH
="${1}" # Full path of the highlighted file
32 PV_WIDTH
="${2}" # Width of the preview pane (number of fitting characters)
33 ## shellcheck disable=SC2034 # PV_HEIGHT is provided for convenience and unused
34 PV_HEIGHT
="${3}" # Height of the preview pane (number of fitting characters)
35 IMAGE_CACHE_PATH
="${4}" # Full path that should be used to cache image preview
36 PV_IMAGE_ENABLED
="${5}" # 'True' if image previews are enabled, 'False' otherwise.
38 FILE_EXTENSION
="${FILE_PATH##*.}"
39 FILE_EXTENSION_LOWER
="$(printf "%s" "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')"
42 HIGHLIGHT_SIZE_MAX
=262143 # 256KiB
43 HIGHLIGHT_TABWIDTH
="${HIGHLIGHT_TABWIDTH:-8}"
44 HIGHLIGHT_STYLE
="${HIGHLIGHT_STYLE:-pablo}"
45 HIGHLIGHT_OPTIONS
="--replace-tabs=${HIGHLIGHT_TABWIDTH} --style=${HIGHLIGHT_STYLE} ${HIGHLIGHT_OPTIONS:-}"
46 PYGMENTIZE_STYLE
="${PYGMENTIZE_STYLE:-autumn}"
47 OPENSCAD_IMGSIZE
="${RNGR_OPENSCAD_IMGSIZE:-1000,1000}"
48 OPENSCAD_COLORSCHEME
="${RNGR_OPENSCAD_COLORSCHEME:-Tomorrow Night}"
51 case "${FILE_EXTENSION_LOWER}" in
53 a
|ace
|alz
|arc
|arj
|bz
|bz2
|cab
|cpio|deb
|gz
|jar
|lha
|lz
|lzh
|lzma
|lzo
|\
54 rpm
|rz
|t7z
|tar|tbz
|tbz2
|tgz
|tlz
|txz
|tZ
|tzo
|war
|xpi
|xz
|Z
|zip)
55 atool
--list -- "${FILE_PATH}" && exit 5
56 bsdtar
--list --file "${FILE_PATH}" && exit 5
59 ## Avoid password prompt by providing empty password
60 unrar lt
-p- -- "${FILE_PATH}" && exit 5
63 ## Avoid password prompt by providing empty password
64 7z l
-p -- "${FILE_PATH}" && exit 5
69 ## Preview as text conversion
70 pdftotext
-l 10 -nopgbrk -q -- "${FILE_PATH}" - | \
71 fmt -w "${PV_WIDTH}" && exit 5
72 mutool draw
-F txt
-i -- "${FILE_PATH}" 1-10 | \
73 fmt -w "${PV_WIDTH}" && exit 5
74 exiftool
"${FILE_PATH}" && exit 5
79 transmission
-show -- "${FILE_PATH}" && exit 5
84 ## Preview as text conversion
85 odt2txt
"${FILE_PATH}" && exit 5
86 ## Preview as markdown conversion
87 pandoc
-s -t markdown
-- "${FILE_PATH}" && exit 5
90 ## Preview as text conversion (unsupported by pandoc for markdown)
91 odt2txt
"${FILE_PATH}" && exit 5
96 ## Preview as csv conversion
97 ## Uses: https://github.com/dilshod/xlsx2csv
98 xlsx2csv
-- "${FILE_PATH}" && exit 5
103 ## Preview as text conversion
104 w3m
-dump "${FILE_PATH}" && exit 5
105 lynx
-dump -- "${FILE_PATH}" && exit 5
106 elinks
-dump "${FILE_PATH}" && exit 5
107 pandoc
-s -t markdown
-- "${FILE_PATH}" && exit 5
112 gpg
--decrypt "$path" | trim
&& exit 5
117 jq
--color-output .
"${FILE_PATH}" && exit 5
118 python
-m json.tool
-- "${FILE_PATH}" && exit 5
123 jupyter nbconvert
--to markdown
"${FILE_PATH}" --stdout | env COLORTERM
=8bit bat
--color=always
--style=plain
--language=markdown
&& exit 5
124 jupyter nbconvert
--to markdown
"${FILE_PATH}" --stdout && exit 5
125 jq
--color-output .
"${FILE_PATH}" && exit 5
126 python
-m json.tool
-- "${FILE_PATH}" && exit 5
129 ## Direct Stream Digital/Transfer (DSDIFF) and wavpack aren't detected
132 mediainfo
"${FILE_PATH}" && exit 5
133 exiftool
"${FILE_PATH}" && exit 5
134 ;; # Continue with next handler on failure
139 ## Size of the preview if there are multiple options or it has to be
140 ## rendered from vector graphics. If the conversion program allows
141 ## specifying only one dimension while keeping the aspect ratio, the width
143 local DEFAULT_SIZE
="1920x1080"
145 local mimetype
="${1}"
146 case "${mimetype}" in
148 image
/svg
+xml
|image
/svg
)
149 rsvg
-convert --keep-aspect-ratio --width "${DEFAULT_SIZE%x*}" "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}.png" \
150 && mv "${IMAGE_CACHE_PATH}.png" "${IMAGE_CACHE_PATH}" \
156 # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
157 # - "${IMAGE_CACHE_PATH}" < "${FILE_PATH}" \
158 # && exit 6 || exit 1;;
163 orientation
="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
164 ## If orientation data is present and the image actually
165 ## needs rotating ("1" means no rotation)...
166 if [[ -n "$orientation" && "$orientation" != 1 ]]; then
167 ## ...auto-rotate the image according to the EXIF data.
168 convert
-- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
171 ## `w3mimgdisplay` will be called for all images (unless overridden
172 ## as above), but might fail for unsupported types.
177 # # Get embedded thumbnail
178 # ffmpeg -i "${FILE_PATH}" -map 0:v -map -0:V -c copy "${IMAGE_CACHE_PATH}" && exit 6
179 # # Get frame 10% into video
180 # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
185 # # Get embedded thumbnail
186 # ffmpeg -i "${FILE_PATH}" -map 0:v -map -0:V -c copy \
187 # "${IMAGE_CACHE_PATH}" && exit 6;;
191 # pdftoppm -f 1 -l 1 \
192 # -scale-to-x "${DEFAULT_SIZE%x*}" \
195 # -jpeg -tiffcompression jpeg \
196 # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
197 # && exit 6 || exit 1;;
200 ## ePub, MOBI, FB2 (using Calibre)
201 # application/epub+zip|application/x-mobipocket-ebook|\
202 # application/x-fictionbook+xml)
203 # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
204 # epub-thumbnailer "${FILE_PATH}" "${IMAGE_CACHE_PATH}" \
205 # "${DEFAULT_SIZE%x*}" && exit 6
206 # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FILE_PATH}" \
207 # >/dev/null && exit 6
211 application
/font
*|application
/*opentype
)
212 preview_png
="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
213 if fontimage
-o "${preview_png}" \
217 --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
218 --text " abcdefghijklmnopqrstuvwxyz " \
219 --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
220 --text " The quick brown fox jumps over the lazy dog. " \
223 convert
-- "${preview_png}" "${IMAGE_CACHE_PATH}" \
224 && rm "${preview_png}" \
231 ## Preview archives using the first image inside.
232 ## (Very useful for comic book collections for example.)
233 # application/zip|application/x-rar|application/x-7z-compressed|\
234 # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
235 # local fn=""; local fe=""
236 # local zip=""; local rar=""; local tar=""; local bsd=""
237 # case "${mimetype}" in
238 # application/zip) zip=1 ;;
239 # application/x-rar) rar=1 ;;
240 # application/x-7z-compressed) ;;
243 # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
244 # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
245 # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
246 # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
248 # fn=$(echo "$fn" | python -c "from __future__ import print_function; \
249 # import sys; import mimetypes as m; \
250 # [ print(l, end='') for l in sys.stdin if \
251 # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
252 # sort -V | head -n 1)
253 # [ "$fn" = "" ] && return
254 # [ "$bsd" ] && fn=$(printf '%b' "$fn")
256 # [ "$tar" ] && tar --extract --to-stdout \
257 # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
258 # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
259 # [ "$bsd" ] && bsdtar --extract --to-stdout \
260 # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
261 # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
262 # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
263 # "${IMAGE_CACHE_PATH}" && exit 6
264 # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
265 # "${IMAGE_CACHE_PATH}" && exit 6
266 # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
271 TMPPNG
="$(mktemp -t XXXXXX.png)"
272 openscad
--colorscheme="${OPENSCAD_COLORSCHEME}" \
273 --imgsize="${OPENSCAD_IMGSIZE/x/,}" \
274 -o "${TMPPNG}" "${1}" \
275 && mv "${TMPPNG}" "${IMAGE_CACHE_PATH}"
278 case "${FILE_EXTENSION_LOWER}" in
280 ## OpenSCAD only supports png image output, and ${IMAGE_CACHE_PATH}
281 ## is hardcoded as jpeg. So we make a tempfile.png and just
282 ## move/rename it to jpg. This works because image libraries are
283 ## smart enough to handle it.
285 openscad_image
"${FILE_PATH}" && exit 6
287 # 3mf|amf|dxf|off|stl)
288 # openscad_image <(echo "import(\"${FILE_PATH}\");") && exit 6
291 draw.io
-x "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" \
292 --width "${DEFAULT_SIZE%x*}" && exit 6
298 local mimetype
="${1}"
299 case "${mimetype}" in
302 ## Preview as text conversion
303 ## note: catdoc does not always work for .doc files
304 ## catdoc: http://www.wagner.pp.ru/~vitus/software/catdoc/
305 catdoc
-- "${FILE_PATH}" && exit 5
308 ## DOCX, ePub, FB2 (using markdown)
309 ## You might want to remove "|epub" and/or "|fb2" below if you have
310 ## uncommented other methods to preview those formats
311 *wordprocessingml.document
|*/epub
+zip|*/x
-fictionbook+xml
)
312 ## Preview as markdown conversion
313 pandoc
-s -t markdown
-- "${FILE_PATH}" && exit 5
318 ## Parsing performed by mu: https://github.com/djcb/mu
319 mu view
-- "${FILE_PATH}" && exit 5
324 ## Preview as csv conversion
325 ## xls2csv comes with catdoc:
326 ## http://www.wagner.pp.ru/~vitus/software/catdoc/
327 xls2csv
-- "${FILE_PATH}" && exit 5
333 if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
336 if [[ "$( tput colors )" -ge 256 ]]; then
337 local pygmentize_format
='terminal256'
338 local highlight_format
='xterm256'
340 local pygmentize_format
='terminal'
341 local highlight_format
='ansi'
343 env HIGHLIGHT_OPTIONS
="${HIGHLIGHT_OPTIONS}" highlight \
344 --out-format="${highlight_format}" \
345 --force -- "${FILE_PATH}" && exit 5
346 env COLORTERM
=8bit bat
--color=always
--style="plain" \
347 -- "${FILE_PATH}" && exit 5
348 pygmentize
-f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
349 -- "${FILE_PATH}" && exit 5
354 ## Preview as text conversion (requires djvulibre)
355 djvutxt
"${FILE_PATH}" | fmt -w "${PV_WIDTH}" && exit 5
356 exiftool
"${FILE_PATH}" && exit 5
361 ## Preview as text conversion
362 # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
363 exiftool
"${FILE_PATH}" && exit 5
368 mediainfo
"${FILE_PATH}" && exit 5
369 exiftool
"${FILE_PATH}" && exit 5
372 ## ELF files (executables and shared objects)
373 application
/x
-executable | application
/x
-pie-executable | application
/x
-sharedlib)
374 readelf
-WCa "${FILE_PATH}" && exit 5
380 echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
385 MIMETYPE
="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
386 if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
387 handle_image
"${MIMETYPE}"
390 handle_mime
"${MIMETYPE}"