81 lines
2.3 KiB
Bash
Executable File
81 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
function usage {
|
|
echo ' High Beam Highlighter
|
|
- a trial to create a very generic modifyable
|
|
syntax highlighter.
|
|
|
|
Example rules: ( ~/.highbeamrc )
|
|
--------------------
|
|
RULES=(
|
|
"[0-9]" "$C" # Show numbers with Cyan color. NOTE: since the colors are represented with numbers, having this later in the rules will break coloring!
|
|
"$USER[ ]\+$USER" "$BGC$BLACK" # Use quotes to have rules with spaces. This colors the "ls -la" usernames. background cyan, black text
|
|
status "$G" # simple word matching, coloring green
|
|
"^d[^ ]*" "$H$G" # color words at the beginning of line, starting with d. color bright green
|
|
"q.\{0,3\}r" "$H$Y" # q*r with maximum 3 characters in between. color bright yellow
|
|
)
|
|
--------------------
|
|
Color variables: $R $G $B $Y $M $C $W $BLACK
|
|
Modify to bright version by prefixing with $H (e.g. $H$G)
|
|
Background colors: $BGR $BGG $BGB $BGY $BGC $BGM $BGW
|
|
|
|
Usage: highbeam [-h] [-f config]
|
|
-h Help
|
|
-f Define config file (default ~/.highbeamrc)
|
|
'
|
|
|
|
}
|
|
|
|
[[ "$1" = "-h" ]] && usage && exit
|
|
|
|
E='\x1b['
|
|
H="${E}1m"
|
|
Z="${E}0m"
|
|
|
|
# list of text colors:
|
|
BLACK="${E}30m"
|
|
R="${E}31m"
|
|
G="${E}32m"
|
|
B="${E}34m"
|
|
Y="${E}33m"
|
|
M="${E}35m"
|
|
C="${E}36m"
|
|
W="${E}37m"
|
|
|
|
|
|
# list of BG colors:
|
|
BGR="${E}41m"
|
|
BGG="${E}42m"
|
|
BGB="${E}44m"
|
|
BGY="${E}43m"
|
|
BGM="${E}45m"
|
|
BGC="${E}46m"
|
|
BGW="${E}47m"
|
|
|
|
CONF_FILE=~/.highbeamrc
|
|
[[ "$1" = "-f" ]] && CONF_FILE=$2
|
|
|
|
# user rules:
|
|
[[ -e "${CONF_FILE}" ]] && . "${CONF_FILE}" || echo You may have wrong construction in "${CONF_FILE}" >&2 || exit
|
|
[[ -e "${CONF_FILE}" ]] || {
|
|
# list of rules
|
|
RULES=(
|
|
"[0-9]" "$C" # Show numbers with Cyan color. NOTE: since the colors are represented with numbers, having this later in the rules will break coloring!
|
|
"$USER[ ]\+$USER" "$BGC$BLACK" # Use quotes to have rules with spaces. background cyan, black text
|
|
status "$G" # simple word matching, coloring green
|
|
"^d[^ ]*" "$H$G" # color words at the beginning of line, starting with d. color bright green
|
|
"q.\{0,3\}r" "$H$Y" # q*r with maximum 3 characters in between. color bright yellow
|
|
)
|
|
}
|
|
# actual code starts here
|
|
|
|
for (( r=0; r<${#RULES[@]}; r++ ));
|
|
do REGEX="$REGEX -e 's/\(${RULES[$r]}\)/${RULES[$(( $r + 1 ))]}\1${Z}/ig'"
|
|
r=$(( $r + 1 ))
|
|
done
|
|
eval sed $REGEX || echo Maybe error in .highbeamrc >&2
|
|
|
|
|
|
|
|
|