From 4e1fdfa87e0e96840eaeef6eafa7c0d272880e9d Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 20 Mar 2022 01:56:40 +0100 Subject: [PATCH] cat-highlight: Add support for CATOPEN; add example --- .config/zsh/functions/cat-highlight | 35 ++++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/.config/zsh/functions/cat-highlight b/.config/zsh/functions/cat-highlight index 6af42f1..f4ac568 100644 --- a/.config/zsh/functions/cat-highlight +++ b/.config/zsh/functions/cat-highlight @@ -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 "${@}"