solved qcd -l problem. highbeam string argument rules

This commit is contained in:
ville rantanen
2013-09-04 18:36:26 +03:00
parent 8edde06849
commit e053dd386c
2 changed files with 42 additions and 26 deletions

View File

@@ -1,7 +1,7 @@
#!/bin/bash
function usage {
echo ' High Beam Highlighter
echo -e ' High Beam Highlighter
- a trial to create a very generic modifyable
syntax highlighter.
@@ -22,12 +22,12 @@ Background colors: $BGR $BGG $BGB $BGY $BGC $BGM $BGW
Usage: highbeam [-h] [-f config]
-h Help
-f Define config file (default ~/.highbeamrc)
-r Define rules with a string, replaces the other rules
-r '\''"land[^[:space:]]\+" "$G" "sky" "$B"'\''
'
}
[[ "$1" = "-h" ]] && usage && exit
E='\x1b['
H="${E}1m"
Z="${E}0m"
@@ -53,27 +53,41 @@ BGC="${E}46m"
BGW="${E}47m"
CONF_FILE=~/.highbeamrc
[[ "$1" = "-f" ]] && CONF_FILE=$2
CONF_LINE=""
while getopts hf:r: opt
do case "$opt" in
h)
usage
exit
;;
f)
CONF_FILE=$OPTARG
;;
r)
CONF_LINE=( $( eval echo $OPTARG ) )
;;
esac
done
[[ -z "$CONF_LINE" ]] && {
# 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
)
[[ -e "${CONF_FILE}" ]] && . "${CONF_FILE}" || {
echo You may have wrong format in "${CONF_FILE}" >&2
usage >&2
exit 1
}
# 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
} || {
# string fed rules
for (( r=0; r<${#CONF_LINE[@]}; r++ ));
do REGEX="$REGEX -e 's/\(${CONF_LINE[$r]}\)/${CONF_LINE[$(( $r + 1 ))]}\1${Z}/ig'"
r=$(( $r + 1 ))
done
}
eval sed $REGEX || echo Maybe errors in your rules: "$REGEX" >&2

View File

@@ -44,6 +44,7 @@ have qcd &&
_qcd()
{
local cur
local d
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$cur" == -* ]]; then
@@ -106,11 +107,12 @@ Keeps a history of folders visited in ~/.bash_cdhistory
esac
done
shift $(($OPTIND - 1))
if [ -z "$1" ]
then [[ $OPTIND -gt 1 ]] && return
\cd; return
fi
unset OPTSTRING
unset OPTIND
if [ -z "$1" ]
then \cd; return
fi
if [ "$1" = "-" ]
then \cd -; return
fi