My dotfiles for my Linux rice managed with stow and make
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scope.sh 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env sh
  2. # ranger supports enhanced previews. If the option "use_preview_script"
  3. # is set to True and this file exists, this script will be called and its
  4. # output is displayed in ranger. ANSI color codes are supported.
  5. # NOTES: This script is considered a configuration file. If you upgrade
  6. # ranger, it will be left untouched. (You must update it yourself.)
  7. # Also, ranger disables STDIN here, so interactive scripts won't work properly
  8. # Meanings of exit codes:
  9. # code | meaning | action of ranger
  10. # -----+------------+-------------------------------------------
  11. # 0 | success | success. display stdout as preview
  12. # 1 | no preview | failure. display no preview at all
  13. # 2 | plain text | display the plain content of the file
  14. # 3 | fix width | success. Don't reload when width changes
  15. # 4 | fix height | success. Don't reload when height changes
  16. # 5 | fix both | success. Don't ever reload
  17. # 6 | image | success. display the image $cached points to as an image preview
  18. # 7 | image | success. display the file directly as an image
  19. # Meaningful aliases for arguments:
  20. path="$1" # Full path of the selected file
  21. width="$2" # Width of the preview pane (number of fitting characters)
  22. height="$3" # Height of the preview pane (number of fitting characters)
  23. cached="$4" # Path that should be used to cache image previews
  24. preview_images="$5" # "True" if image previews are enabled, "False" otherwise.
  25. maxln=200 # Stop after $maxln lines. Can be used like ls | head -n $maxln
  26. # Find out something about the file:
  27. mimetype=$(file --mime-type -Lb "$path")
  28. extension=$(/bin/echo "${path##*.}" | awk '{print tolower($0)}')
  29. # Functions:
  30. # runs a command and saves its output into $output. Useful if you need
  31. # the return value AND want to use the output in a pipe
  32. try() { output=$(eval '"$@"'); }
  33. # writes the output of the previously used "try" command
  34. dump() { /bin/echo "$output"; }
  35. # a common post-processing function used after most commands
  36. trim() { head -n "$maxln"; }
  37. # wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
  38. safepipe() { "$@"; test $? = 0 -o $? = 141; }
  39. # Image previews, if enabled in ranger.
  40. if [ "$preview_images" = "True" ]; then
  41. case "$mimetype" in
  42. # Image previews for SVG files, disabled by default.
  43. ###image/svg+xml)
  44. ### convert "$path" "$cached" && exit 6 || exit 1;;
  45. # Image previews for image files. w3mimgdisplay will be called for all
  46. # image files (unless overriden as above), but might fail for
  47. # unsupported types.
  48. image/*)
  49. exit 7;;
  50. # Image preview for video, disabled by default.:
  51. ###video/*)
  52. ### ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;;
  53. esac
  54. fi
  55. case "$extension" in
  56. # Archive extensions:
  57. a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
  58. rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  59. try bsdtar -lf "$path" && { dump | trim; exit 0; }
  60. try als "$path" && { dump | trim; exit 0; }
  61. try acat "$path" && { dump | trim; exit 3; }
  62. exit 1;;
  63. rar)
  64. # avoid password prompt by providing empty password
  65. try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
  66. 7z)
  67. # avoid password prompt by providing empty password
  68. try 7z -p l "$path" && { dump | trim; exit 0; } || exit 1;;
  69. # PDF documents:
  70. pdf)
  71. try pdftotext -l 10 -nopgbrk -q "$path" - && \
  72. { dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
  73. # BitTorrent Files
  74. torrent)
  75. try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
  76. # ODT Files
  77. odt|ods|odp|sxw)
  78. try odt2txt "$path" && { dump | trim; exit 5; } || exit 1;;
  79. # HTML Pages:
  80. htm|html|xhtml)
  81. try w3m -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
  82. try lynx -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
  83. try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
  84. ;; # fall back to highlight/cat if the text browsers fail
  85. esac
  86. case "$mimetype" in
  87. # Syntax highlight for text files:
  88. text/* | */xml)
  89. if [ "$(tput colors)" -ge 256 ]; then
  90. pygmentize_format=terminal256
  91. highlight_format=xterm256
  92. else
  93. pygmentize_format=terminal
  94. highlight_format=ansi
  95. fi
  96. try safepipe pygmentize "$path" && { dump | trim; exit 5; }
  97. try safepipe highlight --out-format=${highlight_format} "$path" && { dump | trim; exit 5; }
  98. exit 2;;
  99. # Ascii-previews of images:
  100. image/*)
  101. img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
  102. # Display information about media files:
  103. video/* | audio/*)
  104. exiftool "$path" && exit 5
  105. # Use sed to remove spaces so the output fits into the narrow window
  106. try mediainfo "$path" && { dump | trim | sed 's/ \+:/: /;'; exit 5; } || exit 1;;
  107. esac
  108. exit 1