From 1a1afe2419f94852a741d04cfc68bf6fde56d78f Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 18 Mar 2022 16:43:39 +0100 Subject: [PATCH] Make cat-highlight work with multiple files --- .config/zsh/functions.zsh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.config/zsh/functions.zsh b/.config/zsh/functions.zsh index b9b30ff..8e9e69e 100644 --- a/.config/zsh/functions.zsh +++ b/.config/zsh/functions.zsh @@ -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