summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xranger/data/scope.sh86
1 files changed, 43 insertions, 43 deletions
diff --git a/ranger/data/scope.sh b/ranger/data/scope.sh
index 6b89049a..d17d24de 100755
--- a/ranger/data/scope.sh
+++ b/ranger/data/scope.sh
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/usr/bin/env sh
 # ranger supports enhanced previews.  If the option "use_preview_script"
 # is set to True and this file exists, this script will be called and its
 # output is displayed in ranger.  ANSI color codes are supported.
@@ -29,56 +29,56 @@ mimetype=$(file --mime-type -Lb "$path")
 extension=${path##*.}
 
 # Functions:
-# "have $1" succeeds if $1 is an existing command/installed program
-function have { type -P "$1" > /dev/null; }
-# "success" returns the exit code of the first program in the last pipe chain
-function success { test ${PIPESTATUS[0]} = 0; }
+# runs a command and saves its output into $output.  Useful if you need
+# the return value AND want to use the output in a pipe
+try() { output=$(eval $@); }
+
+# writes the output of the previouosly used "try" command
+dump() { echo "$output"; }
+
+# a common post-processing function used after most commands
+trim() { head -n "$maxln"; }
+
 # wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
-function highlight { command highlight "$@"; test $? = 0 -o $? = 141; }
+highlight() { command highlight "$@"; test $? = 0 -o $? = 141; }
 
 case "$extension" in
-	# Archive extensions:
-	7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
-	rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
-		als "$path" | head -n $maxln
-		success && exit 0 || acat "$path" | head -n $maxln && exit 3
+    # Archive extensions:
+    7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
+    rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
+		try als "$path" && { dump | trim; exit 0; }
+		try acat "$path" && { dump | trim; exit 3; }
+		try bsdtar "$path" && { dump | trim; exit 0; }
 		exit 1;;
 	rar)
-		unrar -p- lt "$path" | head -n $maxln
-		success && exit 0 || exit 1;;
-	# PDF documents:
-	pdf)
-		pdftotext -l 10 -nopgbrk -q "$path" - | head -n $maxln | fmt -s -w $width
-		success && exit 0 || exit 1;;
-	# BitTorrent Files
-	torrent)
-		transmission-show "$path" | head -n $maxln && exit 3
-		success && exit 5 || exit 1;;
-	# HTML Pages:
-	htm|html|xhtml)
-		have w3m    && w3m    -dump "$path" | head -n $maxln | fmt -s -w $width && exit 4
-		have lynx   && lynx   -dump "$path" | head -n $maxln | fmt -s -w $width && exit 4
-		have elinks && elinks -dump "$path" | head -n $maxln | fmt -s -w $width && exit 4
-		;; # fall back to highlight/cat if theres no lynx/elinks
+		try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
+    # PDF documents:
+    pdf)
+        try pdftotext -l 10 -nopgbrk -q "$path" - && \
+			{ dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
+    # BitTorrent Files
+    torrent)
+		try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
+    # HTML Pages:
+    htm|html|xhtml)
+		try w3m    -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
+		try lynx   -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
+		try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
+		;; # fall back to highlight/cat if the text browsers fail
 esac
 
 case "$mimetype" in
-	# Syntax highlight for text files:
-	text/* | */xml)
-		highlight --out-format=ansi "$path" | head -n $maxln
-		success && exit 5 || exit 2;;
-	# Ascii-previews of images:
-	image/*)
-		img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
-	# Display information about media files:
-	video/* | audio/*)
-		have exiftool && exiftool "$path" && exit 5
-		# Use sed to remove spaces so the output fits into the narrow window
-		if have mediainfo; then
-			mediainfo "$path" | sed 's/  \+:/: /;'
-			success && exit 5
-		fi
-		exit 1;;
+    # Syntax highlight for text files:
+    text/* | */xml)
+		try highlight --out-format=ansi "$path" && { dump; exit 5; } || exit 2;;
+    # Ascii-previews of images:
+    image/*)
+        img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
+    # Display information about media files:
+    video/* | audio/*)
+        exiftool "$path" && exit 5
+        # Use sed to remove spaces so the output fits into the narrow window
+		try mediainfo "$path" && { dump | trim | sed 's/  \+:/: /;';  exit 5; } || exit 1;;
 esac
 
 exit 1
4:53 -0800 7162' href='/akkartik/mu/commit/html/apps/arith.mu.html?h=main&id=3d1c4216ede8c628558c9fe700fb2be1aa08b473'>3d1c4216 ^
62151e17 ^
3d1c4216 ^




62151e17 ^
3d1c4216 ^














































8f9b21f0 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321