From e053dd386c15343a07eeb01f257ab1279c92e949 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Wed, 4 Sep 2013 18:36:26 +0300 Subject: [PATCH] solved qcd -l problem. highbeam string argument rules --- highbeam | 60 ++++++++++++++++++++++++++++++++-------------------- qcd_function | 8 ++++--- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/highbeam b/highbeam index 2dcf59b..523dfbc 100755 --- a/highbeam +++ b/highbeam @@ -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 - -# 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 )) +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 -eval sed $REGEX || echo Maybe error in .highbeamrc >&2 + +[[ -z "$CONF_LINE" ]] && { + # user rules: + [[ -e "${CONF_FILE}" ]] && . "${CONF_FILE}" || { + echo You may have wrong format in "${CONF_FILE}" >&2 + usage >&2 + exit 1 + } + for (( r=0; r<${#RULES[@]}; r++ )); + do REGEX="$REGEX -e 's/\(${RULES[$r]}\)/${RULES[$(( $r + 1 ))]}\1${Z}/ig'" + r=$(( $r + 1 )) + done +} || { + # 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 diff --git a/qcd_function b/qcd_function index 2751c98..c962d33 100644 --- a/qcd_function +++ b/qcd_function @@ -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