summaryrefslogtreecommitdiff
path: root/plugins/colorize
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/colorize')
-rw-r--r--plugins/colorize/README.md50
-rw-r--r--plugins/colorize/colorize.plugin.zsh116
2 files changed, 145 insertions, 21 deletions
diff --git a/plugins/colorize/README.md b/plugins/colorize/README.md
index c006071f9..405bb6d39 100644
--- a/plugins/colorize/README.md
+++ b/plugins/colorize/README.md
@@ -2,17 +2,55 @@
With this plugin you can syntax-highlight file contents of over 300 supported languages and other text formats.
-To use it, add colorize to the plugins array of your zshrc file:
+Colorize will highlight the content based on the filename extension. If it can't find a syntax-highlighting
+method for a given extension, it will try to find one by looking at the file contents. If no highlight method
+is found it will just cat the file normally, without syntax highlighting.
+
+## Setup
+
+To use it, add colorize to the plugins array of your `~/.zshrc` file:
```
plugins=(... colorize)
```
-## Usage
+## Configuration
+
+### Requirements
+
+This plugin requires that at least one of the following tools is installed:
+
+* [Chroma](https://github.com/alecthomas/chroma)
+* [Pygments](https://pygments.org/download/)
+
+### Colorize tool
+
+Colorize supports `pygmentize` and `chroma` as syntax highlighter. By default colorize uses `pygmentize` unless it's not installed and `chroma` is. This can be overridden by the `ZSH_COLORIZE_TOOL` environment variable:
+
+```
+ZSH_COLORIZE_TOOL=chroma
+```
-* `ccat <file> [files]`: colorize the contents of the file (or files, if more than one are provided). If no arguments are passed it will colorize the standard input or stdin.
+### Styles
-Colorize will highlight the content based on the filename extension. If it can't find a syntax-highlighting method for a given extension, it will try to find one by looking at the file contents. If no highlight method is found it will just cat the file normally, without syntax highlighting.
+Pygments offers multiple styles. By default, the `default` style is used, but you can choose another theme by setting the `ZSH_COLORIZE_STYLE` environment variable:
+
+```
+ZSH_COLORIZE_STYLE="colorful"
+```
+
+### Chroma Formatter Settings
+
+Chroma supports terminal output in 8 color, 256 color, and true-color. If you need to change the default terminal output style from the standard 8 color output, set the `ZSH_COLORIZE_CHROMA_FORMATTER` environment variable:
+
+```
+ZSH_COLORIZE_CHROMA_FORMATTER=terminal256
+```
+
+## Usage
-## Requirements
+* `ccat <file> [files]`: colorize the contents of the file (or files, if more than one are provided).
+ If no files are passed it will colorize the standard input.
-You have to install Pygments first: [pygments.org](http://pygments.org/download/)
+* `cless [less-options] <file> [files]`: colorize the contents of the file (or files, if more than one are provided) and open less.
+ If no files are passed it will colorize the standard input.
+ The LESSOPEN and LESSCLOSE will be overwritten for this to work, but only in a local scope.
diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh
index 8eede9a94..8ea98f80a 100644
--- a/plugins/colorize/colorize.plugin.zsh
+++ b/plugins/colorize/colorize.plugin.zsh
@@ -1,28 +1,114 @@
-# easier alias to use the plugin
-alias ccat='colorize_via_pygmentize'
+# Easier alias to use the plugin
+alias ccat="colorize_cat"
+alias cless="colorize_less"
-colorize_via_pygmentize() {
- if ! (( $+commands[pygmentize] )); then
- echo "package 'Pygments' is not installed!"
+# '$0:A' gets the absolute path of this file
+ZSH_COLORIZE_PLUGIN_PATH=$0:A
+
+colorize_check_requirements() {
+ local -a available_tools
+ available_tools=("chroma" "pygmentize")
+
+ if [ -z "$ZSH_COLORIZE_TOOL" ]; then
+ if (( $+commands[pygmentize] )); then
+ ZSH_COLORIZE_TOOL="pygmentize"
+ elif (( $+commands[chroma] )); then
+ ZSH_COLORIZE_TOOL="chroma"
+ else
+ echo "Neither 'pygments' nor 'chroma' is installed!" >&2
+ return 1
+ fi
+ fi
+
+ if [[ ${available_tools[(Ie)$ZSH_COLORIZE_TOOL]} -eq 0 ]]; then
+ echo "ZSH_COLORIZE_TOOL '$ZSH_COLORIZE_TOOL' not recognized. Available options are 'pygmentize' and 'chroma'." >&2
+ return 1
+ elif (( $+commands["$ZSH_COLORIZE_TOOL"] )); then
+ echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" >&2
return 1
fi
+}
+
+colorize_cat() {
+ if ! colorize_check_requirements; then
+ return 1
+ fi
+
+ # If the environment variable ZSH_COLORIZE_STYLE
+ # is set, use that theme instead. Otherwise,
+ # use the default.
+ if [ -z "$ZSH_COLORIZE_STYLE" ]; then
+ # Both pygmentize & chroma support 'emacs'
+ ZSH_COLORIZE_STYLE="emacs"
+ fi
- # pygmentize stdin if no arguments passed
+ # Use stdin if no arguments have been passed.
if [ $# -eq 0 ]; then
- pygmentize -g
+ if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
+ pygmentize -O style="$ZSH_COLORIZE_STYLE" -g
+ else
+ chroma --style="$ZSH_COLORIZE_STYLE" --formatter="${ZSH_COLORIZE_CHROMA_FORMATTER:-terminal}"
+ fi
return $?
fi
- # guess lexer from file extension, or
- # guess it from file contents if unsuccessful
+ # Guess lexer from file extension, or guess it from file contents if unsuccessful.
local FNAME lexer
- for FNAME in $@
- do
- lexer=$(pygmentize -N "$FNAME")
- if [[ $lexer != text ]]; then
- pygmentize -l "$lexer" "$FNAME"
+ for FNAME in "$@"; do
+ if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
+ lexer=$(pygmentize -N "$FNAME")
+ if [[ $lexer != text ]]; then
+ pygmentize -O style="$ZSH_COLORIZE_STYLE" -l "$lexer" "$FNAME"
+ else
+ pygmentize -O style="$ZSH_COLORIZE_STYLE" -g "$FNAME"
+ fi
else
- pygmentize -g "$FNAME"
+ chroma --style="$ZSH_COLORIZE_STYLE" --formatter="${ZSH_COLORIZE_CHROMA_FORMATTER:-terminal}" "$FNAME"
fi
done
}
+
+# The less option 'F - Forward forever; like "tail -f".' will not work in this implementation
+# caused by the lack of the ability to follow the file within pygmentize.
+colorize_less() {
+ if ! colorize_check_requirements; then
+ return 1
+ fi
+
+ _cless() {
+ # LESS="-R $LESS" enables raw ANSI colors, while maintain already set options.
+ local LESS="-R $LESS"
+
+ # This variable tells less to pipe every file through the specified command
+ # (see the man page of less INPUT PREPROCESSOR).
+ # 'zsh -ic "colorize_cat %s 2> /dev/null"' would not work for huge files like
+ # the ~/.zsh_history. For such files the tty of the preprocessor will be supended.
+ # Therefore we must source this file to make colorize_cat available in the
+ # preprocessor without the interactive mode.
+ # `2>/dev/null` will suppress the error for large files 'broken pipe' of the python
+ # script pygmentize, which will show up if less has not fully "loaded the file"
+ # (e.g. when not scrolled to the bottom) while already the next file will be displayed.
+ local LESSOPEN="| zsh -c 'source \"$ZSH_COLORIZE_PLUGIN_PATH\"; \
+ ZSH_COLORIZE_TOOL=$ZSH_COLORIZE_TOOL ZSH_COLORIZE_STYLE=$ZSH_COLORIZE_STYLE \
+ colorize_cat %s 2> /dev/null'"
+
+ # LESSCLOSE will be set to prevent any errors by executing a user script
+ # which assumes that his LESSOPEN has been executed.
+ local LESSCLOSE=""
+
+ LESS="$LESS" LESSOPEN="$LESSOPEN" LESSCLOSE="$LESSCLOSE" command less "$@"
+ }
+
+ if [ -t 0 ]; then
+ _cless "$@"
+ else
+ # The input is not associated with a terminal, therefore colorize_cat will
+ # colorize this input and pass it to less.
+ # Less has now to decide what to use. If any files have been provided, less
+ # will ignore the input by default, otherwise the colorized input will be used.
+ # If files have been supplied and the input has been redirected, this will
+ # lead to unnecessary overhead, but retains the ability to use the less options
+ # without checking for them inside this script.
+ colorize_cat | _cless "$@"
+ fi
+}