legacy-dots

scope.sh

5.1 kB
  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# 6    | image      | success. display the image $cached points to as an image preview
 20# 7    | image      | success. display the file directly as an image
 21
 22# Meaningful aliases for arguments:
 23path="$1"            # Full path of the selected file
 24width="$1"           # Width of the preview pane (number of fitting characters)
 25height="$2"          # Height of the preview pane (number of fitting characters)
 26cached="$4"          # Path that should be used to cache image previews
 27preview_images="$5"  # "True" if image previews are enabled, "False" otherwise.
 28
 29maxln=200    # Stop after $maxln lines.  Can be used like ls | head -n $maxln
 30
 31# Find out something about the file:
 32mimetype=$(file --mime-type -Lb "$path")
 33extension=$(/bin/echo "${path##*.}" | awk '{print tolower($0)}')
 34
 35# Functions:
 36# runs a command and saves its output into $output.  Useful if you need
 37# the return value AND want to use the output in a pipe
 38try() { output=$(eval '"$@"'); }
 39
 40# writes the output of the previously used "try" command
 41dump() { /bin/echo "$output"; }
 42
 43# a common post-processing function used after most commands
 44trim() { head -n "$maxln"; }
 45
 46# wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
 47safepipe() { "$@"; test $? = 0 -o $? = 141; }
 48
 49# Image previews, if enabled in ranger.
 50if [ "$preview_images" = "True" ]; then
 51    case "$mimetype" in
 52        # Image previews for SVG files, disabled by default.
 53        image/svg+xml)
 54           convert "$path" "$cached" && exit 6 || exit 1;;
 55        # Image previews for image files. w3mimgdisplay will be called for all
 56        # image files (unless overriden as above), but might fail for
 57        # unsupported types.
 58        image/*)
 59            exit 7;;
 60        # Image preview for video, disabled by default.:
 61        video/*)
 62            ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;;
 63    esac
 64fi
 65
 66case "$extension" in
 67    # Archive extensions:
 68    a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
 69    rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
 70        try als "$path" && { dump | trim; exit 0; }
 71        try acat "$path" && { dump | trim; exit 3; }
 72        try bsdtar -lf "$path" && { dump | trim; exit 0; }
 73        exit 1;;
 74    rar)
 75        # avoid password prompt by providing empty password
 76        try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
 77    7z)
 78        # avoid password prompt by providing empty password
 79        try 7z -p l "$path" && { dump | trim; exit 0; } || exit 1;;
 80    # PDF documents:
 81    pdf)
 82        try pdftotext -l 10 -nopgbrk -q "$path" - && \
 83            { dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
 84    # BitTorrent Files
 85    torrent)
 86        try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
 87    # ODT Files
 88    odt|ods|odp|sxw)
 89        try odt2txt "$path" && { dump | trim; exit 5; } || exit 1;;
 90    # HTML Pages:
 91    htm|html|xhtml)
 92        try w3m    -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
 93        try lynx   -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
 94        try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
 95        ;; # fall back to highlight/cat if the text browsers fail
 96esac
 97
 98case "$mimetype" in
 99    # Syntax highlight for text files:
100    text/* | */xml)
101        if [ "$(tput colors)" -ge 256 ]; then
102            pygmentize_format=terminal256
103            highlight_format=xterm256
104        else
105            pygmentize_format=terminal
106            highlight_format=ansi
107        fi
108        try safepipe highlight --out-format=${highlight_format} "$path" && { dump | trim; exit 5; }
109        try safepipe pygmentize -f ${pygmentize_format} "$path" && { dump | trim; exit 5; }
110        exit 2;;
111    # Ascii-previews of images:
112    image/*)
113        img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
114    # Display information about media files:
115    video/* | audio/*)
116        exiftool "$path" && exit 5
117        # Use sed to remove spaces so the output fits into the narrow window
118        try mediainfo "$path" && { dump | trim | sed 's/  \+:/: /;';  exit 5; } || exit 1;;
119esac
120
121exit 1