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}
49 case "${FILE_EXTENSION_LOWER}" in
51 a|ace|alz|arc|arj|bz|bz2|cab|
cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
52 rpm|rz|t7z|
tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|
zip)
53 atool
--list -- "${FILE_PATH}" && exit 5
54 bsdtar
--list --file "${FILE_PATH}" && exit 5
57 ## Avoid password prompt by providing empty password
58 unrar lt
-p- -- "${FILE_PATH}" && exit 5
61 ## Avoid password prompt by providing empty password
62 7z l
-p -- "${FILE_PATH}" && exit 5
67 ## Preview as text conversion
68 pdftotext
-l 10 -nopgbrk -q -- "${FILE_PATH}" - |
fmt -w "${PV_WIDTH}" && exit 5
69 mutool draw
-F txt
-i -- "${FILE_PATH}" 1-10 |
fmt -w "${PV_WIDTH}" && exit 5
70 exiftool
"${FILE_PATH}" && exit 5
75 transmission-show
-- "${FILE_PATH}" && exit 5
80 ## Preview as text conversion
81 odt2txt
"${FILE_PATH}" && exit 5
86 ## Preview as text conversion
87 w3m
-dump "${FILE_PATH}" && exit 5
88 lynx
-dump -- "${FILE_PATH}" && exit 5
89 elinks
-dump "${FILE_PATH}" && exit 5
90 ;; # Continue with next handler on failure
93 jq
--color-output .
"${FILE_PATH}" && exit 5
94 python
-m json.tool
-- "${FILE_PATH}" && exit 5
100 ## Size of the preview if there are multiple options or it has to be
101 ## rendered from vector graphics. If the conversion program allows
102 ## specifying only one dimension while keeping the aspect ratio, the width
104 local DEFAULT_SIZE
="1920x1080"
106 local mimetype
="${1}"
107 case "${mimetype}" in
109 # image/svg+xml|image/svg)
110 # convert -- "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
115 # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
116 # - "${IMAGE_CACHE_PATH}" < "${FILE_PATH}" \
117 # && exit 6 || exit 1;;
122 orientation
="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
123 ## If orientation data is present and the image actually
124 ## needs rotating ("1" means no rotation)...
125 if [[ -n "$orientation" && "$orientation" != 1 ]]; then
126 ## ...auto-rotate the image according to the EXIF data.
127 convert
-- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
130 ## `w3mimgdisplay` will be called for all images (unless overriden
131 ## as above), but might fail for unsupported types.
137 # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
142 # pdftoppm -f 1 -l 1 \
143 # -scale-to-x "${DEFAULT_SIZE%x*}" \
146 # -jpeg -tiffcompression jpeg \
147 # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
148 # && exit 6 || exit 1;;
151 ## ePub, MOBI, FB2 (using Calibre)
152 # application/epub+zip|application/x-mobipocket-ebook|\
153 # application/x-fictionbook+xml)
154 # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
155 # epub-thumbnailer "${FILE_PATH}" "${IMAGE_CACHE_PATH}" \
156 # "${DEFAULT_SIZE%x*}" && exit 6
157 # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FILE_PATH}" \
158 # >/dev/null && exit 6
162 application
/font
*|application
/*opentype
)
163 preview_png
="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
164 if fontimage
-o "${preview_png}" \
168 --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
169 --text " abcdefghijklmnopqrstuvwxyz " \
170 --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
171 --text " The quick brown fox jumps over the lazy dog. " \
174 convert
-- "${preview_png}" "${IMAGE_CACHE_PATH}" \
175 && rm "${preview_png}" \
182 ## Preview archives using the first image inside.
183 ## (Very useful for comic book collections for example.)
184 # application/zip|application/x-rar|application/x-7z-compressed|\
185 # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
186 # local fn=""; local fe=""
187 # local zip=""; local rar=""; local tar=""; local bsd=""
188 # case "${mimetype}" in
189 # application/zip) zip=1 ;;
190 # application/x-rar) rar=1 ;;
191 # application/x-7z-compressed) ;;
194 # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
195 # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
196 # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
197 # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
199 # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
200 # [ print(l, end='') for l in sys.stdin if \
201 # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
202 # sort -V | head -n 1)
203 # [ "$fn" = "" ] && return
204 # [ "$bsd" ] && fn=$(printf '%b' "$fn")
206 # [ "$tar" ] && tar --extract --to-stdout \
207 # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
208 # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
209 # [ "$bsd" ] && bsdtar --extract --to-stdout \
210 # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
211 # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
212 # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
213 # "${IMAGE_CACHE_PATH}" && exit 6
214 # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
215 # "${IMAGE_CACHE_PATH}" && exit 6
216 # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
222 local mimetype
="${1}"
223 case "${mimetype}" in
227 if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
230 if [[ "$( tput colors )" -ge 256 ]]; then
231 local pygmentize_format
='terminal256'
232 local highlight_format
='xterm256'
234 local pygmentize_format
='terminal'
235 local highlight_format
='ansi'
237 env HIGHLIGHT_OPTIONS
="${HIGHLIGHT_OPTIONS}" highlight \
238 --out-format="${highlight_format}" \
239 --force -- "${FILE_PATH}" && exit 5
240 pygmentize
-f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
241 -- "${FILE_PATH}" && exit 5
246 ## Preview as text conversion (requires djvulibre)
247 djvutxt
"${FILE_PATH}" |
fmt -w "${PV_WIDTH}" && exit 5
248 exiftool
"${FILE_PATH}" && exit 5
253 ## Preview as text conversion
254 # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
255 exiftool
"${FILE_PATH}" && exit 5
260 mediainfo
"${FILE_PATH}" && exit 5
261 exiftool
"${FILE_PATH}" && exit 5
267 echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
272 MIMETYPE
="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
273 if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
274 handle_image
"${MIMETYPE}"
277 handle_mime
"${MIMETYPE}"