]> git.rmz.io Git - dotfiles.git/blob - ranger/scope.sh
ranger: update scope.sh
[dotfiles.git] / ranger / scope.sh
1 #!/usr/bin/env 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.
5
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
9
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
21
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.
28
29 maxln=200 # Stop after $maxln lines. Can be used like ls | head -n $maxln
30
31 # Find out something about the file:
32 mimetype=$(file --mime-type -Lb "$path")
33 extension=$(/bin/echo "${path##*.}" | awk '{print tolower($0)}')
34
35 # Functions:
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 '"$@"'); }
39
40 # writes the output of the previously used "try" command
41 dump() { /bin/echo "$output"; }
42
43 # a common post-processing function used after most commands
44 trim() { head -n "$maxln"; }
45
46 # wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
47 safepipe() { "$@"; test $? = 0 -o $? = 141; }
48
49 # Image previews, if enabled in ranger.
50 if [ "$preview_images" = "True" ]; then
51 case "$mimetype" in
52 # Image previews for SVG files, disabled by default.
53 ###image/svg+xml)
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
57 # unsupported types.
58 image/*)
59 exit 7;;
60 # Image preview for video, disabled by default.:
61 ###video/*)
62 ### ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;;
63 esac
64 fi
65
66 case "$extension" in
67 # Archive extensions:
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 als "$path" && { dump | trim; exit 0; }
71 try acat "$path" && { dump | trim; exit 3; }
72 try bsdtar -lf "$path" && { dump | trim; exit 0; }
73 exit 1;;
74 rar)
75 try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
76 # PDF documents:
77 pdf)
78 try pdftotext -l 10 -nopgbrk -q "$path" - && \
79 { dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
80 # BitTorrent Files
81 torrent)
82 try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
83 # ODT Files
84 odt|ods|odp|sxw)
85 try odt2txt "$path" && { dump | trim; exit 5; } || exit 1;;
86 # HTML Pages:
87 htm|html|xhtml)
88 try w3m -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
89 try lynx -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
90 try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
91 ;; # fall back to highlight/cat if the text browsers fail
92 esac
93
94 case "$mimetype" in
95 # Syntax highlight for text files:
96 text/* | */xml)
97 if [ "$(tput colors)" -ge 256 ]; then
98 pygmentize_format=terminal256
99 highlight_format=xterm256
100 else
101 pygmentize_format=terminal
102 highlight_format=ansi
103 fi
104 try safepipe highlight --out-format=${highlight_format} "$path" && { dump | trim; exit 5; }
105 try safepipe pygmentize -f ${pygmentize_format} "$path" && { dump | trim; exit 5; }
106 exit 2;;
107 # Ascii-previews of images:
108 image/*)
109 img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
110 # Display information about media files:
111 video/* | audio/*)
112 exiftool "$path" && exit 5
113 # Use sed to remove spaces so the output fits into the narrow window
114 try mediainfo "$path" && { dump | trim | sed 's/ \+:/: /;'; exit 5; } || exit 1;;
115 esac
116
117 exit 1