1
0
Fork 0

Make cat-highlight work with multiple files

This commit is contained in:
tastytea 2022-03-18 16:43:39 +01:00
parent 9f90c6c8ed
commit 1a1afe2419
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
1 changed files with 16 additions and 4 deletions

View File

@ -28,14 +28,26 @@ function export-emacs
done
}
# Highlight cat output with whatever less is using for highlighting
# Highlight cat output with whatever less is using for highlighting. The command
# in ${LESSOPEN} needs to be able to work with multiple files.
function cat-highlight()
{
# Check if we are in a terminal and $LESSOPEN is not empty
if [[ -t 1 && -n "${LESSOPEN}" ]]; then
# Works only if command understands - as stdin
CATOPEN="${LESSOPEN/\%s/-}"
eval "\cat $@ ${CATOPEN}"
local -a args
local -a files
for arg in $@; do
# Filter out file names
if [[ "${arg:0:1}" != "-" ]]; then
files+="$arg"
else
args+="$arg"
fi
done
local catopen="${LESSOPEN/| /}"
catopen="${catopen/\%s/${files}}"
eval "${catopen} | \cat ${args}"
else
\cat $@
fi