slitaz-base-files rev 221 5.4.2

libtaz.sh: add new functions - optlist and longline
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Aug 24 14:48:45 2013 +0300 (2013-08-24)
parents 79741640ba8a
children 46db9b4c3a46
files doc/libtaz.txt rootfs/lib/libtaz.sh testsuite.sh
line diff
     1.1 --- a/doc/libtaz.txt	Fri Aug 09 15:40:03 2013 +0300
     1.2 +++ b/doc/libtaz.txt	Sat Aug 24 14:48:45 2013 +0300
     1.3 @@ -12,19 +12,21 @@
     1.4  	/lib/libtaz.sh since it is used when /usr may not be mounted.
     1.5  
     1.6  FUNCTIONS
     1.7 -	_ string              Short name for "gettext string; echo"
     1.8 -	_n string             Short name for "gettext string"
     1.9 +	_ <string>            Short name for "eval_gettext string; echo"
    1.10 +	_n <string>           Short name for "eval_gettext string"
    1.11  	status                Return command status [Done|Failed]
    1.12  	separator             Display a line separator
    1.13  	newline               Echo a new line if gettext or echo -n is used
    1.14 -	boldify [string]      Use a bold font for term, html or GTK output
    1.15 -	colorize NB [string]  Colorize a string in term mode
    1.16 +	boldify <string>      Use a bold font for term, html or GTK output
    1.17 +	colorize NB <string>  Colorize a string in term mode
    1.18  	indent NB [string]    Indent text in term mode
    1.19 -	emsg [string]         Output mark up messages
    1.20 +	emsg <string>         Output mark up messages
    1.21  	check_root            Check if user is logged as root
    1.22 -	debug [string]        Display a DEBUG message when --debug is used
    1.23 +	debug <string>        Display a DEBUG message when --debug is used
    1.24  	confirm               Read answer to confirm an action
    1.25 -	log [string]          Log activity, $activity must be set
    1.26 +	log <string>          Log activity, $activity must be set
    1.27 +	optlist <lines>       Prints two-column list (of options, or functions, etc.)
    1.28 +	longline <string>     Wrap words in long terminal message
    1.29  
    1.30  OPTIONS
    1.31  	--output=[raw|gtk|html]
    1.32 @@ -34,7 +36,11 @@
    1.33  	log "Message"
    1.34  	check_root
    1.35  	emsg "<b>bold</b> <c 31>red</c> <c 32>green</c> separator<-> newline<n> <i 26>indent"
    1.36 +	optlist "\
    1.37 +option1				Description1 (after one or any number of tab symbols)
    1.38 +format $(_ 'disk')	$(_ 'Format a specified disk')"
    1.39  
    1.40  AUTHORS
    1.41 -	Written by Christophe Lincoln
    1.42 +	Christophe Lincoln
    1.43 +	Aleksej Bobylev
    1.44  
     2.1 --- a/rootfs/lib/libtaz.sh	Fri Aug 09 15:40:03 2013 +0300
     2.2 +++ b/rootfs/lib/libtaz.sh	Sat Aug 24 14:48:45 2013 +0300
     2.3 @@ -186,3 +186,25 @@
     2.4  	[ "$activity" ] || activity=/var/log/slitaz/libtaz.log
     2.5  	echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
     2.6  }
     2.7 +
     2.8 +# Sophisticated function to print two-column list of options with descriptions
     2.9 +# Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}`
    2.10 +optlist() {
    2.11 +	local in cols col1=1 line
    2.12 +	in="$(echo "$1" | sed 's|		*|	|g')"
    2.13 +	cols=$(get_cols); [ "$cols" ] || cols=80
    2.14 +	IFS="
    2.15 +"
    2.16 +	for line in $in; do
    2.17 +		col=$(echo -n "$line" | cut -f1 | wc -m)
    2.18 +		[ $col -gt $col1 ] && col1=$col
    2.19 +	done
    2.20 +	echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \
    2.21 +	sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*|  &|;s|\t\n||}"
    2.22 +}
    2.23 +
    2.24 +# Wrap words in long terminal message
    2.25 +longline() {
    2.26 +	cols=$(get_cols); [ "$cols" ] || cols=80
    2.27 +	echo -e "$@" | fold -sw$cols
    2.28 +}
     3.1 --- a/testsuite.sh	Fri Aug 09 15:40:03 2013 +0300
     3.2 +++ b/testsuite.sh	Sat Aug 24 14:48:45 2013 +0300
     3.3 @@ -3,6 +3,33 @@
     3.4  . rootfs/lib/libtaz.sh
     3.5  
     3.6  check_libtaz() {
     3.7 +	newline; longline "This package provides the base system files and \
     3.8 +directories, it is built using a wok receipt and Cookutils. The creation of \
     3.9 +the initial files are described in the SliTaz Scratchbook: http://www.slitaz.\
    3.10 +org/en/doc/scratchbook/"
    3.11 +
    3.12 +	newline; boldify Available functions list:
    3.13 +	separator
    3.14 +	optlist "\
    3.15 +_			Alias for eval_gettext with newline. Can be used with success both instead of gettext and eval_gettext.
    3.16 +_n			Alias for eval_gettext without newline at end.
    3.17 +get_cols	Get width of current terminal emulator or console. Number in columns.
    3.18 +status		Output localized short message based on the previous command exit status (0 - OK, other than 0 - error).
    3.19 +separator	Line separator for full terminal width.
    3.20 +newline		Produces empty line.
    3.21 +boldify		Display a bold message.
    3.22 +colorize	Color messages for terminal.
    3.23 +indent		Jump to specified column, useful for simple tabulated lists (tables).
    3.24 +emsg		All-in-one tool that contains: boldify, colorize, newline, separator and indent.
    3.25 +check_root	Check if user have root permissions (logged as root or used su for become root) for execute something.
    3.26 +debug		Display debug info when --debug is used.
    3.27 +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.
    3.28 +log			Log activities in /var/log/slitaz/libtaz.log (by default) or in specified log file.
    3.29 +optlist		Sophisticated, UTF-8 friendly, function to print two-column list of options with descriptions.
    3.30 +longline	Not break words into two lines of terminal when display long messages."
    3.31 +	separator; newline
    3.32 +
    3.33 +
    3.34  	echo -n "Checking libtaz.sh: status() 0"
    3.35  	status
    3.36  
    3.37 @@ -38,7 +65,8 @@
    3.38  		usage=0
    3.39  		echo -n "Checking: ${func}()"
    3.40  		for tool in /usr/bin/cook* /usr/bin/taz* /usr/bin/spk* /usr/sbin/spk* \
    3.41 -			/sbin/taz*
    3.42 +			/sbin/taz* /sbin/hwsetup /var/www/cgi-bin/* /var/www/cooker/*.cgi \
    3.43 +			/var/www/tazpanel/*.cgi 
    3.44  		do
    3.45  			[ -x "$tool" ] || continue
    3.46  			count=$(grep "$func" $tool | wc -l)