]>
git.rmz.io Git - dotfiles.git/blob - ranger/scope.sh
2 # ranger supports enhanced previews. If the option "use_preview_script"
3 # is set to True and this file exists, this script will be called and its
4 # output is displayed in ranger. ANSI color codes are supported.
6 # NOTES: This script is considered a configuration file. If you upgrade
7 # ranger, it will be left untouched. (You must update it yourself.)
8 # Also, ranger disables STDIN here, so interactive scripts won't work properly
10 # Meanings of exit codes:
11 # code | meaning | action of ranger
12 # -----+------------+-------------------------------------------
13 # 0 | success | success. display stdout as preview
14 # 1 | no preview | failure. display no preview at all
15 # 2 | plain text | display the plain content of the file
16 # 3 | fix width | success. Don't reload when width changes
17 # 4 | fix height | success. Don't reload when height changes
18 # 5 | fix both | success. Don't ever reload
19 # 6 | image | success. display the image $cached points to as an image preview
20 # 7 | image | success. display the file directly as an image
22 # Meaningful aliases for arguments:
23 path
="$1" # Full path of the selected file
24 width
="$2" # Width of the preview pane (number of fitting characters)
25 height
="$3" # Height of the preview pane (number of fitting characters)
26 cached
="$4" # Path that should be used to cache image previews
27 preview_images
="$5" # "True" if image previews are enabled, "False" otherwise.
29 maxln
=200 # Stop after $maxln lines. Can be used like ls | head -n $maxln
31 # Find out something about the file:
32 mimetype
=$(file --mime-type -Lb "$path")
33 extension
=$(/bin/echo "${path##*.}" | awk '{print tolower($0)}')
36 # runs a command and saves its output into $output. Useful if you need
37 # the return value AND want to use the output in a pipe
38 try() { output=$(eval '"$@"'); }
40 # writes the output of the previously used "try" command
41 dump() { /bin/echo "$output"; }
43 # a common post-processing function used after most commands
44 trim() { head -n "$maxln"; }
46 # wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
47 safepipe() { "$@"; test $? = 0 -o $? = 141; }
49 # Image previews, if enabled in ranger.
50 if [ "$preview_images" = "True" ]; then
52 # Image previews for SVG files, disabled by default.
54 ### convert "$path" "$cached" && exit 6 || exit 1;;
55 # Image previews for image files. w3mimgdisplay will be called for all
56 # image files (unless overriden as above), but might fail for
60 # Image preview for video, disabled by default.:
62 ### ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;;
68 7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
69 rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
70 try tar taf "$path" && { dump | trim; exit 0; }
71 try als "$path" && { dump | trim; exit 0; }
72 try acat "$path" && { dump | trim; exit 3; }
73 try bsdtar -lf "$path" && { dump | trim; exit 0; }
76 try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
79 try pdftotext -l 10 -nopgbrk -q "$path" - && \
80 { dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
83 try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
86 try odt2txt "$path" && { dump | trim; exit 5; } || exit 1;;
89 try w3m -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
90 try lynx -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
91 try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
92 ;; # fall back to highlight/cat if the text browsers fail
96 # Syntax highlight for text files:
98 if [ "$(tput colors)" -ge 256 ]; then
99 pygmentize_format=terminal256
100 highlight_format=xterm256
102 pygmentize_format=terminal
103 highlight_format=ansi
105 try safepipe highlight --out-format=${highlight_format} "$path" && { dump | trim; exit 5; }
106 try safepipe pygmentize -f ${pygmentize_format} "$path" && { dump | trim; exit 5; }
108 # Ascii-previews of images:
110 img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
111 # Display information about media files:
113 exiftool "$path" && exit 5
114 # Use sed to remove spaces so the output fits into the narrow window
115 try mediainfo "$path" && { dump | trim | sed 's
/ \
+:/: /;'; exit 5; } || exit 1;;