summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2013-02-06 02:08:32 +0100
committerhut <hut@lavabit.com>2013-02-06 02:08:32 +0100
commite27ff2b6237e4d8230a2bf87bfee444651449cb6 (patch)
tree507b1694b618742bfa2d8f92d37c1680b5b60c92 /ranger
parented4cac185ac1ffa0bba14b6be65a268791b78ada (diff)
parent94c814a4adfeadfff75a2e27760f49eabfe8bbd3 (diff)
downloadranger-e27ff2b6237e4d8230a2bf87bfee444651449cb6.tar.gz
Merge branch 'posix_scope'
Diffstat (limited to 'ranger')
-rwxr-xr-xranger/data/scope.sh90
1 files changed, 45 insertions, 45 deletions
diff --git a/ranger/data/scope.sh b/ranger/data/scope.sh
index 6b89049a..64bab712 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
-		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
+    # 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 -lf "$path" && { dump | trim; exit 0; }
+        exit 1;;
+    rar)
+        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 | trim; 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