64bab7122db9fa230795a2a0fe0198200c0331bd
[~bandali/configs] / ranger / .config / 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
20 # Meaningful aliases for arguments:
21 path="$1" # Full path of the selected file
22 width="$2" # Width of the preview pane (number of fitting characters)
23 height="$3" # Height of the preview pane (number of fitting characters)
24
25 maxln=200 # Stop after $maxln lines. Can be used like ls | head -n $maxln
26
27 # Find out something about the file:
28 mimetype=$(file --mime-type -Lb "$path")
29 extension=${path##*.}
30
31 # Functions:
32 # runs a command and saves its output into $output. Useful if you need
33 # the return value AND want to use the output in a pipe
34 try() { output=$(eval '"$@"'); }
35
36 # writes the output of the previouosly used "try" command
37 dump() { echo "$output"; }
38
39 # a common post-processing function used after most commands
40 trim() { head -n "$maxln"; }
41
42 # wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
43 highlight() { command highlight "$@"; test $? = 0 -o $? = 141; }
44
45 case "$extension" in
46 # Archive extensions:
47 7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
48 rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
49 try als "$path" && { dump | trim; exit 0; }
50 try acat "$path" && { dump | trim; exit 3; }
51 try bsdtar -lf "$path" && { dump | trim; exit 0; }
52 exit 1;;
53 rar)
54 try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
55 # PDF documents:
56 pdf)
57 try pdftotext -l 10 -nopgbrk -q "$path" - && \
58 { dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
59 # BitTorrent Files
60 torrent)
61 try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
62 # HTML Pages:
63 htm|html|xhtml)
64 try w3m -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
65 try lynx -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
66 try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
67 ;; # fall back to highlight/cat if the text browsers fail
68 esac
69
70 case "$mimetype" in
71 # Syntax highlight for text files:
72 text/* | */xml)
73 try highlight --out-format=ansi "$path" && { dump | trim; exit 5; } || exit 2;;
74 # Ascii-previews of images:
75 image/*)
76 img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
77 # Display information about media files:
78 video/* | audio/*)
79 exiftool "$path" && exit 5
80 # Use sed to remove spaces so the output fits into the narrow window
81 try mediainfo "$path" && { dump | trim | sed 's/ \+:/: /;'; exit 5; } || exit 1;;
82 esac
83
84 exit 1