576ada138fc5eed3f58a4aff8141e483310c90fb

Author
Stephan Salzmann <kaffdaddy@insomniaonline.de>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

colorize: add README and refactor plugin (#7314)

Diff

This diff is truncated to protect this page.

 1diff --git a/plugins/colorize/README.md b/plugins/colorize/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..c006071f9046290bd110b97ad3a6a0496d259d64
 4--- /dev/null
 5+++ b/plugins/colorize/README.md
 6@@ -0,0 +1,18 @@
 7+# colorize
 8+
 9+With this plugin you can syntax-highlight file contents of over 300 supported languages and other text formats.
10+
11+To use it, add colorize to the plugins array of your zshrc file:
12+```
13+plugins=(... colorize)
14+```
15+
16+## Usage
17+
18+* `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.
19+
20diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh
21index e2af6d25ebaad55c991ed20fb371bb33be25a82a..8eede9a94d50a3b9601527b20e68e0c195a89fba 100644
22--- a/plugins/colorize/colorize.plugin.zsh
23+++ b/plugins/colorize/colorize.plugin.zsh
24@@ -1,9 +1,4 @@
25-# Plugin for highlighting file content
26-# Plugin highlights file content based on the filename extension.
27-# If no highlighting method supported for given extension then it tries 
28-# guess it by looking for file content.
29-
30-#easier alias to use plugin
31+# easier alias to use the plugin
32 alias ccat='colorize_via_pygmentize'
33 
34 colorize_via_pygmentize() {
35@@ -12,16 +7,20 @@ colorize_via_pygmentize() {
36         return 1
37     fi
38 
39+    # pygmentize stdin if no arguments passed
40     if [ $# -eq 0 ]; then
41-        pygmentize -g $@
42+        pygmentize -g
43+        return $?
44     fi
45 
46+    # guess lexer from file extension, or
47+    # guess it from file contents if unsuccessful
48+    local FNAME lexer
49     for FNAME in $@
50     do
51-        filename=$(basename "$FNAME")
52-        lexer=`pygmentize -N \"$filename\"`
53-        if [ "Z$lexer" != "Ztext" ]; then
54-            pygmentize -l $lexer "$FNAME"
55+        lexer=$(pygmentize -N "$FNAME")
56+        if [[ $lexer != text ]]; then
57+            pygmentize -l "$lexer" "$FNAME"
58         else
59             pygmentize -g "$FNAME"
60         fi