# HG changeset patch # User Aleksej Bobylev # Date 1483866597 -7200 # Node ID 5829aa52b9eeb488a68c4f68e65b9c5e7cfc971d # Parent 7054e5e75cec71e2c4cecd02bf0584064178e898 libtaz.sh: add die() and im(), rework confirm(). diff -r 7054e5e75cec -r 5829aa52b9ee doc/libtaz.txt --- a/doc/libtaz.txt Sat Nov 19 23:53:51 2016 +0200 +++ b/doc/libtaz.txt Sun Jan 08 11:09:57 2017 +0200 @@ -25,6 +25,8 @@ emsg Output mark up messages check_root Check if user is logged as root debug Display a DEBUG message when --debug is used + die Report error and finish work + im Returns true if you're in the interactive mode confirm Read answer to confirm an action log Log activity, $activity must be set optlist Prints two-column list (of options, or functions, etc.) @@ -66,8 +68,34 @@ debug "A='$A'" - echo -n "The question"; confirm - confirm "The question (y/N)?" + die 'Config %s absent. Exit' $config + + + Using confirm() + Use global options for auto-answering: '--yes' or '--noconfirm'. + Use global option '--timeout' to define the wait time (default is 30 s). + The letters "y", "Y", "n" and "N", used in the response, are localized. + 1. Outdated syntax. No options. Displays translated " [y/N] ? " and waits + for your input. + Answer formula is "[y/N]": to answer "yes" you need to enter "y" (or "Y"), + while to answer "no" you may enter "n" or "N" or any other letter, or + even just press Enter. + + echo -n "The question"; confirm && echo "Confirmed" + + 2. New syntax. Option is the string displayed. Answer formula the same. + + confirm "The question [y/N]?" && echo "Confirmed" + + 3. Modern syntax. Two options: the question and the preferred answer. + Displays the question and then translated answer formula as "[Y/n]" or + "[y/N]" depending on the preferred answer. + + confirm "The question?" y && echo "Confirmed" + + In all cases, it returns "true" if you answer "yes", while returns "false" + otherwise. + activity='/var/log/my.log' log "Message" diff -r 7054e5e75cec -r 5829aa52b9ee rootfs/lib/libtaz.sh --- a/rootfs/lib/libtaz.sh Sat Nov 19 23:53:51 2016 +0200 +++ b/rootfs/lib/libtaz.sh Sun Jan 08 11:09:57 2017 +0200 @@ -43,8 +43,8 @@ # i18n functions -_() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; } -_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; } +_() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; echo; } +_n() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; } _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; } # Get terminal columns @@ -155,26 +155,37 @@ [ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1" } +# Report error and finish work +die() { longline "$(_ "$@")" >&2; exit 1; } + +# Interactive mode +im() { tty -s; } + # Confirmation confirm() { - local answer='' + local answer='' defanswer='n' # Check auto-answer, if any [ -n "$yes" ] && answer='y' [ -n "$noconfirm" ] && answer='n' # Print question if [ -n "$1" ]; then - echo -n "$1 " + case "$2" in + '') echo -n "$1 ";; + y|Y) echo -n "$1 [$(translate_query Y)/$(translate_query n)] "; defanswer='y';; + *) echo -n "$1 [$(translate_query y)/$(translate_query N)] ";; + esac else - echo -n " ($(translate_query y)/$(translate_query N)) ? " + echo -n " [$(translate_query y)/$(translate_query N)] ? " fi # Is it auto-answer? if [ -z "$answer" ]; then - read answer + im && read -t ${timeout:-30} answer + [ -z "$answer" ] && answer="$(translate_query "$defanswer")" else translate_query "$answer"; echo ' (auto)' fi # Return true/false to use in conditions - [ "$answer" == "$(translate_query y)" ] + [ "$answer" == "$(translate_query y)" -o "$answer" == "$(translate_query Y)" ] } # Log activities