diff options
author | Francisco de ZuvirĂa <franciscodezuviria@gmail.com> | 2019-04-22 16:06:47 -0300 |
---|---|---|
committer | Marc CornellĂ <marc.cornella@live.com> | 2019-04-22 21:06:47 +0200 |
commit | ebd13b60c107f424b40438404c18c3e8dc03433c (patch) | |
tree | 251e1d901772e20fb905fd0f93e567446ea83853 /plugins/colorize/colorize.plugin.zsh | |
parent | 9b2410fbcfa21d6115219fe626a6f422b578d3ac (diff) | |
download | zsh-ebd13b60c107f424b40438404c18c3e8dc03433c.tar.gz zsh-ebd13b60c107f424b40438404c18c3e8dc03433c.tar.bz2 zsh-ebd13b60c107f424b40438404c18c3e8dc03433c.zip |
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`.
Diffstat (limited to 'plugins/colorize/colorize.plugin.zsh')
-rw-r--r-- | plugins/colorize/colorize.plugin.zsh | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/plugins/colorize/colorize.plugin.zsh b/plugins/colorize/colorize.plugin.zsh index 8eede9a94..b8790f260 100644 --- a/plugins/colorize/colorize.plugin.zsh +++ b/plugins/colorize/colorize.plugin.zsh @@ -1,5 +1,6 @@ # easier alias to use the plugin alias ccat='colorize_via_pygmentize' +alias cless='colorize_via_pygmentize_less' colorize_via_pygmentize() { if ! (( $+commands[pygmentize] )); then @@ -16,7 +17,7 @@ colorize_via_pygmentize() { # guess lexer from file extension, or # guess it from file contents if unsuccessful local FNAME lexer - for FNAME in $@ + for FNAME in "$@" do lexer=$(pygmentize -N "$FNAME") if [[ $lexer != text ]]; then @@ -26,3 +27,23 @@ colorize_via_pygmentize() { fi done } + +colorize_via_pygmentize_less() ( + # this function is a subshell so tmp_files can be shared to cleanup function + declare -a tmp_files + + cleanup () { + [[ ${#tmp_files} -gt 0 ]] && rm -f "${tmp_files[@]}" + exit + } + trap 'cleanup' EXIT HUP TERM INT + + while (( $# != 0 )); do #TODO: filter out less opts + tmp_file="$(mktemp --tmpdir "tmp.colorize.XXXX.$(sed 's/\//./g' <<< "$1")")" + tmp_files+=("$tmp_file") + colorize_via_pygmentize "$1" > "$tmp_file" + shift 1 + done + + less -f "${tmp_files[@]}" +) |