dotfiles/.config/zsh/functions/cat-highlight

43 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env zsh
# Highlight cat output with either $CATOPEN or $LESSOPEN. The command needs to
# have %s in it that will be replaced with all input files. Some programs that
# are known to not work will be ignored in $LESSOPEN but not in $CATOPEN.
# Example:
# export CATOPEN="highlight --force --out-format=truecolor --stdout %s"
# alias cat='cat-highlight'
2022-04-03 07:38:07 +02:00
setopt LOCAL_OPTIONS ERR_RETURN PIPE_FAIL
# Check if we are in a terminal and a highlighter is defined
if [[ -t 1 ]] && [[ -n "${CATOPEN}" || -n "${LESSOPEN}" ]]; then
if [[ -z "${CATOPEN}" ]]; then
# Programs that won't work with cat
local -a known_bad=("lesspipe" "pygmentize" "source-highlight")
for baddy in ${known_bad}; do
if [[ "${LESSOPEN}" =~ "${baddy}" ]]; then
\cat "${@}"
return ${?}
fi
done
local catopen="${LESSOPEN/|/}"
else
local catopen="${CATOPEN}"
fi
local -a args
local -a files
for arg in ${@}; do
# Filter out file names
if [[ "${arg[1]}" != "-" ]]; then
2022-04-10 03:58:41 +02:00
files+=${(q)arg}
else
args+="${arg}"
fi
done
eval "${catopen/\%s/${files}} | \cat ${args}"
else
\cat "${@}"
fi