2022-03-19 04:34:14 +01:00
|
|
|
#!/usr/bin/env zsh
|
2022-04-24 23:53:52 +02:00
|
|
|
# Highlight cat output with either $CATOPEN, ~/.lessfilter or $LESSOPEN. The
|
|
|
|
# command needs to have %s in it that will be replaced with input files (one by
|
|
|
|
# one). 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-24 23:53:52 +02:00
|
|
|
setopt LOCAL_OPTIONS PIPE_FAIL
|
2022-04-03 05:47:50 +02:00
|
|
|
|
2022-04-24 23:53:52 +02:00
|
|
|
# Check if we are in a terminal
|
|
|
|
if [[ ! -t 1 ]]; then
|
|
|
|
\cat ${@}
|
|
|
|
return ${?}
|
|
|
|
fi
|
2022-03-20 00:26:38 +01:00
|
|
|
|
2022-04-24 23:53:52 +02:00
|
|
|
# Try to find highlighter
|
|
|
|
if [[ -v CATOPEN && -n ${CATOPEN} ]]; then
|
|
|
|
local catopen=${CATOPEN}
|
|
|
|
elif [[ -x ~/.lessfilter ]]; then
|
|
|
|
local catopen="~/.lessfilter %s"
|
|
|
|
elif [[ -v LESSOPEN && -n ${LESSOPEN} ]]; then
|
|
|
|
local catopen="${LESSOPEN/|/}"
|
|
|
|
# Programs that won't work with cat
|
|
|
|
local -a known_bad=("lesspipe")
|
|
|
|
for baddy in ${known_bad}; do
|
|
|
|
if [[ ${LESSOPEN} =~ ${baddy} ]]; then
|
|
|
|
unset catopen
|
2022-03-19 04:34:14 +01:00
|
|
|
fi
|
|
|
|
done
|
2022-04-24 23:53:52 +02:00
|
|
|
fi
|
2022-03-19 04:34:14 +01:00
|
|
|
|
2022-04-24 23:53:52 +02:00
|
|
|
if [[ ! -v catopen ]]; then
|
|
|
|
\cat ${@}
|
|
|
|
return ${?}
|
2022-03-19 04:34:14 +01:00
|
|
|
fi
|
2022-04-24 23:53:52 +02:00
|
|
|
|
|
|
|
local -a args
|
|
|
|
local -a files
|
|
|
|
for arg in ${@}; do
|
|
|
|
# Filter out file names
|
|
|
|
if [[ ${arg[1]} != "-" ]]; then
|
|
|
|
files+=${(q)arg}
|
|
|
|
else
|
|
|
|
args+=${arg}
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
for file in ${files}; do
|
|
|
|
eval "${catopen/\%s/${file}}" 2>& -
|
2022-04-25 00:16:38 +02:00
|
|
|
[[ ${?} -eq 0 ]] || \cat ${file}
|
2022-04-24 23:53:52 +02:00
|
|
|
done | \cat ${args}
|