# HG changeset patch # User Christophe Lincoln # Date 1337089694 -7200 # Node ID 1db99ad848a85112c1bb353efb011724610d1a6a # Parent 581ddfa0d62071697a45c5324e4fe0d6688c2bd3 Add colorize() to libtaz.sh diff -r 581ddfa0d620 -r 1db99ad848a8 doc/libtaz.txt --- a/doc/libtaz.txt Sat May 12 21:08:08 2012 +0100 +++ b/doc/libtaz.txt Tue May 15 15:48:14 2012 +0200 @@ -12,14 +12,15 @@ /lib/libtaz.sh since it is used when /usr may not be mounted. FUNCTIONS - status Return command status [Done|Failed] - separator Display a line separator - newline Echo a new line if gettext or echo -n is used - boldify [string] Use a bold font for term, html or GTK output - indent Indent text in term mode - check_root Check if user is logged as root - confirm Read answer to confirm an action - log [string] Log activity, $activity must be set + status Return command status [Done|Failed] + separator Display a line separator + newline Echo a new line if gettext or echo -n is used + boldify [string] Use a bold font for term, html or GTK output + colorize [string] NB Colorize a string in term mode + indent Indent text in term mode + check_root Check if user is logged as root + confirm Read answer to confirm an action + log [string] Log activity, $activity must be set OPTIONS --output=[raw|gtk|html] diff -r 581ddfa0d620 -r 1db99ad848a8 rootfs/lib/libtaz.sh --- a/rootfs/lib/libtaz.sh Sat May 12 21:08:08 2012 +0100 +++ b/rootfs/lib/libtaz.sh Tue May 15 15:48:14 2012 +0200 @@ -39,8 +39,8 @@ done=" $okmsg" error=" $ermsg" ;; html) - done=" $okmsg" - error=" $ermsg" ;; + done=" $okmsg" + error=" $ermsg" ;; *) cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}') local scol=$(($cols - 10)) @@ -83,6 +83,19 @@ esac } +# Usage: colorize "Message" colorNB or use --color=NB option +# when running a tool. Default to white/38 and no html or gtk. +colorize() { + : ${color=$2} + case $output in + raw|gtk|html) echo "$1" ;; + *) + : ${color=38} + echo -e "\\033[1;${color}m${1}\\033[0;39m" ;; + esac + unset color +} + # Indent text $1 spaces indent() { local in="$1" diff -r 581ddfa0d620 -r 1db99ad848a8 testsuite.sh --- a/testsuite.sh Sat May 12 21:08:08 2012 +0100 +++ b/testsuite.sh Tue May 15 15:48:14 2012 +0200 @@ -13,6 +13,11 @@ echo -n "Checking libtaz.sh: boldify() " boldify "Message" + echo -n "Checking libtaz.sh: colorize() " + echo -n $(colorize "Message " 33) + echo -n $(colorize "Message " 35) + colorize "Message" 36 + echo "Checking libtaz.sh: separator" separator }