slitaz-base-files rev 317

libtaz.sh: add die() and im(), rework confirm().
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Jan 08 11:09:57 2017 +0200 (2017-01-08)
parents 7054e5e75cec
children 8253327a882a
files doc/libtaz.txt rootfs/lib/libtaz.sh
line diff
     1.1 --- a/doc/libtaz.txt	Sat Nov 19 23:53:51 2016 +0200
     1.2 +++ b/doc/libtaz.txt	Sun Jan 08 11:09:57 2017 +0200
     1.3 @@ -25,6 +25,8 @@
     1.4  	emsg <string>         Output mark up messages
     1.5  	check_root            Check if user is logged as root
     1.6  	debug <string>        Display a DEBUG message when --debug is used
     1.7 +	die <string>          Report error and finish work
     1.8 +	im                    Returns true if you're in the interactive mode
     1.9  	confirm               Read answer to confirm an action
    1.10  	log <string>          Log activity, $activity must be set
    1.11  	optlist <lines>       Prints two-column list (of options, or functions, etc.)
    1.12 @@ -66,8 +68,34 @@
    1.13  
    1.14  	debug "A='$A'"
    1.15  
    1.16 -	echo -n "The question"; confirm
    1.17 -	confirm "The question (y/N)?"
    1.18 +	die 'Config %s absent. Exit' $config
    1.19 +
    1.20 +
    1.21 +	Using confirm()
    1.22 +	Use global options for auto-answering: '--yes' or '--noconfirm'.
    1.23 +	Use global option '--timeout' to define the wait time (default is 30 s).
    1.24 +	The letters "y", "Y", "n" and "N", used in the response, are localized.
    1.25 +	1. Outdated syntax. No options. Displays translated " [y/N] ? " and waits
    1.26 +	   for your input.
    1.27 +	   Answer formula is "[y/N]": to answer "yes" you need to enter "y" (or "Y"),
    1.28 +	   while to answer "no" you may enter "n" or "N" or any other letter, or
    1.29 +	   even just press Enter.
    1.30 +
    1.31 +	   echo -n "The question"; confirm && echo "Confirmed"
    1.32 +
    1.33 +	2. New syntax. Option is the string displayed. Answer formula the same.
    1.34 +
    1.35 +	   confirm "The question [y/N]?" && echo "Confirmed"
    1.36 +
    1.37 +	3. Modern syntax. Two options: the question and the preferred answer.
    1.38 +	   Displays the question and then translated answer formula as "[Y/n]" or
    1.39 +	   "[y/N]" depending on the preferred answer.
    1.40 +
    1.41 +	   confirm "The question?" y && echo "Confirmed"
    1.42 +
    1.43 +	In all cases, it returns "true" if you answer "yes", while returns "false"
    1.44 +	otherwise.
    1.45 +
    1.46  
    1.47  	activity='/var/log/my.log'
    1.48  	log "Message"
     2.1 --- a/rootfs/lib/libtaz.sh	Sat Nov 19 23:53:51 2016 +0200
     2.2 +++ b/rootfs/lib/libtaz.sh	Sun Jan 08 11:09:57 2017 +0200
     2.3 @@ -43,8 +43,8 @@
     2.4  
     2.5  
     2.6  # i18n functions
     2.7 -_()  { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
     2.8 -_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
     2.9 +_()  { local T="$1"; shift; printf "$(gettext "$T")" "$@"; echo; }
    2.10 +_n() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; }
    2.11  _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; }
    2.12  
    2.13  # Get terminal columns
    2.14 @@ -155,26 +155,37 @@
    2.15  	[ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
    2.16  }
    2.17  
    2.18 +# Report error and finish work
    2.19 +die() { longline "$(_ "$@")" >&2; exit 1; }
    2.20 +
    2.21 +# Interactive mode
    2.22 +im() { tty -s; }
    2.23 +
    2.24  # Confirmation
    2.25  confirm() {
    2.26 -	local answer=''
    2.27 +	local answer='' defanswer='n'
    2.28  	# Check auto-answer, if any
    2.29  	[ -n "$yes" ] && answer='y'
    2.30  	[ -n "$noconfirm" ] && answer='n'
    2.31  	# Print question
    2.32  	if [ -n "$1" ]; then
    2.33 -		echo -n "$1 "
    2.34 +		case "$2" in
    2.35 +			'')  echo -n "$1 ";;
    2.36 +			y|Y) echo -n "$1 [$(translate_query Y)/$(translate_query n)] "; defanswer='y';;
    2.37 +			*)   echo -n "$1 [$(translate_query y)/$(translate_query N)] ";;
    2.38 +		esac
    2.39  	else
    2.40 -		echo -n " ($(translate_query y)/$(translate_query N)) ? "
    2.41 +		echo -n " [$(translate_query y)/$(translate_query N)] ? "
    2.42  	fi
    2.43  	# Is it auto-answer?
    2.44  	if [ -z "$answer" ]; then
    2.45 -		read answer
    2.46 +		im && read -t ${timeout:-30} answer
    2.47 +		[ -z "$answer" ] && answer="$(translate_query "$defanswer")"
    2.48  	else
    2.49  		translate_query "$answer"; echo ' (auto)'
    2.50  	fi
    2.51  	# Return true/false to use in conditions
    2.52 -	[ "$answer" == "$(translate_query y)" ]
    2.53 +	[ "$answer" == "$(translate_query y)" -o "$answer" == "$(translate_query Y)" ]
    2.54  }
    2.55  
    2.56  # Log activities