From f9e0d5be06b9a39307fbb94b7474427d165c5bd2 Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 5 Feb 2013 02:47:39 +0100 Subject: data/scope.sh: rewrote in POSIX shell --- ranger/data/scope.sh | 86 ++++++++++++++++++++++++++-------------------------- 1 file 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 -- cgit 1.4.1-2-gfad0