cat-highlight: Add support for CATOPEN; add example

This commit is contained in:
tastytea 2022-03-20 01:56:40 +01:00
parent a7a404fa92
commit 4e1fdfa87e
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM

View File

@ -1,17 +1,27 @@
#!/usr/bin/env zsh #!/usr/bin/env zsh
# Highlight cat output with whatever less is using for highlighting. The command # Highlight cat output with either $CATOPEN or $LESSOPEN. The command needs to
# in $LESSOPEN needs to be able to work with multiple files. # 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.
# Check if we are in a terminal and $LESSOPEN is not empty # Example:
if [[ -t 1 && -n "${LESSOPEN}" ]]; then # export CATOPEN="highlight --force --out-format=truecolor --stdout %s"
# Programs that won't work with cat # alias cat='cat-highlight'
local -a known_bad=("lesspipe")
for baddy in ${known_bad}; do # Check if we are in a terminal and a highlighter is defined
if [[ "${LESSOPEN}" =~ "${baddy}" ]]; then if [[ -t 1 ]] && [[ -n "${CATOPEN}" || -n "${LESSOPEN}" ]]; then
\cat "${@}" if [[ -z "${CATOPEN}" ]]; then
return ${?} # Programs that won't work with cat
fi local -a known_bad=("lesspipe")
done 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 args
local -a files local -a files
@ -24,7 +34,6 @@ if [[ -t 1 && -n "${LESSOPEN}" ]]; then
fi fi
done done
local catopen="${LESSOPEN/|/}"
eval "${catopen/\%s/${files}} | \cat ${args}" eval "${catopen/\%s/${files}} | \cat ${args}"
else else
\cat "${@}" \cat "${@}"