1
0
Fork 0

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
1 changed files with 22 additions and 13 deletions

View File

@ -1,17 +1,27 @@
#!/usr/bin/env zsh
# Highlight cat output with whatever less is using for highlighting. The command
# in $LESSOPEN needs to be able to work with multiple files.
# 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.
# Check if we are in a terminal and $LESSOPEN is not empty
if [[ -t 1 && -n "${LESSOPEN}" ]]; 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
# 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
@ -24,7 +34,6 @@ if [[ -t 1 && -n "${LESSOPEN}" ]]; then
fi
done
local catopen="${LESSOPEN/|/}"
eval "${catopen/\%s/${files}} | \cat ${args}"
else
\cat "${@}"