[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 highlight(){
  28     command highlight -O ansi "$@"
  29 }
  30 
  31 ################################################################################
  32 
  33 case $path in
  34     */Mail/*) frm -Sq "$path" | fmt -w $width && exit 4;;
  35 esac
  36 
  37 ################################################################################
  38 
  39 [[ $image ]] &&
  40 case ${ext,,} in
  41     bmp) convert "$path" $cache && exit 6;;
  42     svg) convert "$path" $cache && exit 6;;
  43     ttf) convert "$path" $cache && exit 6;;
  44     otf) convert "$path" $cache && exit 6;;
  45 
  46     swf ) gnash-thumbnailer "$path"  $cache 512 && exit 6;;
  47     heic) heif-thumbnailer  "$path" "$cache"    && exit 6;;
  48 
  49     xcf  ) xcf2png  "$path" -o $cache && exit 6;;
  50     kra  ) unzip -p "$path" preview.png > $cache && exit 6;;
  51     blend) blender-thumbnailer.py "$path" $cache && exit 6;;
  52 
  53     pdf) pdftoppm -singlefile -jpeg "$path" > $cache && exit 6;;
  54 esac
  55 
  56 case ${ext,,} in
  57     1   ) man     "$path" && exit 4;;
  58     mp3 ) id3     "$path" && exit 4;;
  59     odt ) odt2txt "$path" && exit 4;;
  60     json) jq -C . "$path" && exit 4;;
  61 
  62     gpg) gpg -d "$path" && exit 4; exit 1;;
  63     asc) gpg -d "$path" && exit 4; exit 1;;
  64 
  65     heic) heif-info "$path" && exit 4;;
  66     mbox) frm -Sn "$path" | tac && exit 4;;
  67 
  68     cfg) highlight -S conf "$path" && exit 4; exit 2;;
  69     cnf) highlight -S conf "$path" && exit 4; exit 2;;
  70 
  71     svg ) highlight -S xml  "$path" && exit 4; exit 2;;
  72     dae ) highlight -S xml  "$path" && exit 4; exit 2;;
  73     opml) highlight -S xml  "$path" && exit 4; exit 2;;
  74 
  75     godot ) highlight -S ini "$path" && exit 4; exit 2;;
  76     import) highlight -S ini "$path" && exit 4; exit 2;;
  77     tres  ) highlight -S ini "$path" && exit 4; exit 2;;
  78     tscn  ) highlight -S ini "$path" && exit 4; exit 2;;
  79 
  80     torrent) transmission-show "$path" && exit 4;;
  81     db     ) sqlite3 "$path" .schema | highlight -WS sql && exit 4;;
  82 esac
  83 
  84 ################################################################################
  85 
  86 [[ $image ]] &&
  87 case $mime in
  88     image/*) exit 7;;
  89     video/*) ffmpegthumbnailer -i "$path" -o $cache -s 0 && exit 6;;
  90 esac
  91 
  92 case $mime in
  93     image/*)
  94         LANG= exiv2 "$path" && exit 4
  95         mediainfo   "$path" && exit 4
  96     ;;
  97 
  98     audio/*) mediainfo "$path" && exit 4;;
  99     video/*) mediainfo "$path" && exit 4;;
 100 
 101     text/*) highlight "$path" && exit 4; exit 2;;
 102     */xml ) highlight "$path" && exit 4; exit 2;;
 103 
 104     */x-object        ) readelf -hd "$path" && exit 4;;
 105     */x-pie-executable) readelf -hd "$path" && exit 4;;
 106 esac
 107 
 108 ################################################################################
 109 
 110 exit 1