ebd13b60c107f424b40438404c18c3e8dc03433c

Author
Francisco de Zuviría <franciscodezuviria@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

colorize: add ability to colorize multiple files into less (#7662)

cless is an alias for a colorized less wrappper: colorize_via_pygmentize_less.
Note that cless opens many files as independent files, navigatable with `:n` and `:p`.

Diff

This diff is truncated to protect this page.

 1diff --git a/plugins/colorize/README.md b/plugins/colorize/README.md
 2index c006071f9046290bd110b97ad3a6a0496d259d64..71fa87861019eb74b696d0bd8614df2e749d8586 100644
 3--- a/plugins/colorize/README.md
 4+++ b/plugins/colorize/README.md
 5@@ -2,6 +2,10 @@
 6 
 7 With this plugin you can syntax-highlight file contents of over 300 supported languages and other text formats.
 8 
 9+Colorize will highlight the content based on the filename extension. If it can't find a syntax-highlighting
10+method for a given extension, it will try to find one by looking at the file contents. If no highlight method
11+is found it will just cat the file normally, without syntax highlighting.
12+
13 To use it, add colorize to the plugins array of your zshrc file:
14 ```
15 plugins=(... colorize)
16@@ -9,9 +13,16 @@ plugins=(... colorize)
17 
18 ## Usage
19 
20-* `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.
21+* `ccat  <file> [files]`: colorize the contents of the file (or files, if more than one are provided). 
22+  If no arguments are passed it will colorize the standard input or stdin.
23+
24+* `cless <file> [files]`: colorize the contents of the file (or files, if more than one are provided) and
25+  open less. If no arguments are passed it will colorize the standard input or stdin.
26 
27diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh
28index 8eede9a94d50a3b9601527b20e68e0c195a89fba..b8790f2608250ff9b7961c35cea1784e0039d901 100644
29--- a/plugins/colorize/colorize.plugin.zsh
30+++ b/plugins/colorize/colorize.plugin.zsh
31@@ -1,5 +1,6 @@
32 # easier alias to use the plugin
33 alias ccat='colorize_via_pygmentize'
34+alias cless='colorize_via_pygmentize_less'  
35 
36 colorize_via_pygmentize() {
37     if ! (( $+commands[pygmentize] )); then
38@@ -16,7 +17,7 @@ colorize_via_pygmentize() {
39     # guess lexer from file extension, or
40     # guess it from file contents if unsuccessful
41     local FNAME lexer
42-    for FNAME in $@
43+    for FNAME in "$@"
44     do
45         lexer=$(pygmentize -N "$FNAME")
46         if [[ $lexer != text ]]; then
47@@ -26,3 +27,23 @@ colorize_via_pygmentize() {
48         fi
49     done
50 }
51+
52+colorize_via_pygmentize_less() (   
53+    # this function is a subshell so tmp_files can be shared to cleanup function
54+    declare -a tmp_files 
55+    
56+    cleanup () {
57+        [[ ${#tmp_files} -gt 0 ]] && rm -f "${tmp_files[@]}"
58+        exit
59+    }
60+    trap 'cleanup' EXIT HUP TERM INT
61+    
62+    while (( $# != 0 )); do     #TODO: filter out less opts
63+        tmp_file="$(mktemp --tmpdir "tmp.colorize.XXXX.$(sed 's/\//./g' <<< "$1")")"
64+        tmp_files+=("$tmp_file")
65+        colorize_via_pygmentize "$1" > "$tmp_file"
66+        shift 1
67+    done
68+
69+    less -f "${tmp_files[@]}"
70+)