[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 mediainfo(){ # {{{
  16     command mediainfo "$@" |
  17     sed 's/^\s*$/ /; s/\s*:\s*/\t/' |
  18     column -ts $'\t'
  19 } # }}}
  20 highlight(){ # {{{
  21     command highlight -O ansi "$@"
  22 } # }}}
  23 
  24 file=$1
  25 width=$2
  26 cache=$4
  27 image=${5/False}
  28 mime=$(file -Lb --mime-type "$file")
  29 
  30 [[ $DISPLAY ]] || image=
  31 
  32 mkdir -p ${cache%/*}
  33 
  34 ################################################################################
  35 
  36 [[ $mime == */json      ]] && jq -C . "$file" && exit 4
  37 [[ $mime == */troff     ]] && man     "$file" && exit 4
  38 [[ $mime == */html      ]] && highlight -S html "$file" && exit 4
  39 [[ $mime == */xml       ]] && highlight -S xml  "$file" && exit 4
  40 [[ $mime == */*torrent  ]] && transmission-show "$file" && exit 4
  41 [[ $mime == */x-sqlite3 ]] && sqlite3 "$file" .schema | highlight -WS sql && exit 4
  42 [[ $file == *.kra       ]] && { # {{{
  43     [[ $image ]] &&
  44     unzip -p "$file" mergedimage.png > $cache && exit 6
  45     unzip -p "$file" documentinfo.xml | highlight -S xml && exit 4
  46 } # }}}
  47 [[ $file == *.blend     ]] && { # {{{
  48     [[ $image ]] &&
  49     blender-thumbnailer.py "$file" $cache && exit 6
  50 } # }}}
  51 [[ $mime == */svg+xml   ]] && { # {{{
  52     [[ $image ]] &&
  53     rsvg-convert "$file" > $cache && exit 6
  54     sed 500q "$file" | highlight -S svg && exit 4
  55 } # }}}
  56 [[ $mime == */pdf       ]] && { # {{{
  57     [[ $image ]] &&
  58     pdftoppm -singlefile -jpeg "$file" > $cache && exit 6
  59     pdftotext "$file" - | fmt -w $width && exit 4
  60 } # }}}
  61 [[ $mime == */heic      ]] && { # {{{
  62     [[ $image ]] &&
  63     convert "$file[0]" $cache && exit 6
  64     heif-info "$file" && exit 4
  65 } # }}}
  66 [[ $mime == video/*     ]] && { # {{{
  67     [[ $image ]] &&
  68     ffmpegthumbnailer -i "$file" -o $cache -s 0 && exit 6
  69     mediainfo "$file" && exit 4
  70 } # }}}
  71 [[ $mime == audio/*     ]] && { # {{{
  72     mediainfo "$file" && exit 4
  73 } # }}}
  74 [[ $mime == image/*     ]] && { # {{{
  75     # [[ $image ]] && o=$(identify -format '%[EXIF:Orientation]' "$file") &&
  76     # [[ $o && $o != 1 ]] && convert "$file" -auto-orient "$cache" && exit 6
  77 
  78     [[ $image ]] && exit 7
  79     LANG= exiv2 "$file" && exit 4
  80     mediainfo   "$file" && exit 4
  81 } # }}}
  82 [[ $mime == text/*      ]] && { # {{{
  83     highlight "$file" && exit 4
  84     exit 2
  85 } # }}}
  86 
  87 [[ $(file -Lbi "$file") == *binary ]] || exit 2
  88 
  89 # readelf -hd "$file" && exit 4
  90 # objdump -fp "$file" && exit 4
  91 
  92 exit 1