summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-10-09 22:57:49 +0200
committerhut <hut@lavabit.com>2010-10-09 22:58:38 +0200
commitfecebfe81e90d565f4af60c91d3ebc76f29d3c21 (patch)
treec174e8d40c2a6d9b74485eafcf3c6cb95947a76a
parentb76bb9a154d69abe7cbb2558faaddf1cc3c1397a (diff)
downloadranger-fecebfe81e90d565f4af60c91d3ebc76f29d3c21.tar.gz
data/scope.sh: improved
-rwxr-xr-xranger/data/scope.sh46
1 files changed, 28 insertions, 18 deletions
diff --git a/ranger/data/scope.sh b/ranger/data/scope.sh
index 9ce35fd9..c99f715b 100755
--- a/ranger/data/scope.sh
+++ b/ranger/data/scope.sh
@@ -3,15 +3,9 @@
 # This script is called whenever you preview a file.
 # Its output is used as the preview.  ANSI color codes are supported.
 
-# NOTE: This is considered to be a configuration file.  If you upgrade
+# NOTES: This script is considered a configuration file.  If you upgrade
 # ranger, it will be left untouched. (You must update it yourself)
-
-# Meanings of arguments:
-# name | meaning
-# -----+--------------------------------------------------------
-# $1   | Full filename of the selected file
-# $2   | Width of the preview pane (number of fitting characters)
-# $3   | Height of the preview pane (number of fitting characters)
+# NEVER make this script interactive. (by starting mplayer or something)
 
 # Meanings of exit codes:
 # code | meaning    | action of ranger
@@ -19,31 +13,47 @@
 # 0    | success    | display stdout as preview
 # 1    | no preview | display no preview at all
 # 2    | plain text | display the plain content of the file
+# 3    | fix width  | success. Don't reload when width changes
+# 4    | fix height | success. Don't reload when height changes
+# 5    | fix both   | success. Don't ever reload
+
+# Meaningful aliases for arguments:
+path="$1"    # Full path of the selected file
+width="$2"   # Width of the preview pane (number of fitting characters)
+height="$3"  # Height of the preview pane (number of fitting characters)
+
+# Find out something about the file:
+mimetype=$(file --mime-type -Lb "$path")
+extension=$(echo "$path" | grep '\.' | grep -o '[^.]\+$')
 
-mimetype=$(file --mime-type -Lb "$1")
-extension=$(echo "$1" | grep '\.' | grep -o '[^.]\+$')
+# Other useful stuff
+maxln=200                                   # print up to $maxln lines
+function have { type -P "$1" > /dev/null; } # test if program is installed
 
 case "$extension" in
 	# Archive extensions:
 	7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|gz|jar|lha|lz|lzh|lzma|lzo\
 	|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xz|Z|zip)
-		atool -l "$1" || exit 1
-		exit 0;;
+		atool -l "$path" | head -n $maxln && exit 3
+		exit 1;;
+	pdf)
+		pdftotext "$path" | head -n $maxln && exit 3
+		exit 1;;
 	# HTML Pages:
 	htm|html|xhtml)
-		lynx -dump "$1" || elinks -dump "$1" || exit 1
-		exit 0;;
+		have lynx && lynx -dump "$path" | head -n $maxln && exit 5
+		have elinks && elinks -dump "$path" | head -n $maxln && exit 5
+		;; # fall back to highlight/cat if theres no lynx/elinks
 esac
 
 case "$mimetype" in
 	# Syntax highlight for text files:
 	text/* | */xml)
-		highlight --ansi "$1" || cat "$1" || exit 1
-		exit 0;;
+		(highlight --ansi "$path" || cat "$path") | head -n $maxln
+		exit 5;;
 	# Ascii-previews of images:
 	image/*)
-		img2txt -W "$2" "$1" || exit 1
-		exit 0;;
+		img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
 esac
 
 exit 1