2022-03-19 04:34:14 +01:00
|
|
|
#!/usr/bin/env zsh
|
2022-03-20 01:56:40 +01:00
|
|
|
# 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.
|
2022-03-19 03:54:51 +01:00
|
|
|
|
2022-03-20 01:56:40 +01:00
|
|
|
# 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
|
2022-03-20 00:26:38 +01:00
|
|
|
|
2022-03-19 04:34:14 +01:00
|
|
|
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
|