Want more features on Pastebin? Sign Up, it's FREE!

scope.sh

By: sarbash on Dec 10th, 2011  |  syntax: Python  |  size: 3.24 KB  |  views: 139  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. # ranger supports enhanced previews.  If the option "use_preview_script"
  3. # is set to True (by default it's False), this script will be called
  4. # and its 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.  
  20. # Meaningful aliases for arguments:
  21. path="$1"    # Full path of the selected file
  22. width="$2"   # Width of the preview pane (number of fitting characters)
  23. height="$3"  # Height of the preview pane (number of fitting characters)
  24.  
  25. maxln=200    # Stop after $maxln lines.  Can be used like ls | head -n $maxln
  26.  
  27. # Find out something about the file:
  28. mimetype=$(file --mime-type -Lbk "$path")
  29. extension=$(echo "$path" | grep '\.' | grep -o '[^.]\+$')
  30.  
  31. # Functions:
  32. # "have $1" succeeds if $1 is an existing command/installed program
  33. function have { type -P "$1" > /dev/null; }
  34. # "success" returns the exit code of the first program in the last pipe chain
  35. function success { test ${PIPESTATUS[0]} = 0 -o ${PIPESTATUS[0]} = 141; }
  36.  
  37. case "$extension" in
  38.         # Archive extensions:
  39.         7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
  40.         rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  41.                 als "$path" | head -n $maxln
  42.                 success && exit 0 || acat "$path" | head -n $maxln && exit 3
  43.                 exit 1;;
  44.         # PDF documents:
  45.         pdf)
  46.                 pdftotext -l 10 -nopgbrk -q "$path" - | head -n $maxln | fmt -s -w $width
  47.         success && exit 0 || exit 1;;
  48.         # BitTorrent Files
  49.         torrent)
  50.                 torri "$path" | head -n $maxln && exit 0
  51.                 exit 1;;
  52.         # HTML Pages:
  53.         htm|html|xhtml)
  54.                 have w3m    && w3m    -dump "$path" | head -n $maxln | fmt -s -w $width && exit 4
  55.                 have lynx   && lynx   -dump "$path" | head -n $maxln | fmt -s -w $width && exit 4
  56.                 have elinks && elinks -dump "$path" | head -n $maxln | fmt -s -w $width && exit 4
  57.                 ;; # fall back to highlight/cat if theres no lynx/elinks
  58. esac
  59.  
  60. case "$mimetype" in
  61.     *perl)
  62.                 highlight --out-format=ansi -S Perl "$path" | head -n $maxln
  63.         success && exit 5 || exit 2;;
  64.     *python)
  65.                 highlight --out-format=ansi -S Python "$path" | head -n $maxln
  66.         success && exit 5 || exit 2;;
  67.         # Syntax highlight for text files:
  68.         text/* | */xml)
  69.                 highlight --out-format=ansi "$path" | head -n $maxln
  70.         success && exit 5 || exit 2;;
  71.         # Ascii-previews of images:
  72.         image/*)
  73.                 img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
  74.         # Display information about media files:
  75.         video/* | audio/*)
  76.                 # Use sed to remove spaces so the output fits into the narrow window
  77.                 mediainfo "$path" | sed 's/  \+:/: /;'
  78.                 success && exit 5 || exit 1;;
  79. esac
  80.  
  81. exit 1
clone this paste RAW Paste Data