From 66e2284a08f86e5dcf661e3cf220483e1fb1f530 Mon Sep 17 00:00:00 2001 From: "Aaron N. Brock" Date: Fri, 15 Nov 2019 14:11:50 -0500 Subject: [PATCH 1/6] Add support for chroma --- plugins/colorize/colorize.plugin.zsh | 47 ++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh index 565ba5a36..051d2269c 100644 --- a/plugins/colorize/colorize.plugin.zsh +++ b/plugins/colorize/colorize.plugin.zsh @@ -3,21 +3,46 @@ alias ccat='colorize_via_pygmentize' alias cless='colorize_via_pygmentize_less' colorize_via_pygmentize() { - if ! (( $+commands[pygmentize] )); then - echo "package 'Pygments' is not installed!" + + if [[ $ZSH_COLORIZE_TOOL != "chroma" && $ZSH_COLORIZE_TOOL != "pygmentize" ]]; then + echo "ZSH_COLORIZE_TOOL not recognized. Options are 'pygmentize' or 'chroma'" return 1 fi - # If the environment varianle ZSH_COLORIZE_STYLE + if [ -z $ZSH_COLORIZE_TOOL ]; then + if (( $+commands[pygmentize] )); then + ZSH_COLORIZE_TOOL="pygmentize" + elif (( $+commands[chroma] )); then + ZSH_COLORIZE_TOOL="chroma" + else + echo "niether 'Pygments' nor 'chroma' is not installed!" + return 1 + fi + fi + + echo "Tool: $ZSH_COLORIZE_TOOL" + + # If the environment variable ZSH_COLORIZE_STYLE # is set, use that theme instead. Otherwise, # use the default. if [ -z $ZSH_COLORIZE_STYLE ]; then - ZSH_COLORIZE_STYLE="default" + if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then + ZSH_COLORIZE_STYLE="default" + else + # Choosing 'emacs' to match pygmentize's default as per: + # https://github.com/pygments/pygments/blob/master/pygments/styles/default.py#L19 + ZSH_COLORIZE_STYLE="emacs" + fi fi + echo "color style: $ZSH_COLORIZE_STYLE" # pygmentize stdin if no arguments passed if [ $# -eq 0 ]; then - pygmentize -O style="$ZSH_COLORIZE_STYLE" -g + if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then + pygmentize -O style="$ZSH_COLORIZE_STYLE" -g + else + chroma --style="$ZSH_COLORIZE_STYLE" + fi return $? fi @@ -27,11 +52,15 @@ colorize_via_pygmentize() { local FNAME lexer for FNAME in "$@" do - lexer=$(pygmentize -N "$FNAME") - if [[ $lexer != text ]]; then - pygmentize -O style="$ZSH_COLORIZE_STYLE" -l "$lexer" "$FNAME" + 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 -O style="$ZSH_COLORIZE_STYLE" -g "$FNAME" + chroma --style="$ZSH_COLORIZE_STYLE" "$FNAME" fi done } From 8aa070db0e1f8a5752f0e45834b7093b9b72860f Mon Sep 17 00:00:00 2001 From: "Aaron N. Brock" Date: Fri, 15 Nov 2019 15:34:24 -0500 Subject: [PATCH 2/6] Update README.md --- plugins/colorize/README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/colorize/README.md b/plugins/colorize/README.md index d1f878e62..b6e27fd27 100644 --- a/plugins/colorize/README.md +++ b/plugins/colorize/README.md @@ -10,12 +10,23 @@ To use it, add colorize to the plugins array of your zshrc file: ``` plugins=(... colorize) ``` +## Configuration -## Styles +### Colorize tool + +Colorize supports using either the `pygmentize` tool or the `chroma` tool. By default colorize uses `pygmentize` unless it's not installed & `chroma` is installed. However, you can override this with the `ZSH_COLORIZE_TOOL` environment variable: + +``` +ZSH_COLORIZE_TOOL=chroma +``` + +### Styles 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"` +``` +ZSH_COLORIZE_STYLE="colorful" +``` ## Usage @@ -32,4 +43,6 @@ In the latter form, the file contents will be concatenated and presented by less ## Requirements -You have to install Pygments first: [pygments.org](http://pygments.org/download/) +You have to either install Pygments: [pygments.org](http://pygments.org/download/) + +Or install chroma: [https://github.com/alecthomas/chroma](https://github.com/alecthomas/chroma) \ No newline at end of file From d08238fb0f5150bfd0be8201537a2a8ca6dbbdc9 Mon Sep 17 00:00:00 2001 From: Jakob Probst Date: Sat, 16 Nov 2019 13:10:02 +0100 Subject: [PATCH 3/6] Fix some comments and messages. Remove (probably) debug messages. Improve ZSH_COLORIZE_TOOL recognition. --- plugins/colorize/README.md | 4 ++-- plugins/colorize/colorize.plugin.zsh | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/plugins/colorize/README.md b/plugins/colorize/README.md index b6e27fd27..38d17dc76 100644 --- a/plugins/colorize/README.md +++ b/plugins/colorize/README.md @@ -14,7 +14,7 @@ plugins=(... colorize) ### Colorize tool -Colorize supports using either the `pygmentize` tool or the `chroma` tool. By default colorize uses `pygmentize` unless it's not installed & `chroma` is installed. However, you can override this with the `ZSH_COLORIZE_TOOL` environment variable: +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 @@ -30,7 +30,7 @@ ZSH_COLORIZE_STYLE="colorful" ## Usage -* `ccat [files]`: colorize the contents of the file (or files, if more than one are provided). +* `ccat [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. * `cless [files]`: colorize the contents of the file (or files, if more than one are provided) and diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh index 051d2269c..ac826e44b 100644 --- a/plugins/colorize/colorize.plugin.zsh +++ b/plugins/colorize/colorize.plugin.zsh @@ -3,11 +3,7 @@ alias ccat='colorize_via_pygmentize' alias cless='colorize_via_pygmentize_less' colorize_via_pygmentize() { - - if [[ $ZSH_COLORIZE_TOOL != "chroma" && $ZSH_COLORIZE_TOOL != "pygmentize" ]]; then - echo "ZSH_COLORIZE_TOOL not recognized. Options are 'pygmentize' or 'chroma'" - return 1 - fi + local available_tools=("chroma" "pygmentize") if [ -z $ZSH_COLORIZE_TOOL ]; then if (( $+commands[pygmentize] )); then @@ -15,12 +11,18 @@ colorize_via_pygmentize() { elif (( $+commands[chroma] )); then ZSH_COLORIZE_TOOL="chroma" else - echo "niether 'Pygments' nor 'chroma' is not installed!" + echo "Neither 'pygments' nor 'chroma' is installed!" return 1 fi fi - echo "Tool: $ZSH_COLORIZE_TOOL" + 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'." + return 1 + elif (( $+commands[$ZSH_COLORIZE_TOOL] )); then + echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" + return 1 + fi # If the environment variable ZSH_COLORIZE_STYLE # is set, use that theme instead. Otherwise, @@ -35,7 +37,6 @@ colorize_via_pygmentize() { fi fi - echo "color style: $ZSH_COLORIZE_STYLE" # pygmentize stdin if no arguments passed if [ $# -eq 0 ]; then if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then From b776f1d20f0537cefec9b58e41933dfd04334b62 Mon Sep 17 00:00:00 2001 From: "Aaron N. Brock" Date: Sun, 17 Nov 2019 14:03:14 -0500 Subject: [PATCH 4/6] Fix issue recognizing if tools are insalled --- plugins/colorize/colorize.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh index ac826e44b..2335113d5 100644 --- a/plugins/colorize/colorize.plugin.zsh +++ b/plugins/colorize/colorize.plugin.zsh @@ -19,7 +19,7 @@ colorize_via_pygmentize() { 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'." return 1 - elif (( $+commands[$ZSH_COLORIZE_TOOL] )); then + elif (( $+commands["$ZSH_COLORIZE_TOOL"] )); then echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" return 1 fi From c194b51560039707dc375412e70b6faa0d3ceaa5 Mon Sep 17 00:00:00 2001 From: "Aaron N. Brock" Date: Sun, 17 Nov 2019 14:10:03 -0500 Subject: [PATCH 5/6] Update default color to 'emacs' which both chroma & pygmentize support --- plugins/colorize/colorize.plugin.zsh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh index 2335113d5..8edf81d54 100644 --- a/plugins/colorize/colorize.plugin.zsh +++ b/plugins/colorize/colorize.plugin.zsh @@ -28,13 +28,8 @@ colorize_via_pygmentize() { # is set, use that theme instead. Otherwise, # use the default. if [ -z $ZSH_COLORIZE_STYLE ]; then - if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then - ZSH_COLORIZE_STYLE="default" - else - # Choosing 'emacs' to match pygmentize's default as per: - # https://github.com/pygments/pygments/blob/master/pygments/styles/default.py#L19 - ZSH_COLORIZE_STYLE="emacs" - fi + # Both pygmentize & chroma support 'emacs' + ZSH_COLORIZE_STYLE="emacs" fi # pygmentize stdin if no arguments passed From 5d5d202794ad62c51435798fdd464245f928e5db Mon Sep 17 00:00:00 2001 From: Jakob Probst Date: Sun, 17 Nov 2019 21:40:42 +0100 Subject: [PATCH 6/6] Echo to Error-Stream. Double quote to prevent globbing and word splitting. --- plugins/colorize/colorize.plugin.zsh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh index 8edf81d54..e250397be 100644 --- a/plugins/colorize/colorize.plugin.zsh +++ b/plugins/colorize/colorize.plugin.zsh @@ -5,36 +5,36 @@ alias cless='colorize_via_pygmentize_less' colorize_via_pygmentize() { local available_tools=("chroma" "pygmentize") - if [ -z $ZSH_COLORIZE_TOOL ]; then + 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!" + 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'." + 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!" + echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" >&2 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 + if [ -z "$ZSH_COLORIZE_STYLE" ]; then # Both pygmentize & chroma support 'emacs' ZSH_COLORIZE_STYLE="emacs" fi # pygmentize stdin if no arguments passed if [ $# -eq 0 ]; then - if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then + if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then pygmentize -O style="$ZSH_COLORIZE_STYLE" -g else chroma --style="$ZSH_COLORIZE_STYLE" @@ -48,7 +48,7 @@ colorize_via_pygmentize() { local FNAME lexer for FNAME in "$@" do - if [[ $ZSH_COLORIZE_TOOL == "pygmentize" ]]; then + if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then lexer=$(pygmentize -N "$FNAME") if [[ $lexer != text ]]; then pygmentize -O style="$ZSH_COLORIZE_STYLE" -l "$lexer" "$FNAME"