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