compat: Use a bash function to colorify the messages
authorOzan Çağlayan <ozancag@gmail.com>
Tue, 17 Jul 2012 12:48:22 +0000 (15:48 +0300)
committerLuis R. Rodriguez <mcgrof@frijolero.org>
Wed, 18 Jul 2012 00:00:50 +0000 (17:00 -0700)
Declare a prettify() function that takes a color and a message
parameter instead of wrapping every message in the script with
ANSI codes.

Also add a nocolor mode which will be enabled with the -n flag.

Signed-off-by: Ozan Çağlayan <ozancag@gmail.com>
Signed-off-by: Luis R. Rodriguez <mcgrof@frijolero.org>
bin/ckmake

index 8e98c349da910f82cbde0c917c756908fd91c4bb..d408160700572ae825868ceb4d5b325a6043f0c1 100755 (executable)
 # found. This relies on distribution specific kernels, but can handle
 # your own custom list of target kernels. Log is setnt to LOG variable.
 
-# Pretty colors
-GREEN="\033[01;32m"
-YELLOW="\033[01;33m"
-NORMAL="\033[00m"
-BLUE="\033[34m"
-RED="\033[31m"
-PURPLE="\033[35m"
-CYAN="\033[36m"
-UNDERLINE="\033[02m"
 
 #export KCFLAGS="-Wno-unused-but-set-variable"
 KERNEL_DIR="/lib/modules"
@@ -28,9 +19,10 @@ LOG="ckmake.log"
 LOG_TMP="ckmake-tmp.log"
 REPORT="ckmake-report.log"
 TIME="0"
-QUIET=""
-DEBUG=""
+DEBUG="0"
+NOCOLOR="0"
 ARGS=""
+QUIET=""
 RET_FILE="ret-tmp.txt"
 
 # First and last kernels to use
@@ -47,6 +39,42 @@ if [[ -d "$HOME/compat-ksrc" ]]; then
        KSRC_PREFIX="$HOME/compat-ksrc"
 fi
 
+# Colorify output if NOCOLOR != 1 or -n is not given
+function prettify()
+{
+       if [[ $NOCOLOR == "1" ]]; then
+               echo -n "$2"
+       else
+               ANSI_CODE=
+               NORMAL="\033[00m"
+               case $1 in
+                       "green")
+                               ANSI_CODE="\033[01;32m"
+                               ;;
+                       "yellow")
+                               ANSI_CODE="\033[01;33m"
+                               ;;
+                       "blue")
+                               ANSI_CODE="\033[34m"
+                               ;;
+                       "red")
+                               ANSI_CODE="\033[31m"
+                               ;;
+                       "purple")
+                               ANSI_CODE="\033[35m"
+                               ;;
+                       "cyan")
+                               ANSI_CODE="\033[36m"
+                               ;;
+                       "underline")
+                               ANSI_CODE="\033[02m"
+                               ;;
+               esac
+
+               echo -e -n "${ANSI_CODE}$2${NORMAL}"
+       fi
+}
+
 function tee_color_split()
 {
        while read; do
@@ -57,9 +85,7 @@ function tee_color_split()
 
 function log_try_kernel()
 {
-       echo -en "Trying kernel ${BLUE}"
-       printf "%40s\t" "${1}"
-       echo -en "${NORMAL}"
+       printf "Trying kernel %40s\t" "$(prettify blue $1)"
 }
 
 function usage()
@@ -68,6 +94,7 @@ function usage()
        echo -e "-t   will run: 'time ckmake; time ckmake' account for"
        echo -e "     benchmark how long it takes to compile without ccache"
        echo -e "     and a run after cache kicks in"
+       echo -e "-n   Do not use colors in the output"
        echo -e "-q   will ask ckmake to run make with -s to only output errors"
        echo
        echo -e "<optional-target>  is the arguments you want to pass to the"
@@ -93,6 +120,10 @@ for i in $@ ; do
                        TIME="1"
                        shift
                        ;;
+               "-n")
+                       NOCOLOR="1"
+                       shift
+                       ;;
                "-s")
                        QUIET="-s"
                        shift
@@ -107,7 +138,7 @@ for i in $@ ; do
                                FIRST=$(echo $i | sed 's|\.\.|-|' | awk -F"-" '{print $1}')
                                LAST=$(echo $i | sed 's|\.\.|-|' | awk -F"-" '{print $2}')
                                RANGE="${FIRST}..${LAST}"
-                               echo -e "Going to use kernel ranges: ${BLUE}${FIRST}${NORMAL}..${BLUE}${LAST}${NORMAL}"
+                               echo -e "Going to use kernel ranges: $(prettify blue $FIRST)..$(prettify blue $LAST)"
                                shift
                        fi
 
@@ -127,10 +158,7 @@ function run_ckmake()
                        continue
                fi
 
-               # We cannot use tee_color_split() as bash read only spits
-               # out output when a newline comes in. We can modif IFS but
-               # I am still not sure how to do this properly.
-               log_try_kernel $KERNEL | perl -pe 's|(\e)\[(\d+)(;*)(\d*)(\w)||g' >> $LOG
+               NOCOLOR="1" log_try_kernel $KERNEL >> $LOG
                log_try_kernel $KERNEL
 
                #ionice -c 3 nice -n 20 make $QUIET KLIB=$DIR KLIB_BUILD=$DIR -j6 -Wunused-but-set-variable $ARGS &>> $LOG
@@ -142,9 +170,9 @@ function run_ckmake()
                fi
 
                if [[ $CUR_RET -eq 0 ]]; then
-                       echo -e "${GREEN}[OK]${NORMAL}" | tee_color_split $LOG
+                       echo -e "$(prettify green [OK])" | tee_color_split $LOG
                else
-                       echo -e "${RED}[FAILED]${NORMAL}" | tee_color_split $LOG
+                       echo -e "$(prettify red [FAILED])" | tee_color_split $LOG
                        RET=$CUR_RET
                fi
 
@@ -199,7 +227,7 @@ for i in $(find $KSRC_PREFIX/lib/modules/ -type d -name \*generic\* | sort -n -r
                fi
 
                if [[ ! -z $DEBUG ]]; then
-                       echo -e "${CYAN}${FIRST}${NORMAL} $(kernel_version $FIRST) <= ${GREEN}${KERNEL}${NORMAL} $(kernel_version $KERNEL) <= ${CYAN}${LAST}${NORMAL} $(kernel_version $LAST)"
+                       echo -e "$(prettify cyan $FIRST) $(kernel_version $FIRST) <= $(prettify green $KERNEL) $(kernel_version $KERNEL) <= $(prettify cyan $LAST) $(kernel_version $LAST)"
                fi
        fi