slitaz-base-files rev 167

Add colorize() to libtaz.sh
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 15:48:14 2012 +0200 (2012-05-15)
parents 581ddfa0d620
children 3a6613021c40
files doc/libtaz.txt rootfs/lib/libtaz.sh testsuite.sh
line diff
     1.1 --- a/doc/libtaz.txt	Sat May 12 21:08:08 2012 +0100
     1.2 +++ b/doc/libtaz.txt	Tue May 15 15:48:14 2012 +0200
     1.3 @@ -12,14 +12,15 @@
     1.4  	/lib/libtaz.sh since it is used when /usr may not be mounted.
     1.5  
     1.6  FUNCTIONS
     1.7 -	status             Return command status [Done|Failed]
     1.8 -	separator          Display a line separator
     1.9 -	newline            Echo a new line if gettext or echo -n is used
    1.10 -	boldify [string]   Use a bold font for term, html or GTK output
    1.11 -	indent             Indent text in term mode
    1.12 -	check_root         Check if user is logged as root
    1.13 -	confirm            Read answer to confirm an action
    1.14 -	log [string]       Log activity, $activity must be set
    1.15 +	status                Return command status [Done|Failed]
    1.16 +	separator             Display a line separator
    1.17 +	newline               Echo a new line if gettext or echo -n is used
    1.18 +	boldify [string]      Use a bold font for term, html or GTK output
    1.19 +	colorize [string] NB  Colorize a string in term mode
    1.20 +	indent                Indent text in term mode
    1.21 +	check_root            Check if user is logged as root
    1.22 +	confirm               Read answer to confirm an action
    1.23 +	log [string]          Log activity, $activity must be set
    1.24  
    1.25  OPTIONS
    1.26  	--output=[raw|gtk|html]
     2.1 --- a/rootfs/lib/libtaz.sh	Sat May 12 21:08:08 2012 +0100
     2.2 +++ b/rootfs/lib/libtaz.sh	Tue May 15 15:48:14 2012 +0200
     2.3 @@ -39,8 +39,8 @@
     2.4  			done=" $okmsg"
     2.5  			error=" $ermsg" ;;
     2.6  		html)
     2.7 -			done=" <span class='done'>$okmsg</span>"
     2.8 -			error=" <span class='error'>$ermsg</span>" ;;
     2.9 +			done=" <span style='color: $okcolor;'>$okmsg</span>"
    2.10 +			error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
    2.11  		*)
    2.12  			cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
    2.13  			local scol=$(($cols - 10))
    2.14 @@ -83,6 +83,19 @@
    2.15  	esac
    2.16  }
    2.17  
    2.18 +# Usage: colorize "Message" colorNB or use --color=NB option
    2.19 +# when running a tool. Default to white/38 and no html or gtk.
    2.20 +colorize() {
    2.21 +	: ${color=$2}
    2.22 +	case $output in
    2.23 +		raw|gtk|html) echo "$1" ;;
    2.24 +		*)
    2.25 +			: ${color=38}
    2.26 +			echo -e "\\033[1;${color}m${1}\\033[0;39m" ;;
    2.27 +	esac
    2.28 +	unset color
    2.29 +}
    2.30 +
    2.31  # Indent text $1 spaces
    2.32  indent() {
    2.33  	local in="$1"
     3.1 --- a/testsuite.sh	Sat May 12 21:08:08 2012 +0100
     3.2 +++ b/testsuite.sh	Tue May 15 15:48:14 2012 +0200
     3.3 @@ -13,6 +13,11 @@
     3.4  	echo -n "Checking libtaz.sh: boldify() "
     3.5  	boldify "Message"
     3.6  
     3.7 +	echo -n "Checking libtaz.sh: colorize() "
     3.8 +	echo -n $(colorize "Message " 33)
     3.9 +	echo -n $(colorize "Message " 35)
    3.10 +	colorize "Message" 36
    3.11 +
    3.12  	echo "Checking libtaz.sh: separator"
    3.13  	separator
    3.14  }