slitaz-base-files rev 158

added generic yes/no function along with indent and statys function to libtaz
author Christian Mesh <meshca@clarkson.edu>
date Fri May 11 11:13:15 2012 -0500 (2012-05-11)
parents 6f52c9934cfc
children 71fb195c4f3e
files rootfs/lib/libtaz.sh
line diff
     1.1 --- a/rootfs/lib/libtaz.sh	Fri May 11 10:35:00 2012 -0500
     1.2 +++ b/rootfs/lib/libtaz.sh	Fri May 11 11:13:15 2012 -0500
     1.3 @@ -78,6 +78,13 @@
     1.4  	esac
     1.5  }
     1.6  
     1.7 +# Indent text $1 spaces
     1.8 +indent() { 
     1.9 +	local in="$1"
    1.10 +	shift
    1.11 +	echo -e "\033["$in"G $@"; 
    1.12 +}
    1.13 +
    1.14  # Check if user is logged as root.
    1.15  check_root() {
    1.16  	if [ $(id -u) != 0 ]; then
    1.17 @@ -85,3 +92,44 @@
    1.18  		exit 1
    1.19  	fi
    1.20  }
    1.21 +
    1.22 +yes_no() {
    1.23 +	[ "$autoyes" ] && true
    1.24 +	echo -n " ($(translate_query y)/$(translate_query N)) ? "
    1.25 +	read answer
    1.26 +	[ "$answer" == "$(translate_query y)" ] 
    1.27 +}
    1.28 +
    1.29 +translate_query() {
    1.30 +	case $1 in
    1.31 +		y) gettext "y" ;;
    1.32 +		Y) gettext "Y" ;;
    1.33 +		n) gettext "n" ;;
    1.34 +		N) gettext "N" ;;
    1.35 +		# Support other cases but keep them untranslated.
    1.36 +		*) echo "$1" ;;
    1.37 +	esac
    1.38 +}
    1.39 +
    1.40 +# Return command status. Default to colored console output.
    1.41 +status() {
    1.42 +	local check=$?
    1.43 +	case $output in
    1.44 +		raw|gtk) 
    1.45 +			done=" $okmsg" 
    1.46 +			error=" $ermsg" ;;
    1.47 +		html)
    1.48 +			done=" <span class='done'>$okmsg</span>" 
    1.49 +			error=" <span class='error'>$ermsg</span>" ;;
    1.50 +		*)
    1.51 +			cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
    1.52 +			local scol=$(($cols - 10))
    1.53 +			done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
    1.54 +			error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
    1.55 +	esac
    1.56 +	if [ $check = 0 ]; then
    1.57 +		echo -e "$done"
    1.58 +	else
    1.59 +		echo -e "$error"
    1.60 +	fi
    1.61 +}