1
0
Fork 0

Reorganise highlighter selection in less and cat

- LESSOPEN contains lesspipe{,sh} only
- ~/.lessfilter tries more highlighters
- cat-highlight processes each file independently
- cat-highlight tries to use ~/.lessfilter
This commit is contained in:
tastytea 2022-04-24 23:53:52 +02:00
parent d483b23eb1
commit d2e68c5c8f
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
3 changed files with 64 additions and 47 deletions

View File

@ -57,15 +57,7 @@ export PASSWORD_STORE_GENERATED_LENGTH="43"
export LESS="--tabs=4 --RAW-CONTROL-CHARS --LONG-PROMPT"
if command -v highlight > /dev/null; then
# ~/.lessfilter handles the highlighting
LESSOPEN="| $(command -v lesspipe) %s"
export CATOPEN="highlight --force --out-format=truecolor --style=base16/unikitty-reversible --stdout %s"
elif command -v pygmentize > /dev/null; then
LESSOPEN="| $(command -v pygmentize) -f terminal16m -g -O style=paraiso-dark %s"
elif command -v source-highlight > /dev/null; then
LESSOPEN="| $(command -v source-highlight) --failsafe --infer-lang --out-format=esc --style-file=esc.style --input=%s"
elif command -v lesspipe > /dev/null; then
if command -v lesspipe > /dev/null; then
LESSOPEN="| $(command -v lesspipe) %s"
elif command -v lesspipe.sh > /dev/null; then
LESSOPEN="| $(command -v lesspipe.sh) %s"

View File

@ -1,42 +1,54 @@
#!/usr/bin/env zsh
# 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.
# Highlight cat output with either $CATOPEN, ~/.lessfilter or $LESSOPEN. The
# command needs to have %s in it that will be replaced with input files (one by
# one). Some programs that are known to not work will be ignored in $LESSOPEN
# but not in $CATOPEN.
# Example:
# export CATOPEN="highlight --force --out-format=truecolor --stdout %s"
# alias cat='cat-highlight'
setopt LOCAL_OPTIONS ERR_RETURN PIPE_FAIL
setopt LOCAL_OPTIONS PIPE_FAIL
# 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" "pygmentize" "source-highlight")
for baddy in ${known_bad}; do
if [[ "${LESSOPEN}" =~ "${baddy}" ]]; then
\cat "${@}"
return ${?}
fi
done
local catopen="${LESSOPEN/|/}"
else
local catopen="${CATOPEN}"
fi
# Check if we are in a terminal
if [[ ! -t 1 ]]; then
\cat ${@}
return ${?}
fi
local -a args
local -a files
for arg in ${@}; do
# Filter out file names
if [[ "${arg[1]}" != "-" ]]; then
files+=${(q)arg}
else
args+="${arg}"
# Try to find highlighter
if [[ -v CATOPEN && -n ${CATOPEN} ]]; then
local catopen=${CATOPEN}
elif [[ -x ~/.lessfilter ]]; then
local catopen="~/.lessfilter %s"
elif [[ -v LESSOPEN && -n ${LESSOPEN} ]]; then
local catopen="${LESSOPEN/|/}"
# Programs that won't work with cat
local -a known_bad=("lesspipe")
for baddy in ${known_bad}; do
if [[ ${LESSOPEN} =~ ${baddy} ]]; then
unset catopen
fi
done
eval "${catopen/\%s/${files}} | \cat ${args}"
else
\cat "${@}"
fi
if [[ ! -v catopen ]]; then
\cat ${@}
return ${?}
fi
local -a args
local -a files
for arg in ${@}; do
# Filter out file names
if [[ ${arg[1]} != "-" ]]; then
files+=${(q)arg}
else
args+=${arg}
fi
done
for file in ${files}; do
eval "${catopen/\%s/${file}}" 2>& -
[[ ${?} -ne 0 ]] && \cat ${file}
done | \cat ${args}

View File

@ -4,14 +4,27 @@
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
command -v highlight > /dev/null
file="${1}"
local file="${1}"
local ret
local output
local output="$(highlight --force \
--out-format=truecolor \
--style=base16/unikitty-reversible \
--stdout "${file}")"
ret=${?}
if type highlight >& -; then
output="$(highlight --force \
--out-format=truecolor \
--style=base16/unikitty-reversible \
--stdout "${file}")"
ret=${?}
elif type pygmentize >& -; then
output="$(pygmentize -f terminal16m -g -O style=paraiso-dark ${file})"
ret=${?}
elif type source-highlight >& -; then
output="$(source-highlight --failsafe \
--infer-lang \
--out-format=esc \
--style-file=esc.style \
--input=${file})"
ret=${?}
fi
[[ ${ret} -eq 0 ]] || return ${ret}
# Try to catch when the output is just the magic string