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 # Meanings of exit codes:
15 # code | meaning | action of ranger
16 # -----+------------+-------------------------------------------
17 # 0 | success | Display stdout as preview
18 # 1 | no preview | Display no preview at all
19 # 2 | plain text | Display the plain content of the file
20 # 3 | fix width | Don't reload when width changes
21 # 4 | fix height | Don't reload when height changes
22 # 5 | fix both | Don't ever reload
23 # 6 | image | Display the image `$IMAGE_CACHE_PATH` points to as an image preview
24 # 7 | image | Display the file directly as an image
27 FILE_PATH="${1}" # Full path of the highlighted file
28 PV_WIDTH="${2}" # Width of the preview pane (number of fitting characters)
29 PV_HEIGHT="${3}" # Height of the preview pane (number of fitting characters)
30 IMAGE_CACHE_PATH="${4}" # Full path that should be used to cache image preview
31 PV_IMAGE_ENABLED="${5}" # 'True' if image previews are enabled, 'False' otherwise.
33 FILE_EXTENSION="${FILE_PATH##*.}"
34 FILE_EXTENSION_LOWER=$(echo ${FILE_EXTENSION} | tr '[:upper:]' '[:lower:]')
37 HIGHLIGHT_SIZE_MAX=262143 # 256KiB
39 HIGHLIGHT_STYLE='pablo'
40 PYGMENTIZE_STYLE='autumn'
44 case "${FILE_EXTENSION_LOWER}" in
46 a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
47 rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
48 atool --list -- "${FILE_PATH}" && exit 5
49 bsdtar --list --file "${FILE_PATH}" && exit 5
52 # Avoid password prompt by providing empty password
53 unrar lt -p- -- "${FILE_PATH}" && exit 5
56 # Avoid password prompt by providing empty password
57 7z l -p -- "${FILE_PATH}" && exit 5
62 # Preview as text conversion
63 pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | fmt -w ${PV_WIDTH} && exit 5
64 mutool draw -F txt -i -- "${FILE_PATH}" 1-10 | fmt -w ${PV_WIDTH} && exit 5
65 exiftool "${FILE_PATH}" && exit 5
70 transmission-show -- "${FILE_PATH}" && exit 5
75 # Preview as text conversion
76 odt2txt "${FILE_PATH}" && exit 5
81 # Preview as text conversion
82 w3m -dump "${FILE_PATH}" && exit 5
83 lynx -dump -- "${FILE_PATH}" && exit 5
84 elinks -dump "${FILE_PATH}" && exit 5
85 ;; # Continue with next handler on failure
94 # convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
100 orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
101 # If orientation data is present and the image actually
102 # needs rotating ("1" means no rotation)...
103 if [[ -n "$orientation" && "$orientation" != 1 ]]; then
104 # ...auto-rotate the image according to the EXIF data.
105 convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
108 # `w3mimgdisplay` will be called for all images (unless overriden as above),
109 # but might fail for unsupported types.
115 # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
119 # pdftoppm -f 1 -l 1 \
123 # -jpeg -tiffcompression jpeg \
124 # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \
125 # && exit 6 || exit 1;;
127 # Preview archives using the first image inside.
128 # (Very useful for comic book collections for example.)
129 # application/zip|application/x-rar|application/x-7z-compressed|\
130 # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
131 # local fn=""; local fe=""
132 # local zip=""; local rar=""; local tar=""; local bsd=""
133 # case "${mimetype}" in
134 # application/zip) zip=1 ;;
135 # application/x-rar) rar=1 ;;
136 # application/x-7z-compressed) ;;
139 # { [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}"); } || \
140 # { fn=$(bsdtar --list --file "${FILE_PATH}") && bsd=1 && tar=""; } || \
141 # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FILE_PATH}"); } || \
142 # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FILE_PATH}"); } || return
144 # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
145 # [ print(l, end='') for l in sys.stdin if \
146 # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
147 # sort -V | head -n 1)
148 # [ "$fn" = "" ] && return
149 # [ "$bsd" ] && fn=$(printf '%b' "$fn")
151 # [ "$tar" ] && tar --extract --to-stdout \
152 # --file "${FILE_PATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
153 # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
154 # [ "$bsd" ] && bsdtar --extract --to-stdout \
155 # --file "${FILE_PATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
156 # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
157 # [ "$rar" ] && unrar p -p- -inul -- "${FILE_PATH}" "$fn" > \
158 # "${IMAGE_CACHE_PATH}" && exit 6
159 # [ "$zip" ] && unzip -pP "" -- "${FILE_PATH}" "$fe" > \
160 # "${IMAGE_CACHE_PATH}" && exit 6
161 # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
167 local mimetype="${1}"
168 case "${mimetype}" in
172 if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
175 if [[ "$( tput colors )" -ge 256 ]]; then
176 local pygmentize_format='terminal256'
177 local highlight_format='xterm256'
179 local pygmentize_format='terminal'
180 local highlight_format='ansi'
182 highlight --replace-tabs="${HIGHLIGHT_TABWIDTH}" --out-format="${highlight_format}" \
183 --style="${HIGHLIGHT_STYLE}" --force -- "${FILE_PATH}" && exit 5
184 # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}" -- "${FILE_PATH}" && exit 5
189 # Preview as text conversion
190 # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
191 exiftool "${FILE_PATH}" && exit 5
196 mediainfo "${FILE_PATH}" && exit 5
197 exiftool "${FILE_PATH}" && exit 5
203 echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
208 MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
209 if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
210 handle_image "${MIMETYPE}"
213 handle_mime "${MIMETYPE}"