# HG changeset patch # User Aleksej Bobylev # Date 1377344925 -10800 # Node ID 259c174621c77355ce1e8fcf4737a14b6bc69308 # Parent 79741640ba8a8fee8bca1bd7061143656738dc72 libtaz.sh: add new functions - optlist and longline diff -r 79741640ba8a -r 259c174621c7 doc/libtaz.txt --- a/doc/libtaz.txt Fri Aug 09 15:40:03 2013 +0300 +++ b/doc/libtaz.txt Sat Aug 24 14:48:45 2013 +0300 @@ -12,19 +12,21 @@ /lib/libtaz.sh since it is used when /usr may not be mounted. FUNCTIONS - _ string Short name for "gettext string; echo" - _n string Short name for "gettext string" + _ Short name for "eval_gettext string; echo" + _n Short name for "eval_gettext string" status Return command status [Done|Failed] separator Display a line separator newline Echo a new line if gettext or echo -n is used - boldify [string] Use a bold font for term, html or GTK output - colorize NB [string] Colorize a string in term mode + boldify Use a bold font for term, html or GTK output + colorize NB Colorize a string in term mode indent NB [string] Indent text in term mode - emsg [string] Output mark up messages + emsg Output mark up messages check_root Check if user is logged as root - debug [string] Display a DEBUG message when --debug is used + debug Display a DEBUG message when --debug is used confirm Read answer to confirm an action - log [string] Log activity, $activity must be set + log Log activity, $activity must be set + optlist Prints two-column list (of options, or functions, etc.) + longline Wrap words in long terminal message OPTIONS --output=[raw|gtk|html] @@ -34,7 +36,11 @@ log "Message" check_root emsg "bold red green separator<-> newline indent" + optlist "\ +option1 Description1 (after one or any number of tab symbols) +format $(_ 'disk') $(_ 'Format a specified disk')" AUTHORS - Written by Christophe Lincoln + Christophe Lincoln + Aleksej Bobylev diff -r 79741640ba8a -r 259c174621c7 rootfs/lib/libtaz.sh --- a/rootfs/lib/libtaz.sh Fri Aug 09 15:40:03 2013 +0300 +++ b/rootfs/lib/libtaz.sh Sat Aug 24 14:48:45 2013 +0300 @@ -186,3 +186,25 @@ [ "$activity" ] || activity=/var/log/slitaz/libtaz.log echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity } + +# Sophisticated function to print two-column list of options with descriptions +# Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}` +optlist() { + local in cols col1=1 line + in="$(echo "$1" | sed 's| *| |g')" + cols=$(get_cols); [ "$cols" ] || cols=80 + IFS=" +" + 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)) | \ + sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}" +} + +# Wrap words in long terminal message +longline() { + cols=$(get_cols); [ "$cols" ] || cols=80 + echo -e "$@" | fold -sw$cols +} diff -r 79741640ba8a -r 259c174621c7 testsuite.sh --- a/testsuite.sh Fri Aug 09 15:40:03 2013 +0300 +++ b/testsuite.sh Sat Aug 24 14:48:45 2013 +0300 @@ -3,6 +3,33 @@ . rootfs/lib/libtaz.sh check_libtaz() { + newline; longline "This package provides the base system files and \ +directories, it is built using a wok receipt and Cookutils. The creation of \ +the initial files are described in the SliTaz Scratchbook: http://www.slitaz.\ +org/en/doc/scratchbook/" + + newline; boldify Available functions list: + separator + optlist "\ +_ Alias for eval_gettext with newline. Can be used with success both instead of gettext and eval_gettext. +_n Alias for eval_gettext without newline at end. +get_cols Get width of current terminal emulator or console. Number in columns. +status Output localized short message based on the previous command exit status (0 - OK, other than 0 - error). +separator Line separator for full terminal width. +newline Produces empty line. +boldify Display a bold message. +colorize Color messages for terminal. +indent Jump to specified column, useful for simple tabulated lists (tables). +emsg All-in-one tool that contains: boldify, colorize, newline, separator and indent. +check_root Check if user have root permissions (logged as root or used su for become root) for execute something. +debug Display debug info when --debug is used. +confirm Used at end of questions - adds '(y/N)?' and waits for answer. Press 'y' if yes, any other letters/words or just Enter - if no. Note that 'y' and 'N' can be localized and this function know about that. +log Log activities in /var/log/slitaz/libtaz.log (by default) or in specified log file. +optlist Sophisticated, UTF-8 friendly, function to print two-column list of options with descriptions. +longline Not break words into two lines of terminal when display long messages." + separator; newline + + echo -n "Checking libtaz.sh: status() 0" status @@ -38,7 +65,8 @@ usage=0 echo -n "Checking: ${func}()" for tool in /usr/bin/cook* /usr/bin/taz* /usr/bin/spk* /usr/sbin/spk* \ - /sbin/taz* + /sbin/taz* /sbin/hwsetup /var/www/cgi-bin/* /var/www/cooker/*.cgi \ + /var/www/tazpanel/*.cgi do [ -x "$tool" ] || continue count=$(grep "$func" $tool | wc -l)