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:
parent
d483b23eb1
commit
d2e68c5c8f
|
@ -57,15 +57,7 @@ export PASSWORD_STORE_GENERATED_LENGTH="43"
|
||||||
|
|
||||||
export LESS="--tabs=4 --RAW-CONTROL-CHARS --LONG-PROMPT"
|
export LESS="--tabs=4 --RAW-CONTROL-CHARS --LONG-PROMPT"
|
||||||
|
|
||||||
if command -v highlight > /dev/null; then
|
if command -v lesspipe > /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
|
|
||||||
LESSOPEN="| $(command -v lesspipe) %s"
|
LESSOPEN="| $(command -v lesspipe) %s"
|
||||||
elif command -v lesspipe.sh > /dev/null; then
|
elif command -v lesspipe.sh > /dev/null; then
|
||||||
LESSOPEN="| $(command -v lesspipe.sh) %s"
|
LESSOPEN="| $(command -v lesspipe.sh) %s"
|
||||||
|
|
|
@ -1,42 +1,54 @@
|
||||||
#!/usr/bin/env zsh
|
#!/usr/bin/env zsh
|
||||||
# Highlight cat output with either $CATOPEN or $LESSOPEN. The command needs to
|
# Highlight cat output with either $CATOPEN, ~/.lessfilter or $LESSOPEN. The
|
||||||
# have %s in it that will be replaced with all input files. Some programs that
|
# command needs to have %s in it that will be replaced with input files (one by
|
||||||
# are known to not work will be ignored in $LESSOPEN but not in $CATOPEN.
|
# one). Some programs that are known to not work will be ignored in $LESSOPEN
|
||||||
|
# but not in $CATOPEN.
|
||||||
|
|
||||||
# Example:
|
# Example:
|
||||||
# export CATOPEN="highlight --force --out-format=truecolor --stdout %s"
|
# export CATOPEN="highlight --force --out-format=truecolor --stdout %s"
|
||||||
# alias cat='cat-highlight'
|
# 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
|
# Check if we are in a terminal
|
||||||
if [[ -t 1 ]] && [[ -n "${CATOPEN}" || -n "${LESSOPEN}" ]]; then
|
if [[ ! -t 1 ]]; then
|
||||||
if [[ -z "${CATOPEN}" ]]; then
|
\cat ${@}
|
||||||
# Programs that won't work with cat
|
return ${?}
|
||||||
local -a known_bad=("lesspipe" "pygmentize" "source-highlight")
|
fi
|
||||||
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
|
# Try to find highlighter
|
||||||
local -a files
|
if [[ -v CATOPEN && -n ${CATOPEN} ]]; then
|
||||||
for arg in ${@}; do
|
local catopen=${CATOPEN}
|
||||||
# Filter out file names
|
elif [[ -x ~/.lessfilter ]]; then
|
||||||
if [[ "${arg[1]}" != "-" ]]; then
|
local catopen="~/.lessfilter %s"
|
||||||
files+=${(q)arg}
|
elif [[ -v LESSOPEN && -n ${LESSOPEN} ]]; then
|
||||||
else
|
local catopen="${LESSOPEN/|/}"
|
||||||
args+="${arg}"
|
# 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
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
eval "${catopen/\%s/${files}} | \cat ${args}"
|
|
||||||
else
|
|
||||||
\cat "${@}"
|
|
||||||
fi
|
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}
|
||||||
|
|
27
.lessfilter
27
.lessfilter
|
@ -4,14 +4,27 @@
|
||||||
|
|
||||||
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
|
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
|
||||||
|
|
||||||
command -v highlight > /dev/null
|
local file="${1}"
|
||||||
file="${1}"
|
local ret
|
||||||
|
local output
|
||||||
|
|
||||||
local output="$(highlight --force \
|
if type highlight >& -; then
|
||||||
--out-format=truecolor \
|
output="$(highlight --force \
|
||||||
--style=base16/unikitty-reversible \
|
--out-format=truecolor \
|
||||||
--stdout "${file}")"
|
--style=base16/unikitty-reversible \
|
||||||
ret=${?}
|
--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}
|
[[ ${ret} -eq 0 ]] || return ${ret}
|
||||||
|
|
||||||
# Try to catch when the output is just the magic string
|
# Try to catch when the output is just the magic string
|
||||||
|
|
Loading…
Reference in New Issue