colorize.plugin.zsh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Easier alias to use the plugin
  2. alias ccat="colorize_cat"
  3. alias cless="colorize_less"
  4. # '$0:A' gets the absolute path of this file
  5. ZSH_COLORIZE_PLUGIN_PATH=$0:A
  6. colorize_check_requirements() {
  7. local -a available_tools
  8. available_tools=("chroma" "pygmentize")
  9. if [ -z "$ZSH_COLORIZE_TOOL" ]; then
  10. if (( $+commands[pygmentize] )); then
  11. ZSH_COLORIZE_TOOL="pygmentize"
  12. elif (( $+commands[chroma] )); then
  13. ZSH_COLORIZE_TOOL="chroma"
  14. else
  15. echo "Neither 'pygments' nor 'chroma' is installed!" >&2
  16. return 1
  17. fi
  18. fi
  19. if [[ ${available_tools[(Ie)$ZSH_COLORIZE_TOOL]} -eq 0 ]]; then
  20. echo "ZSH_COLORIZE_TOOL '$ZSH_COLORIZE_TOOL' not recognized. Available options are 'pygmentize' and 'chroma'." >&2
  21. return 1
  22. elif ! (( $+commands[$ZSH_COLORIZE_TOOL] )); then
  23. echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" >&2
  24. return 1
  25. fi
  26. }
  27. colorize_cat() {
  28. if ! colorize_check_requirements; then
  29. return 1
  30. fi
  31. # If the environment variable ZSH_COLORIZE_STYLE
  32. # is set, use that theme instead. Otherwise,
  33. # use the default.
  34. if [ -z "$ZSH_COLORIZE_STYLE" ]; then
  35. # Both pygmentize & chroma support 'emacs'
  36. ZSH_COLORIZE_STYLE="emacs"
  37. fi
  38. # Use stdin if stdin is not attached to a terminal.
  39. if [ ! -t 0 ]; then
  40. if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
  41. pygmentize -O style="$ZSH_COLORIZE_STYLE" -g
  42. else
  43. chroma --style="$ZSH_COLORIZE_STYLE" --formatter="${ZSH_COLORIZE_CHROMA_FORMATTER:-terminal}" "$@"
  44. fi
  45. return $?
  46. fi
  47. # Guess lexer from file extension, or guess it from file contents if unsuccessful.
  48. local FNAME lexer
  49. for FNAME in "$@"; do
  50. if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
  51. lexer=$(pygmentize -N "$FNAME")
  52. if [[ $lexer != text ]]; then
  53. pygmentize -O style="$ZSH_COLORIZE_STYLE" -l "$lexer" "$FNAME"
  54. else
  55. pygmentize -O style="$ZSH_COLORIZE_STYLE" -g "$FNAME"
  56. fi
  57. else
  58. chroma --style="$ZSH_COLORIZE_STYLE" --formatter="${ZSH_COLORIZE_CHROMA_FORMATTER:-terminal}" "$FNAME"
  59. fi
  60. done
  61. }
  62. # The less option 'F - Forward forever; like "tail -f".' will not work in this implementation
  63. # caused by the lack of the ability to follow the file within pygmentize.
  64. colorize_less() {
  65. if ! colorize_check_requirements; then
  66. return 1
  67. fi
  68. _cless() {
  69. # LESS="-R $LESS" enables raw ANSI colors, while maintain already set options.
  70. local LESS="-R $LESS"
  71. # This variable tells less to pipe every file through the specified command
  72. # (see the man page of less INPUT PREPROCESSOR).
  73. # 'zsh -ic "colorize_cat %s 2> /dev/null"' would not work for huge files like
  74. # the ~/.zsh_history. For such files the tty of the preprocessor will be suspended.
  75. # Therefore we must source this file to make colorize_cat available in the
  76. # preprocessor without the interactive mode.
  77. # `2>/dev/null` will suppress the error for large files 'broken pipe' of the python
  78. # script pygmentize, which will show up if less has not fully "loaded the file"
  79. # (e.g. when not scrolled to the bottom) while already the next file will be displayed.
  80. local LESSOPEN="| zsh -c 'source \"$ZSH_COLORIZE_PLUGIN_PATH\"; \
  81. ZSH_COLORIZE_TOOL=$ZSH_COLORIZE_TOOL ZSH_COLORIZE_STYLE=$ZSH_COLORIZE_STYLE \
  82. colorize_cat %s 2> /dev/null'"
  83. # LESSCLOSE will be set to prevent any errors by executing a user script
  84. # which assumes that his LESSOPEN has been executed.
  85. local LESSCLOSE=""
  86. LESS="$LESS" LESSOPEN="$LESSOPEN" LESSCLOSE="$LESSCLOSE" command less "$@"
  87. }
  88. if [ -t 0 ]; then
  89. _cless "$@"
  90. else
  91. # The input is not associated with a terminal, therefore colorize_cat will
  92. # colorize this input and pass it to less.
  93. # Less has now to decide what to use. If any files have been provided, less
  94. # will ignore the input by default, otherwise the colorized input will be used.
  95. # If files have been supplied and the input has been redirected, this will
  96. # lead to unnecessary overhead, but retains the ability to use the less options
  97. # without checking for them inside this script.
  98. colorize_cat | _cless "$@"
  99. fi
  100. }