scope.sh (8326B)
1 #!/usr/bin/env bash 2 3 set -o noclobber -o noglob -o nounset -o pipefail 4 IFS=$'\n' 5 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 10 11 # This script is considered a configuration file and must be updated manually. 12 # It will be left untouched if you upgrade ranger. 13 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 25 26 # Script arguments 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. 32 33 FILE_EXTENSION="${FILE_PATH##*.}" 34 FILE_EXTENSION_LOWER=$(echo ${FILE_EXTENSION} | tr '[:upper:]' '[:lower:]') 35 36 # Settings 37 HIGHLIGHT_SIZE_MAX=262143 # 256KiB 38 HIGHLIGHT_TABWIDTH=8 39 HIGHLIGHT_STYLE='pablo' 40 PYGMENTIZE_STYLE='autumn' 41 42 43 handle_extension() { 44 case "${FILE_EXTENSION_LOWER}" in 45 # Archive 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 50 exit 1;; 51 rar) 52 # Avoid password prompt by providing empty password 53 unrar lt -p- -- "${FILE_PATH}" && exit 5 54 exit 1;; 55 7z) 56 # Avoid password prompt by providing empty password 57 7z l -p -- "${FILE_PATH}" && exit 5 58 exit 1;; 59 60 # PDF 61 pdf) 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 66 exit 1;; 67 68 # BitTorrent 69 torrent) 70 transmission-show -- "${FILE_PATH}" && exit 5 71 exit 1;; 72 73 # OpenDocument 74 odt|ods|odp|sxw) 75 # Preview as text conversion 76 odt2txt "${FILE_PATH}" && exit 5 77 exit 1;; 78 79 # HTML 80 htm|html|xhtml) 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 86 esac 87 } 88 89 handle_image() { 90 local mimetype="${1}" 91 case "${mimetype}" in 92 # SVG 93 # image/svg+xml) 94 # convert "${FILE_PATH}" "${IMAGE_CACHE_PATH}" && exit 6 95 # exit 1;; 96 97 # Image 98 image/*) 99 local orientation 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 106 fi 107 108 # `w3mimgdisplay` will be called for all images (unless overriden as above), 109 # but might fail for unsupported types. 110 exit 7;; 111 112 # Video 113 # video/*) 114 # # Thumbnail 115 # ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6 116 # exit 1;; 117 # PDF 118 # application/pdf) 119 # pdftoppm -f 1 -l 1 \ 120 # -scale-to-x 1920 \ 121 # -scale-to-y -1 \ 122 # -singlefile \ 123 # -jpeg -tiffcompression jpeg \ 124 # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \ 125 # && exit 6 || exit 1;; 126 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) ;; 137 # *) tar=1 ;; 138 # esac 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 143 # 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") 150 # 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}" 162 # ;; 163 esac 164 } 165 166 handle_mime() { 167 local mimetype="${1}" 168 case "${mimetype}" in 169 # Text 170 text/* | */xml) 171 # Syntax highlight 172 if [[ "$( stat --printf='%s' -- "${FILE_PATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then 173 exit 2 174 fi 175 if [[ "$( tput colors )" -ge 256 ]]; then 176 local pygmentize_format='terminal256' 177 local highlight_format='xterm256' 178 else 179 local pygmentize_format='terminal' 180 local highlight_format='ansi' 181 fi 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 185 exit 2;; 186 187 # Image 188 image/*) 189 # Preview as text conversion 190 # img2txt --gamma=0.6 --width="${PV_WIDTH}" -- "${FILE_PATH}" && exit 4 191 exiftool "${FILE_PATH}" && exit 5 192 exit 1;; 193 194 # Video and audio 195 video/* | audio/*) 196 mediainfo "${FILE_PATH}" && exit 5 197 exiftool "${FILE_PATH}" && exit 5 198 exit 1;; 199 esac 200 } 201 202 handle_fallback() { 203 echo '----- File Type Classification -----' && file --dereference --brief -- "${FILE_PATH}" && exit 5 204 exit 1 205 } 206 207 208 MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )" 209 if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then 210 handle_image "${MIMETYPE}" 211 fi 212 handle_extension 213 handle_mime "${MIMETYPE}" 214 handle_fallback 215 216 exit 1