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

41 lines
1.1 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'
# 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")
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
files+="${arg}"
else
args+="${arg}"
fi
done
eval "${catopen/\%s/${files}} | \cat ${args}"
else
\cat "${@}"
fi