66e2284a08f86e5dcf661e3cf220483e1fb1f530

Author
Aaron N. Brock <aaron@functionize.com>
Committer
Aaron N. Brock <aaron@functionize.com>
Date

Message

Add support for chroma

Diff

 1diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh
 2index 565ba5a36e4525eb8fb91898f99ecfc2eea791fd..051d2269ce73e9e8494a72e26054db1bb3b12d3d 100644
 3--- a/plugins/colorize/colorize.plugin.zsh
 4+++ b/plugins/colorize/colorize.plugin.zsh
 5@@ -3,21 +3,46 @@ alias ccat='colorize_via_pygmentize'
 6 alias cless='colorize_via_pygmentize_less'
 7 
 8 colorize_via_pygmentize() {
 9-    if ! (( $+commands[pygmentize] )); then
10-        echo "package 'Pygments' is not installed!"
11+    
12+    if [[ $ZSH_COLORIZE_TOOL != "chroma" && $ZSH_COLORIZE_TOOL != "pygmentize" ]]; then
13+        echo "ZSH_COLORIZE_TOOL not recognized.  Options are 'pygmentize' or 'chroma'"
14         return 1
15     fi
16 
17-    # If the environment varianle ZSH_COLORIZE_STYLE
18+    if [ -z $ZSH_COLORIZE_TOOL ]; then
19+        if (( $+commands[pygmentize] )); then
20+            ZSH_COLORIZE_TOOL="pygmentize"
21+        elif (( $+commands[chroma] )); then
22+            ZSH_COLORIZE_TOOL="chroma"
23+        else
24+            echo "niether 'Pygments' nor 'chroma' is not installed!"
25+            return 1
26+        fi
27+    fi
28+
29+    echo "Tool: $ZSH_COLORIZE_TOOL"
30+
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-        ZSH_COLORIZE_STYLE="default"
36+        if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then
37+            ZSH_COLORIZE_STYLE="default"
38+        else
39+            # Choosing 'emacs' to match pygmentize's default as per:
40+            # https://github.com/pygments/pygments/blob/master/pygments/styles/default.py#L19
41+            ZSH_COLORIZE_STYLE="emacs"
42+        fi
43     fi
44 
45+    echo "color style: $ZSH_COLORIZE_STYLE"
46     # pygmentize stdin if no arguments passed
47     if [ $# -eq 0 ]; then
48-        pygmentize -O style="$ZSH_COLORIZE_STYLE" -g
49+        if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then
50+            pygmentize -O style="$ZSH_COLORIZE_STYLE" -g
51+        else
52+            chroma --style="$ZSH_COLORIZE_STYLE"
53+        fi
54         return $?
55     fi
56 
57@@ -27,11 +52,15 @@ colorize_via_pygmentize() {
58     local FNAME lexer
59     for FNAME in "$@"
60     do
61-        lexer=$(pygmentize -N "$FNAME")
62-        if [[ $lexer != text ]]; then
63-            pygmentize -O style="$ZSH_COLORIZE_STYLE" -l "$lexer" "$FNAME"
64+        if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then
65+            lexer=$(pygmentize -N "$FNAME")
66+            if [[ $lexer != text ]]; then
67+                pygmentize -O style="$ZSH_COLORIZE_STYLE" -l "$lexer" "$FNAME"
68+            else
69+                pygmentize -O style="$ZSH_COLORIZE_STYLE" -g "$FNAME"
70+            fi
71         else
72-            pygmentize -O style="$ZSH_COLORIZE_STYLE" -g "$FNAME"
73+            chroma --style="$ZSH_COLORIZE_STYLE" "$FNAME"
74         fi
75     done
76 }