Dotfiles for my tiling window manager + terminal workflow.
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.

89 lines
3.7 KiB

  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. # Meaningful aliases for arguments:
  19. path="$1" # Full path of the selected file
  20. width="$2" # Width of the preview pane (number of fitting characters)
  21. height="$3" # Height of the preview pane (number of fitting characters)
  22. cached="$4" # Path that should be used to cache image previews
  23. maxln=200 # Stop after $maxln lines. Can be used like ls | head -n $maxln
  24. # Find out something about the file:
  25. mimetype=$(file --mime-type -Lb "$path")
  26. extension=$(/bin/echo -E "${path##*.}" | tr "[:upper:]" "[:lower:]")
  27. # Functions:
  28. # runs a command and saves its output into $output. Useful if you need
  29. # the return value AND want to use the output in a pipe
  30. try() { output=$(eval '"$@"'); }
  31. # writes the output of the previously used "try" command
  32. dump() { /bin/echo -E "$output"; }
  33. # a common post-processing function used after most commands
  34. trim() { head -n "$maxln"; }
  35. # wraps highlight to treat exit code 141 (killed by SIGPIPE) as success
  36. highlight() { command highlight "$@"; test $? = 0 -o $? = 141; }
  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. rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  41. try als "$path" && { dump | trim; exit 0; }
  42. try acat "$path" && { dump | trim; exit 3; }
  43. try bsdtar -lf "$path" && { dump | trim; exit 0; }
  44. exit 1;;
  45. rar)
  46. try unrar -p- lt "$path" && { dump | trim; exit 0; } || exit 1;;
  47. # PDF documents:
  48. pdf)
  49. try pdftotext -l 10 -nopgbrk -q "$path" - && \
  50. { dump | trim | fmt -s -w $width; exit 0; } || exit 1;;
  51. # BitTorrent Files
  52. torrent)
  53. try transmission-show "$path" && { dump | trim; exit 5; } || exit 1;;
  54. # HTML Pages:
  55. htm|html|xhtml)
  56. try w3m -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
  57. try lynx -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
  58. try elinks -dump "$path" && { dump | trim | fmt -s -w $width; exit 4; }
  59. ;; # fall back to highlight/cat if the text browsers fail
  60. esac
  61. case "$mimetype" in
  62. # Syntax highlight for text files:
  63. text/* | */xml)
  64. try highlight --out-format=ansi "$path" && { dump | trim; exit 5; } || exit 2;;
  65. # Ascii-previews of images:
  66. image/*)
  67. img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;;
  68. # Image preview for videos, disabled by default:
  69. # video/*)
  70. # ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;;
  71. # Display information about media files:
  72. video/* | audio/*)
  73. exiftool "$path" && exit 5
  74. # Use sed to remove spaces so the output fits into the narrow window
  75. try mediainfo "$path" && { dump | trim | sed 's/ \+:/: /;'; exit 5; } || exit 1;;
  76. esac
  77. exit 1