Add GitHub Actions ci
[ranger.git] / ranger / data / scope.sh
blobeb09b2bf9ad1acbe359854f0f84d641cf491a796
1 #!/usr/bin/env bash
3 set -o noclobber -o noglob -o nounset -o pipefail
4 IFS=$'\n'
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
30 ## Script arguments
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:]')"
41 ## Settings
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}
48 handle_extension() {
49 case "${FILE_EXTENSION_LOWER}" in
50 ## Archive
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
55 exit 1;;
56 rar)
57 ## Avoid password prompt by providing empty password
58 unrar lt -p- -- "${FILE_PATH}" && exit 5
59 exit 1;;
60 7z)
61 ## Avoid password prompt by providing empty password
62 7z l -p -- "${FILE_PATH}" && exit 5
63 exit 1;;
65 ## PDF
66 pdf)
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
71 exit 1;;
73 ## BitTorrent
74 torrent)
75 transmission-show -- "${FILE_PATH}" && exit 5
76 exit 1;;
78 ## OpenDocument
79 odt|ods|odp|sxw)
80 ## Preview as text conversion
81 odt2txt "${FILE_PATH}" && exit 5
82 exit 1;;
84 ## HTML
85 htm|html|xhtml)
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
91 ## JSON
92 json)
93 jq --color-output . "${FILE_PATH}" && exit 5
94 python -m json.tool -- "${FILE_PATH}" && exit 5
96 esac
99 handle_image() {
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
103 ## will be used.
104 local DEFAULT_SIZE="1920x1080"
106 local mimetype="${1}"
107 case "${mimetype}" in
108 ## SVG
109 # image/svg+xml|image/svg)
110 # convert -- "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6
111 # exit 1;;
113 ## DjVu
114 # image/vnd.djvu)
115 # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
116 # - "${IMAGE_CACHE_PATH}" < "${FILE_PATH}" \
117 # && exit 6 || exit 1;;
119 ## Image
120 image/*)
121 local orientation
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.
132 exit 7;;
134 ## Video
135 # video/*)
136 # # Thumbnail
137 # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
138 # exit 1;;
140 ## PDF
141 # application/pdf)
142 # pdftoppm -f 1 -l 1 \
143 # -scale-to-x "${DEFAULT_SIZE%x*}" \
144 # -scale-to-y -1 \
145 # -singlefile \
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
159 # exit 1;;
161 ## Font
162 application/font*|application/*opentype)
163 preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
164 if fontimage -o "${preview_png}" \
165 --pixelsize "120" \
166 --fontname \
167 --pixelsize "80" \
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. " \
172 "${FILE_PATH}";
173 then
174 convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
175 && rm "${preview_png}" \
176 && exit 6
177 else
178 exit 1
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) ;;
192 # *) tar=1 ;;
193 # esac
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}"
217 # ;;
218 esac
221 handle_mime() {
222 local mimetype="${1}"
223 case "${mimetype}" in
224 ## Text
225 text/* | */xml)
226 ## Syntax highlight
227 if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
228 exit 2
230 if [[ "$( tput colors )" -ge 256 ]]; then
231 local pygmentize_format='terminal256'
232 local highlight_format='xterm256'
233 else
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
242 exit 2;;
244 ## DjVu
245 image/vnd.djvu)
246 ## Preview as text conversion (requires djvulibre)
247 djvutxt "${FILE_PATH}" | fmt -w "${PV_WIDTH}" && exit 5
248 exiftool "${FILE_PATH}" && exit 5
249 exit 1;;
251 ## Image
252 image/*)
253 ## Preview as text conversion
254 # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4
255 exiftool "${FILE_PATH}" && exit 5
256 exit 1;;
258 ## Video and audio
259 video/* | audio/*)
260 mediainfo "${FILE_PATH}" && exit 5
261 exiftool "${FILE_PATH}" && exit 5
262 exit 1;;
263 esac
266 handle_fallback() {
267 echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5
268 exit 1
272 MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
273 if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
274 handle_image "${MIMETYPE}"
276 handle_extension
277 handle_mime "${MIMETYPE}"
278 handle_fallback
280 exit 1