slitaz-base-files rev 289
libtaz: implement options --quiet and --cols=
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Thu Dec 03 04:59:59 2015 +0200 (2015-12-03) |
parents | 973c3b2ecc5a |
children | eecec1b9437a |
files | doc/libtaz.txt rootfs/lib/libtaz.sh testsuite.sh |
line diff
1.1 --- a/doc/libtaz.txt Mon Nov 30 02:33:22 2015 +0200 1.2 +++ b/doc/libtaz.txt Thu Dec 03 04:59:59 2015 +0200 1.3 @@ -38,6 +38,8 @@ 1.4 OPTIONS 1.5 --output=[raw|gtk|html] 1.6 --activity=/path/files/activity 1.7 + --quiet Display nothing but errors (usually: action ... status) 1.8 + --cols=<number> Output width in columns 1.9 1.10 1.11 EXAMPLES
2.1 --- a/rootfs/lib/libtaz.sh Mon Nov 30 02:33:22 2015 +0200 2.2 +++ b/rootfs/lib/libtaz.sh Thu Dec 03 04:59:59 2015 +0200 2.3 @@ -48,44 +48,43 @@ 2.4 _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; } 2.5 2.6 # Get terminal columns 2.7 -get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; } 2.8 +get_cols() { stty size 2>/dev/null | awk -vc=$cols 'END{print c?c:($2 && $2<80)?$2:80}'; } 2.9 2.10 # Last command status 2.11 status() { 2.12 - local check=$? 2.13 + local ret_code=$? 2.14 + [ -n "$quiet" -a "$ret_code" -eq 0 ] && return 2.15 + [ -n "$quiet" ] && action "$saved_action" no-quiet 2.16 + 2.17 + case $ret_code in 2.18 + 0) local msg="$okmsg" color="$okcolor";; 2.19 + *) local msg="$ermsg" color="$ercolor";; 2.20 + esac 2.21 case $output in 2.22 - raw|gtk) 2.23 - done=" $okmsg" 2.24 - error=" $ermsg";; 2.25 - html) 2.26 - done=" <span class=\"float-right color$okcolor\">$okmsg</span>" 2.27 - error=" <span class=\"float-right color$ercolor\">$ermsg</span>";; 2.28 - *) 2.29 - done="[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]" 2.30 - error="[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";; 2.31 - esac 2.32 - case $check in 2.33 - 0) echo -e "$done";; 2.34 - *) echo -e "$error";; 2.35 + raw|gtk) echo " $msg";; 2.36 + html) echo " <span class=\"float-right color$color\">$msg</span>";; 2.37 + *) echo -e "[ \\033[1;${color}m$msg\\033[0;39m ]";; 2.38 esac 2.39 } 2.40 2.41 # Line separator 2.42 separator() { 2.43 + [ -n "$quiet" ] && return 2.44 case $output in 2.45 gtk) echo '--------';; 2.46 - html) echo -n '<hr/>';; 2.47 - *) 2.48 - local cols=$(get_cols) 2.49 - printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";; 2.50 + html) echo -n '<hr/>';; 2.51 + *) printf "%$(get_cols)s\n" | tr ' ' "${1:-=}";; 2.52 esac 2.53 } 2.54 2.55 # New line 2.56 -newline() { echo; } 2.57 +newline() { 2.58 + [ -z "$quiet" ] && echo 2.59 +} 2.60 2.61 # Display a bold message 2.62 boldify() { 2.63 + [ -n "$quiet" ] && return 2.64 case $output in 2.65 raw) echo "$@" ;; 2.66 gtk) echo "<b>$@</b>" ;; 2.67 @@ -96,6 +95,7 @@ 2.68 2.69 # Colorize message 2.70 colorize() { 2.71 + [ -n "$quiet" ] && return 2.72 : ${color=$1} 2.73 shift 2.74 case $output in 2.75 @@ -111,6 +111,7 @@ 2.76 2.77 # Indent text 2.78 indent() { 2.79 + [ -n "$quiet" ] && return 2.80 local in="$1" 2.81 shift 2.82 echo -e "\033["$in"G $@"; 2.83 @@ -118,7 +119,8 @@ 2.84 2.85 # Extended MeSsaGe output 2.86 emsg() { 2.87 - local sep="\n--------\n" 2.88 + [ -n "$quiet" ] && return 2.89 + local sep="\n$(separator)\n" 2.90 case $output in 2.91 raw) 2.92 echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \ 2.93 @@ -131,12 +133,6 @@ 2.94 s|<c \([0-9]*\)>|<span class="color\1">|g; s|</c>|</span>|g; \ 2.95 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;; 2.96 *) 2.97 - local sep="\n" 2.98 - local cols=$(get_cols) 2.99 - [ "$cols" ] || cols=80 2.100 - for c in $(seq 1 $cols); do 2.101 - sep="${sep}=" 2.102 - done 2.103 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g; 2.104 s|<c 0\([0-9]*\)>|\\033[\1m|g; s|<c \([1-9][0-9]*\)>|\\033[1;\1m|g; 2.105 s|</c>|\\033[0;39m|g; s|<n>|\n|g; 2.106 @@ -148,7 +144,7 @@ 2.107 2.108 # Check if user is logged as root 2.109 check_root() { 2.110 - if [ $(id -u) != 0 ]; then 2.111 + if [ $(id -u) -ne 0 ]; then 2.112 lgettext "You must be root to execute:"; echo " $(basename $0) $@" 2.113 exit 1 2.114 fi 2.115 @@ -181,34 +177,36 @@ 2.116 2.117 # Print two-column list of options with descriptions 2.118 optlist() { 2.119 - local in cols col1=1 line 2.120 - in="$(echo "$1" | sed 's| *| |g')" 2.121 - cols=$(get_cols); [ "$cols" ] || cols=80 2.122 + [ -n "$quiet" ] && return 2.123 + local in="$(echo "$1" | sed 's| *| |g')" w=$(get_cols) col1=1 line 2.124 IFS=$'\n' 2.125 for line in $in; do 2.126 col=$(echo -n "$line" | cut -f1 | wc -m) 2.127 [ $col -gt $col1 ] && col1=$col 2.128 done 2.129 - echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \ 2.130 + echo "$in" | sed 's|\t|&\n|' | fold -sw$((w - col1 - 4)) | \ 2.131 sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}" 2.132 } 2.133 2.134 # Wrap words in long terminal message 2.135 longline() { 2.136 - cols=$(get_cols) 2.137 - echo -e "$@" | fold -sw${cols:-80} 2.138 + [ -n "$quiet" ] && return 2.139 + local w=$(get_cols) 2.140 + echo -e "$@" | fold -sw$w 2.141 } 2.142 2.143 # Print localized title 2.144 title() { 2.145 + [ -n "$quiet" ] && return 2.146 case $output in 2.147 html) echo "<section><header>$(_ "$@")</header><pre class=\"scroll\">";; 2.148 - *) newline; boldify "$(_ "$@")"; separator;; 2.149 + *) newline; boldify "$(_ "$@")"; separator;; 2.150 esac 2.151 } 2.152 2.153 # Print footer 2.154 footer() { 2.155 + [ -n "$quiet" ] && return 2.156 case $output in 2.157 html) echo "</pre><footer>$1</footer></section>";; 2.158 *) separator; echo "$1"; [ -n "$1" ] && newline;; 2.159 @@ -216,13 +214,17 @@ 2.160 } 2.161 2.162 # Print current action 2.163 +saved_action='' 2.164 action() { 2.165 - local w cols scol msg chars padding 2.166 + saved_action="$1" 2.167 + [ -n "$quiet" -a -z "$2" ] && return 2.168 + local w c scol msg chars 2.169 w=$(_ 'w'); w=${w/w/10} 2.170 - cols=$(get_cols); cols=${cols:-80}; scol=$(( $cols - $w )) 2.171 + c=$(get_cols) 2.172 + scol=$(( $c - $w )) 2.173 msg="$(_n "$@" | fold -sw$scol)" 2.174 - chars=$(echo -n "$msg" | tail -n1 | wc -m); padding=$(( $scol - $chars )) 2.175 - msg="$(printf '%s%'$padding's' "$msg" "")" 2.176 + chars=$(echo -n "$msg" | tail -n1 | wc -m) 2.177 + msg="$(printf '%s%'$(( $scol - $chars ))'s' "$msg" '')" 2.178 2.179 case $output in 2.180 raw|gtk|html) echo -n "$msg";; 2.181 @@ -232,16 +234,16 @@ 2.182 2.183 # Print long line as list item 2.184 itemize() { 2.185 + [ -n "$quiet" ] && return 2.186 case $output in 2.187 gtk) echo "$@";; 2.188 *) 2.189 - local inp="$@" cols=$(get_cols) first offset 2.190 - cols="${cols:-80}" 2.191 - first="$(echo -e "$inp" | fold -sw$cols | head -n1)" 2.192 + local inp="$@" w=$(get_cols) first offset 2.193 + first="$(echo -e "$inp" | fold -sw$w | head -n1)" 2.194 echo "$first" 2.195 cols1="$(echo "${first:1}" | wc -c)" 2.196 offset=$(echo "$first" | sed -n 's|^\([^:\*-]*[:\*-]\).*$|\1|p' | wc -m) 2.197 - echo "${inp:$cols1}" | fold -sw$((cols - offset)) | awk \ 2.198 + echo "${inp:$cols1}" | fold -sw$((w - offset)) | awk \ 2.199 '($0){printf "%'$offset's%s\n","",$0}' 2.200 ;; 2.201 esac
3.1 --- a/testsuite.sh Mon Nov 30 02:33:22 2015 +0200 3.2 +++ b/testsuite.sh Thu Dec 03 04:59:59 2015 +0200 3.3 @@ -131,10 +131,12 @@ 3.4 cat $activity 3.5 rm -f $activity 3.6 3.7 -check_libtaz 3.8 -output='raw' 3.9 -title 'Checking libtaz.sh: --output=raw' 3.10 -check_libtaz 3.11 +for output in '' raw gtk html; do 3.12 + export output 3.13 + newline; newline 3.14 + title 'Checking libtaz.sh: --output=$output' 3.15 + check_libtaz 3.16 +done 3.17 3.18 # Check libtaz.sh functions usage 3.19 output='term'