# HG changeset patch # User Aleksej Bobylev # Date 1449111599 -7200 # Node ID cca198d5952c2c62d14f8afaa84803b8fea79491 # Parent 973c3b2ecc5a4352129c1067334f0788ca486aa7 libtaz: implement options --quiet and --cols= diff -r 973c3b2ecc5a -r cca198d5952c doc/libtaz.txt --- a/doc/libtaz.txt Mon Nov 30 02:33:22 2015 +0200 +++ b/doc/libtaz.txt Thu Dec 03 04:59:59 2015 +0200 @@ -38,6 +38,8 @@ OPTIONS --output=[raw|gtk|html] --activity=/path/files/activity + --quiet Display nothing but errors (usually: action ... status) + --cols= Output width in columns EXAMPLES diff -r 973c3b2ecc5a -r cca198d5952c rootfs/lib/libtaz.sh --- a/rootfs/lib/libtaz.sh Mon Nov 30 02:33:22 2015 +0200 +++ b/rootfs/lib/libtaz.sh Thu Dec 03 04:59:59 2015 +0200 @@ -48,44 +48,43 @@ _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; } # Get terminal columns -get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; } +get_cols() { stty size 2>/dev/null | awk -vc=$cols 'END{print c?c:($2 && $2<80)?$2:80}'; } # Last command status status() { - local check=$? + local ret_code=$? + [ -n "$quiet" -a "$ret_code" -eq 0 ] && return + [ -n "$quiet" ] && action "$saved_action" no-quiet + + case $ret_code in + 0) local msg="$okmsg" color="$okcolor";; + *) local msg="$ermsg" color="$ercolor";; + esac case $output in - raw|gtk) - done=" $okmsg" - error=" $ermsg";; - html) - done=" $okmsg" - error=" $ermsg";; - *) - done="[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]" - error="[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";; - esac - case $check in - 0) echo -e "$done";; - *) echo -e "$error";; + raw|gtk) echo " $msg";; + html) echo " $msg";; + *) echo -e "[ \\033[1;${color}m$msg\\033[0;39m ]";; esac } # Line separator separator() { + [ -n "$quiet" ] && return case $output in gtk) echo '--------';; - html) echo -n '
';; - *) - local cols=$(get_cols) - printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";; + html) echo -n '
';; + *) printf "%$(get_cols)s\n" | tr ' ' "${1:-=}";; esac } # New line -newline() { echo; } +newline() { + [ -z "$quiet" ] && echo +} # Display a bold message boldify() { + [ -n "$quiet" ] && return case $output in raw) echo "$@" ;; gtk) echo "$@" ;; @@ -96,6 +95,7 @@ # Colorize message colorize() { + [ -n "$quiet" ] && return : ${color=$1} shift case $output in @@ -111,6 +111,7 @@ # Indent text indent() { + [ -n "$quiet" ] && return local in="$1" shift echo -e "\033["$in"G $@"; @@ -118,7 +119,8 @@ # Extended MeSsaGe output emsg() { - local sep="\n--------\n" + [ -n "$quiet" ] && return + local sep="\n$(separator)\n" case $output in raw) echo "$@" | sed -e 's|||g; s|||g; s|||g; \ @@ -131,12 +133,6 @@ s|||g; s|||g; \ s||
|g; s|<->|
|g; s|| |g' ;; *) - local sep="\n" - local cols=$(get_cols) - [ "$cols" ] || cols=80 - for c in $(seq 1 $cols); do - sep="${sep}=" - done echo -en "$(echo "$@" | sed -e 's||\\033[1m|g; s||\\033[0m|g; s||\\033[\1m|g; s||\\033[1;\1m|g; s||\\033[0;39m|g; s||\n|g; @@ -148,7 +144,7 @@ # Check if user is logged as root check_root() { - if [ $(id -u) != 0 ]; then + if [ $(id -u) -ne 0 ]; then lgettext "You must be root to execute:"; echo " $(basename $0) $@" exit 1 fi @@ -181,34 +177,36 @@ # Print two-column list of options with descriptions optlist() { - local in cols col1=1 line - in="$(echo "$1" | sed 's| *| |g')" - cols=$(get_cols); [ "$cols" ] || cols=80 + [ -n "$quiet" ] && return + local in="$(echo "$1" | sed 's| *| |g')" w=$(get_cols) col1=1 line IFS=$'\n' for line in $in; do col=$(echo -n "$line" | cut -f1 | wc -m) [ $col -gt $col1 ] && col1=$col done - echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \ + echo "$in" | sed 's|\t|&\n|' | fold -sw$((w - col1 - 4)) | \ sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}" } # Wrap words in long terminal message longline() { - cols=$(get_cols) - echo -e "$@" | fold -sw${cols:-80} + [ -n "$quiet" ] && return + local w=$(get_cols) + echo -e "$@" | fold -sw$w } # Print localized title title() { + [ -n "$quiet" ] && return case $output in html) echo "
$(_ "$@")
";;
-		*)    newline; boldify "$(_ "$@")"; separator;;
+		*) newline; boldify "$(_ "$@")"; separator;;
 	esac
 }
 
 # Print footer
 footer() {
+	[ -n "$quiet" ] && return
 	case $output in
 		html) echo "
";; *) separator; echo "$1"; [ -n "$1" ] && newline;; @@ -216,13 +214,17 @@ } # Print current action +saved_action='' action() { - local w cols scol msg chars padding + saved_action="$1" + [ -n "$quiet" -a -z "$2" ] && return + local w c scol msg chars w=$(_ 'w'); w=${w/w/10} - cols=$(get_cols); cols=${cols:-80}; scol=$(( $cols - $w )) + c=$(get_cols) + scol=$(( $c - $w )) msg="$(_n "$@" | fold -sw$scol)" - chars=$(echo -n "$msg" | tail -n1 | wc -m); padding=$(( $scol - $chars )) - msg="$(printf '%s%'$padding's' "$msg" "")" + chars=$(echo -n "$msg" | tail -n1 | wc -m) + msg="$(printf '%s%'$(( $scol - $chars ))'s' "$msg" '')" case $output in raw|gtk|html) echo -n "$msg";; @@ -232,16 +234,16 @@ # Print long line as list item itemize() { + [ -n "$quiet" ] && return case $output in gtk) echo "$@";; *) - local inp="$@" cols=$(get_cols) first offset - cols="${cols:-80}" - first="$(echo -e "$inp" | fold -sw$cols | head -n1)" + local inp="$@" w=$(get_cols) first offset + first="$(echo -e "$inp" | fold -sw$w | head -n1)" echo "$first" cols1="$(echo "${first:1}" | wc -c)" offset=$(echo "$first" | sed -n 's|^\([^:\*-]*[:\*-]\).*$|\1|p' | wc -m) - echo "${inp:$cols1}" | fold -sw$((cols - offset)) | awk \ + echo "${inp:$cols1}" | fold -sw$((w - offset)) | awk \ '($0){printf "%'$offset's%s\n","",$0}' ;; esac diff -r 973c3b2ecc5a -r cca198d5952c testsuite.sh --- a/testsuite.sh Mon Nov 30 02:33:22 2015 +0200 +++ b/testsuite.sh Thu Dec 03 04:59:59 2015 +0200 @@ -131,10 +131,12 @@ cat $activity rm -f $activity -check_libtaz -output='raw' -title 'Checking libtaz.sh: --output=raw' -check_libtaz +for output in '' raw gtk html; do + export output + newline; newline + title 'Checking libtaz.sh: --output=$output' + check_libtaz +done # Check libtaz.sh functions usage output='term'