config/ranger/scope.sh

   1 #!/bin/bash
   2 # Meanings of exit codes:
   3 # code | meaning    | action of ranger
   4 # -----+------------+-------------------------------------------
   5 # 0    | success    | Success. Display stdout as preview
   6 # 1    | no preview | Failure. Display no preview at all
   7 # 2    | plain text | Display the plain content of the file
   8 # 3    | fix width  | Success. Don't reload when width changes
   9 # 4    | fix height | Success. Don't reload when height changes
  10 # 5    | fix both   | Success. Don't ever reload
  11 # 6    | image      | Success. Display the image $cache points to as an image preview
  12 # 7    | image      | Display the file directly as an image
  13 
  14 path="$1"
  15 cache="$4"
  16 
  17 width="$2"
  18 height="$3"
  19 
  20 ext=${path##*.}
  21 ext=${ext,,}
  22 mime=$(file -Lb --mime-type "$path")
  23 
  24 mkdir -p ${cache%/*}
  25 
  26 case $ext in
  27     odt) odt2txt "$path" && exit 5;;
  28     ttf|otf) convert "$path" "$cache" && exit 6;;
  29     swf) gnash-thumbnailer "$path" "$cache" 512 && exit 6;;
  30     pdf) pdftoppm -singlefile -jpeg "$path" > "$cache" && exit 6;;
  31 
  32     svg) inkscape "$path" -e "$cache" && exit 6;;
  33     xcf) xcf2png "$path" -o "$cache" && exit 6;;
  34     kra) unzip -p "$path" preview.png > "$cache" && exit 6;;
  35     blend) blender-thumbnailer.py "$path" "$cache" && exit 6;;
  36     dae) highlight -O ansi -S xml "$path" && exit 5 || exit 2;;
  37 
  38     json) jq -C . "$path" && exit 5;;
  39     mbox) frm -S "$path" | tac && exit 5;;
  40     torrent) transmission-show "$path" && exit 5;;
  41     db) sqlite3 "$path" .schema | highlight -O ansi -WS sql && exit 5;;
  42     cfg|cnf) highlight -O ansi -S conf "$path" && exit 5 || exit 2;;
  43     opml) highlight -O ansi -S xml "$path" && exit 5 || exit 2;;
  44     godot|import|tres|tscn) highlight -O ansi -S ini "$path" && exit 5 || exit 2;;
  45 esac
  46 
  47 case $mime in
  48     image/*) exit 7;;
  49     text/*|*/xml) highlight -O ansi "$path" && exit 5 || exit 2;;
  50     audio/*) mediainfo "$path" | sed 's/\s\{19\}//' && exit 5;;
  51     video/*) ffmpegthumbnailer -i "$path" -o "$cache" -s 0 && exit 6;;
  52     */x-dosexec) gnome-exe-thumbnailer "$path" "$cache" && exit 6;;
  53 esac
  54 
  55 exit 1