mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-11-19 20:30:09 +00:00
colorize: update plugin to support less options (#8392)
This commit is contained in:
parent
e204c596ef
commit
e21fbe7dff
2 changed files with 64 additions and 37 deletions
|
@ -17,10 +17,10 @@ plugins=(... colorize)
|
||||||
|
|
||||||
### Requirements
|
### Requirements
|
||||||
|
|
||||||
This plugin requires that either of the following tools be installed:
|
This plugin requires that at least one of the following tools is installed:
|
||||||
|
|
||||||
* Chroma: [https://github.com/alecthomas/chroma](https://github.com/alecthomas/chroma)
|
* [Chroma](https://github.com/alecthomas/chroma)
|
||||||
* Pygments be installed: [pygments.org](https://pygments.org/)
|
* [Pygments](https://pygments.org/download/)
|
||||||
|
|
||||||
### Colorize tool
|
### Colorize tool
|
||||||
|
|
||||||
|
@ -41,12 +41,8 @@ ZSH_COLORIZE_STYLE="colorful"
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
* `ccat <file> [files]`: colorize the contents of the file (or files, if more than one are provided).
|
* `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.
|
If no files are passed it will colorize the standard input.
|
||||||
|
|
||||||
* `cless <file> [files]`: colorize the contents of the file (or files, if more than one are provided) and
|
* `cless [less-options] <file> [files]`: colorize the contents of the file (or files, if more than one are provided) and open less.
|
||||||
open less. If no arguments are passed it will colorize the standard input or stdin.
|
If no files are passed it will colorize the standard input.
|
||||||
|
The LESSOPEN and LESSCLOSE will be overwritten for this to work, but only in a local scope.
|
||||||
Note that `cless` will behave as less when provided more than one file: you have to navigate files with
|
|
||||||
the commands `:n` for next and `:p` for previous. The downside is that less options are not supported.
|
|
||||||
But you can circumvent this by either using the LESS environment variable, or by running `ccat file1 file2|less --opts`.
|
|
||||||
In the latter form, the file contents will be concatenated and presented by less as a single file.
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
# easier alias to use the plugin
|
# Easier alias to use the plugin
|
||||||
alias ccat='colorize_via_pygmentize'
|
alias ccat="colorize_cat"
|
||||||
alias cless='colorize_via_pygmentize_less'
|
alias cless="colorize_less"
|
||||||
|
|
||||||
colorize_via_pygmentize() {
|
# '$0:A' gets the absolute path of this file
|
||||||
|
ZSH_COLORIZE_PLUGIN_PATH=$0:A
|
||||||
|
|
||||||
|
colorize_check_requirements() {
|
||||||
local available_tools=("chroma" "pygmentize")
|
local available_tools=("chroma" "pygmentize")
|
||||||
|
|
||||||
if [ -z "$ZSH_COLORIZE_TOOL" ]; then
|
if [ -z "$ZSH_COLORIZE_TOOL" ]; then
|
||||||
|
@ -23,6 +26,12 @@ colorize_via_pygmentize() {
|
||||||
echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" >&2
|
echo "Package '$ZSH_COLORIZE_TOOL' is not installed!" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
colorize_cat() {
|
||||||
|
if ! colorize_check_requirements; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# If the environment variable ZSH_COLORIZE_STYLE
|
# If the environment variable ZSH_COLORIZE_STYLE
|
||||||
# is set, use that theme instead. Otherwise,
|
# is set, use that theme instead. Otherwise,
|
||||||
|
@ -32,7 +41,7 @@ colorize_via_pygmentize() {
|
||||||
ZSH_COLORIZE_STYLE="emacs"
|
ZSH_COLORIZE_STYLE="emacs"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# pygmentize stdin if no arguments passed
|
# Use stdin if no arguments have been passed.
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
|
if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
|
||||||
pygmentize -O style="$ZSH_COLORIZE_STYLE" -g
|
pygmentize -O style="$ZSH_COLORIZE_STYLE" -g
|
||||||
|
@ -42,12 +51,9 @@ colorize_via_pygmentize() {
|
||||||
return $?
|
return $?
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# guess lexer from file extension, or
|
# Guess lexer from file extension, or guess it from file contents if unsuccessful.
|
||||||
# guess it from file contents if unsuccessful
|
|
||||||
|
|
||||||
local FNAME lexer
|
local FNAME lexer
|
||||||
for FNAME in "$@"
|
for FNAME in "$@"; do
|
||||||
do
|
|
||||||
if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
|
if [[ "$ZSH_COLORIZE_TOOL" == "pygmentize" ]]; then
|
||||||
lexer=$(pygmentize -N "$FNAME")
|
lexer=$(pygmentize -N "$FNAME")
|
||||||
if [[ $lexer != text ]]; then
|
if [[ $lexer != text ]]; then
|
||||||
|
@ -61,22 +67,47 @@ colorize_via_pygmentize() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
colorize_via_pygmentize_less() (
|
# The less option 'F - Forward forever; like "tail -f".' will not work in this implementation
|
||||||
# this function is a subshell so tmp_files can be shared to cleanup function
|
# caused by the lack of the ability to follow the file within pygmentize.
|
||||||
declare -a tmp_files
|
colorize_less() {
|
||||||
|
if ! colorize_check_requirements; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
cleanup () {
|
_cless() {
|
||||||
[[ ${#tmp_files} -gt 0 ]] && rm -f "${tmp_files[@]}"
|
# LESS="-R $LESS" enables raw ANSI colors, while maintain already set options.
|
||||||
exit
|
local LESS="-R $LESS"
|
||||||
|
|
||||||
|
# This variable tells less to pipe every file through the specified command
|
||||||
|
# (see the man page of less INPUT PREPROCESSOR).
|
||||||
|
# 'zsh -ic "colorize_cat %s 2> /dev/null"' would not work for huge files like
|
||||||
|
# the ~/.zsh_history. For such files the tty of the preprocessor will be supended.
|
||||||
|
# Therefore we must source this file to make colorize_cat available in the
|
||||||
|
# preprocessor without the interactive mode.
|
||||||
|
# `2>/dev/null` will suppress the error for large files 'broken pipe' of the python
|
||||||
|
# script pygmentize, which will show up if less has not fully "loaded the file"
|
||||||
|
# (e.g. when not scrolled to the bottom) while already the next file will be displayed.
|
||||||
|
local LESSOPEN="| zsh -c 'source \"$ZSH_COLORIZE_PLUGIN_PATH\"; \
|
||||||
|
ZSH_COLORIZE_TOOL=$ZSH_COLORIZE_TOOL ZSH_COLORIZE_STYLE=$ZSH_COLORIZE_STYLE \
|
||||||
|
colorize_cat %s 2> /dev/null'"
|
||||||
|
|
||||||
|
# LESSCLOSE will be set to prevent any errors by executing a user script
|
||||||
|
# which assumes that his LESSOPEN has been executed.
|
||||||
|
local LESSCLOSE=""
|
||||||
|
|
||||||
|
LESS="$LESS" LESSOPEN="$LESSOPEN" LESSCLOSE="$LESSCLOSE" less "$@"
|
||||||
}
|
}
|
||||||
trap 'cleanup' EXIT HUP TERM INT
|
|
||||||
|
|
||||||
while (( $# != 0 )); do #TODO: filter out less opts
|
if [ -t 0 ]; then
|
||||||
tmp_file="$(mktemp -t "tmp.colorize.XXXX.$(sed 's/\//./g' <<< "$1")")"
|
_cless "$@"
|
||||||
tmp_files+=("$tmp_file")
|
else
|
||||||
colorize_via_pygmentize "$1" > "$tmp_file"
|
# The input is not associated with a terminal, therefore colorize_cat will
|
||||||
shift 1
|
# colorize this input and pass it to less.
|
||||||
done
|
# Less has now to decide what to use. If any files have been provided, less
|
||||||
|
# will ignore the input by default, otherwise the colorized input will be used.
|
||||||
less -f "${tmp_files[@]}"
|
# If files have been supplied and the input has been redirected, this will
|
||||||
)
|
# lead to unnecessary overhead, but retains the ability to use the less options
|
||||||
|
# without checking for them inside this script.
|
||||||
|
colorize_cat | _cless "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue