[download]

config/ranger/scope.sh

   1 #!/bin/bash
   2 
   3 # Meanings of exit codes:
   4 # code | meaning    | action of ranger
   5 # -----+------------+-------------------------------------------
   6 # 0    | success    | Success. Display stdout as preview
   7 # 1    | no preview | Failure. Display no preview at all
   8 # 2    | plain text | Display the plain content of the file
   9 # 3    | fix width  | Success. Don't reload when width changes
  10 # 4    | fix height | Success. Don't reload when height changes
  11 # 5    | fix both   | Success. Don't ever reload
  12 # 6    | image      | Success. Display the image $cache points to as an image preview
  13 # 7    | image      | Display the file directly as an image
  14 
  15 path=$1  width=$2
  16 cache=$4 image=${5/False}
  17 
  18 ext=${path##*.}
  19 mime=$(file -Lb --mime-type "$path")
  20 
  21 mkdir -p ${cache%/*}
  22 
  23 mediainfo(){
  24     command mediainfo "$@" | sed 's/\s*:\s*/| /' | column -ts \| -o :
  25 }
  26 
  27 ################################################################################
  28 
  29 case $path in
  30     */Mail/*) frm -Sq "$path" | fmt -w $width && exit 4;;
  31 esac
  32 
  33 ################################################################################
  34 
  35 [[ $image ]] && case ${ext,,} in
  36     ttf|otf|bmp|svg) convert "$path" $cache && exit 6;;
  37 
  38       xcf) xcf2png  "$path" -o $cache && exit 6;;
  39       kra) unzip -p "$path" preview.png > $cache && exit 6;;
  40     blend) blender-thumbnailer.py "$path" $cache && exit 6;;
  41 
  42     swf) gnash-thumbnailer "$path"  $cache 512       && exit 6;;
  43     pdf) pdftoppm -singlefile -jpeg "$path" > $cache && exit 6;;
  44 esac
  45 
  46 case ${ext,,} in
  47      odt) odt2txt "$path" && exit 4;;
  48     json) jq -C . "$path" && exit 4;;
  49     mbox) frm -Sn "$path" | tac     && exit 4;;
  50      svg) highlight -O ansi "$path" && exit 4; exit 2;;
  51       db) sqlite3 "$path" .schema | highlight -O ansi -WS sql && exit 4;;
  52 
  53      gpg|asc) gpg -d "$path" && exit 4;    exit 1;;
  54      torrent) transmission-show "$path" && exit 4;;
  55      cfg|cnf) highlight -O ansi -S conf "$path" && exit 4; exit 2;;
  56     opml|dae) highlight -O ansi -S xml  "$path" && exit 4; exit 2;;
  57 
  58     godot|import|tres|tscn) highlight -O ansi -S ini "$path" && exit 4; exit 2;;
  59 esac
  60 
  61 ################################################################################
  62 
  63 [[ $image ]] && case $mime in
  64     image/*) exit 7;;
  65     video/*) ffmpegthumbnailer -i "$path" -o $cache -s 0 && exit 6;;
  66 esac
  67 
  68 case $mime in
  69     image/*)
  70         LANG= exiv2 "$path" && exit 4
  71         mediainfo   "$path" && exit 4
  72     ;;
  73     audio/*|video/*)      mediainfo "$path" && exit 4;;
  74     text/*|*/xml) highlight -O ansi "$path" && exit 4; exit 2;;
  75 esac
  76 
  77 ################################################################################
  78 
  79 exit 1