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'
|
|
|
|
|
2022-04-03 07:38:07 +02:00
|
|
|
setopt LOCAL_OPTIONS ERR_RETURN PIPE_FAIL
|
2022-04-03 05:47:50 +02:00
|
|
|
|
2022-03-20 01:56:40 +01:00
|
|
|
# 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
|
2022-03-22 01:00:14 +01:00
|
|
|
local -a known_bad=("lesspipe" "pygmentize" "source-highlight")
|
2022-03-20 01:56:40 +01:00
|
|
|
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
|
2022-04-10 03:25:50 +02:00
|
|
|
for arg in ${@}; do
|
2022-03-19 04:34:14 +01:00
|
|
|
# Filter out file names
|
|
|
|
if [[ "${arg[1]}" != "-" ]]; then
|
2022-04-10 03:58:41 +02:00
|
|
|
files+=${(q)arg}
|
2022-03-19 04:34:14 +01:00
|
|
|
else
|
|
|
|
args+="${arg}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
eval "${catopen/\%s/${files}} | \cat ${args}"
|
|
|
|
else
|
|
|
|
\cat "${@}"
|
|
|
|
fi
|