slitaz-base-files rev 271

libtaz.sh: add _p(), title(), footer(), action(), itemize(); update doc/libtaz.sh and testsuite.sh
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon May 25 19:32:59 2015 +0300 (2015-05-25)
parents 29f028ec8077
children 91fcb259b3bf
files doc/libtaz.txt rootfs/lib/libtaz.sh testsuite.sh
line diff
     1.1 --- a/doc/libtaz.txt	Thu Apr 30 02:00:25 2015 +0300
     1.2 +++ b/doc/libtaz.txt	Mon May 25 19:32:59 2015 +0300
     1.3 @@ -29,6 +29,10 @@
     1.4  	log <string>          Log activity, $activity must be set
     1.5  	optlist <lines>       Prints two-column list (of options, or functions, etc.)
     1.6  	longline <string>     Wrap words in long terminal message
     1.7 +	title <string>        Print localized title
     1.8 +	footer [<string>]     Print footer
     1.9 +	action <string>       Print action
    1.10 +	itemize <string>      Print long line as list item (check for :-* markers)
    1.11  
    1.12  
    1.13  OPTIONS
    1.14 @@ -73,6 +77,15 @@
    1.15  	longline $(busybox --help)
    1.16  	longline "$(cat /usr/share/licenses/lgpl.txt)"
    1.17  
    1.18 +	title "Summary"
    1.19 +	footer "$(_ 'No errors found')"
    1.20 +	action 'Checking settings...'; checking_settings; status
    1.21 +
    1.22 +	itemize "  - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed d\
    1.23 +o eiusmod tempor incididunt ut labore et dolore magna aliqua."
    1.24 +	itemize "  * Ut enim ad minim veniam, quis nostrud exercitation ullamco lab\
    1.25 +oris nisi ut aliquip ex ea commodo consequat."
    1.26 +
    1.27  
    1.28  USAGE NOTES
    1.29  	SliTaz Base functions used from boot scripts to end user tools.
     2.1 --- a/rootfs/lib/libtaz.sh	Thu Apr 30 02:00:25 2015 +0300
     2.2 +++ b/rootfs/lib/libtaz.sh	Mon May 25 19:32:59 2015 +0300
     2.3 @@ -41,6 +41,7 @@
     2.4  # i18n functions
     2.5  _()  { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
     2.6  _n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
     2.7 +_p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; }
     2.8  
     2.9  # Get terminal columns
    2.10  get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; }
    2.11 @@ -169,7 +170,7 @@
    2.12  
    2.13  # Log activities
    2.14  log() {
    2.15 -	echo "$(date '+%Y-%m-%d %H:%M') : $@" >> ${activity:-/var/log/slitaz/libtaz.log}
    2.16 +	echo "$(date '+%F %R') : $@" >> ${activity:-/var/log/slitaz/libtaz.log}
    2.17  }
    2.18  
    2.19  # Print two-column list of options with descriptions
    2.20 @@ -191,3 +192,39 @@
    2.21  	cols=$(get_cols)
    2.22  	echo -e "$@" | fold -sw${cols:-80}
    2.23  }
    2.24 +
    2.25 +# Print localized title
    2.26 +title() {
    2.27 +	case $output in
    2.28 +		html) echo "<section><header>$(_ "$@")</header><pre class=\"scroll\">";;
    2.29 +		*)    newline; boldify "$(_ "$@")"; separator;;
    2.30 +	esac
    2.31 +}
    2.32 +
    2.33 +# Print footer
    2.34 +footer() {
    2.35 +	case $output in
    2.36 +		html) echo "</pre><footer>$1</footer></section>";;
    2.37 +		*)    separator; echo "$1"; [ -n "$1" ] && newline;;
    2.38 +	esac
    2.39 +}
    2.40 +
    2.41 +# Print current action
    2.42 +action() {
    2.43 +	case $output in
    2.44 +		raw|gtk|html) _n "$@";;
    2.45 +		*) echo -ne "\033[0;33m"$(_ "$@")"\033[0m";;
    2.46 +	esac
    2.47 +}
    2.48 +
    2.49 +# Print long line as list item
    2.50 +itemize() {
    2.51 +	local inp="$@" cols=$(get_cols) first offset
    2.52 +	cols="${cols:-80}"
    2.53 +	first="$(echo -e "$inp" | fold -sw$cols | head -n1)"
    2.54 +	echo "$first"
    2.55 +	cols1="$(echo "${first:1}" | wc -c)"
    2.56 +	offset=$(echo "$first" | sed -n 's|^\([^:\*-]*[:\*-]\).*$|\1|p' | wc -m)
    2.57 +	echo "${inp:$cols1}" | fold -sw$((cols - offset)) | awk \
    2.58 +		'($0){printf "%'$offset's%s\n","",$0}'
    2.59 +}
     3.1 --- a/testsuite.sh	Thu Apr 30 02:00:25 2015 +0300
     3.2 +++ b/testsuite.sh	Mon May 25 19:32:59 2015 +0300
     3.3 @@ -8,11 +8,13 @@
     3.4  the initial files are described in the SliTaz Scratchbook: http://www.slitaz.\
     3.5  org/en/doc/scratchbook/"
     3.6  
     3.7 -	newline; boldify Available functions list:
     3.8 -	separator
     3.9 +
    3.10 +	title 'Available functions list:'
    3.11 +
    3.12  	optlist "\
    3.13 -_		Alias for eval_gettext with newline. Can be used with success both instead of gettext and eval_gettext.
    3.14 -_n		Alias for eval_gettext without newline at end.
    3.15 +_		Alias for gettext function with newline. Can be used with success both instead of gettext and eval_gettext.
    3.16 +_n		Alias for gettext function without newline at end.
    3.17 +_p		Alias for plural gettext function.
    3.18  get_cols	Get width of current terminal emulator or console. Number in columns.
    3.19  status		Output localized short message based on the previous command exit status (0 - OK, other than 0 - error).
    3.20  separator	Line separator for full terminal width.
    3.21 @@ -27,78 +29,102 @@
    3.22  		Note that 'y' and 'N' can be localized and this function knows about that.
    3.23  log		Log activities in /var/log/slitaz/libtaz.log (by default) or in specified log file.
    3.24  optlist		Sophisticated, UTF-8 friendly, function to print two-column list of options with descriptions.
    3.25 -longline	Doesn't break words into two lines of terminal when displaying long messages."
    3.26 -	separator; newline
    3.27 +longline	Doesn't break words into two lines of terminal when displaying long messages.
    3.28 +title		Print localized title.
    3.29 +footer		Print footer.
    3.30 +action		Print action.
    3.31 +itemize		Print long line as list item, check for markers: colon (:), dash (-), and asterisk (*)."
    3.32 +	separator '~'; newline
    3.33  
    3.34  
    3.35 -	echo -n "Checking libtaz.sh: status() 0"
    3.36 +	action 'Checking libtaz.sh: status() 0'
    3.37  	status
    3.38  
    3.39 -	echo -n "Checking libtaz.sh: status() 1"
    3.40 +	action 'Checking libtaz.sh: status() 1'
    3.41  	touch /tmp/1/2/2/4 2>/dev/null
    3.42  	status
    3.43  
    3.44 -	echo -n "Checking libtaz.sh: boldify() "
    3.45 +	action 'Checking libtaz.sh: boldify() '
    3.46  	boldify "Message"
    3.47  
    3.48 -	echo -n "Checking libtaz.sh: colorize() "
    3.49 +	action 'Checking libtaz.sh: colorize() '
    3.50  	echo -n $(colorize 33 "Message ")
    3.51  	echo -n $(colorize 35 "Message ")
    3.52  	colorize 36 "Message"
    3.53  
    3.54 -	echo "Checking libtaz.sh: separator"
    3.55 +	action 'Checking libtaz.sh: separator'; newline
    3.56  	separator
    3.57  
    3.58 -	echo -n "Checking libtaz.sh: emsg() "
    3.59 -	emsg "<b>bold</b> color: <c 31>bold red</c> <c 32>bold green</c> separator:<->newline:<n> message with<i 26>indent"
    3.60 +	action 'Checking libtaz.sh: emsg() '
    3.61 +	emsg '<b>bold</b> color: <c 31>bold red</c> <c 32>bold green</c> separator:<->newline:<n> message with<i 26>indent'
    3.62 +
    3.63 +	itemize "Fish: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed\
    3.64 + do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad mini\
    3.65 +m veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commo\
    3.66 +do consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ci\
    3.67 +llum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non pro\
    3.68 +ident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    3.69 +
    3.70 +	newline
    3.71 +
    3.72 +	itemize "  * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed d\
    3.73 +o eiusmod tempor incididunt ut labore et dolore magna aliqua."
    3.74 +	itemize "  * Ut enim ad minim veniam, quis nostrud exercitation ullamco lab\
    3.75 +oris nisi ut aliquip ex ea commodo consequat."
    3.76 +	itemize "  * Duis aute irure dolor in reprehenderit in voluptate velit esse\
    3.77 + cillum dolore eu fugiat nulla pariatur."
    3.78 +	itemize "  * Excepteur sint occaecat cupidatat non proident, sunt in culpa \
    3.79 +qui officia deserunt mollit anim id est laborum."
    3.80 +
    3.81 +
    3.82 +	newline; echo 'Using itemize() in the tazpkg:'
    3.83 +	tazpkg info gtk+
    3.84  }
    3.85  
    3.86  # Usage: check_functions path/to/lib.sh
    3.87  check_functions() {
    3.88 -	lib=$1
    3.89 +	lib="$1"
    3.90  	echo -n "$(boldify "Checking: $(basename $lib) functions")"
    3.91 -	indent 34 "$(colorize 32 $(grep "[a-z]() {" $lib | wc -l))"
    3.92 +	indent 34 "$(colorize 32 $(grep "[a-z_]() {" $lib | wc -l))"
    3.93  	separator
    3.94 -	grep "[a-z]() {" $lib | while read line
    3.95 -	do
    3.96 -		func=`echo "$line" | cut -d '(' -f 1`
    3.97 -		count=0
    3.98 -		usage=0
    3.99 -		echo -n "Checking: ${func}()"
   3.100 +	grep "[a-z_]() *{" $lib | while read line; do
   3.101 +		func=`echo "$line" | cut -d'(' -f1`
   3.102 +		count='0'
   3.103 +		usage='0'
   3.104  		for tool in /usr/bin/cook* /usr/bin/taz* /usr/bin/spk* /usr/sbin/spk* \
   3.105  			/sbin/taz* /sbin/hwsetup /var/www/cgi-bin/* /var/www/cooker/*.cgi \
   3.106  			/var/www/tazpanel/*.cgi 
   3.107  		do
   3.108  			[ -x "$tool" ] || continue
   3.109 -			count=$(grep "$func" $tool | wc -l)
   3.110 +			count=$(grep "$func[^a-z]" $tool | wc -l)
   3.111  			usage=$(($usage + $count))
   3.112  		done
   3.113 -		indent 34 "$usage"
   3.114 +		printf '%-34s%4d\n' "Checking: ${func}()" "$usage"
   3.115  	done
   3.116  	separator
   3.117  }
   3.118  
   3.119  #clear
   3.120 -echo -n "Checking libtaz.sh: log()"
   3.121 -activity=/tmp/testsuite.log
   3.122 -log "Message from SliTaz testsuite"
   3.123 +action 'Checking libtaz.sh: log()'
   3.124 +activity='/tmp/testsuite.log'
   3.125 +log 'Message from SliTaz testsuite'
   3.126  status
   3.127  cat $activity
   3.128  rm -f $activity
   3.129  
   3.130  check_libtaz
   3.131 -output="raw"
   3.132 -echo -e "\nChecking libtaz.sh: --output=raw"
   3.133 +output='raw'
   3.134 +title 'Checking libtaz.sh: --output=raw'
   3.135  check_libtaz
   3.136  
   3.137  # Check libtaz.sh functions usage
   3.138 -output="term"
   3.139 +output='term'
   3.140  check_functions 'rootfs/lib/libtaz.sh'
   3.141  
   3.142  # Check libpkg.sh functions usage
   3.143  check_functions 'rootfs/usr/lib/slitaz/libpkg.sh'
   3.144  
   3.145 -[ "$forced" ] && echo "Checking option: forced=$forced"
   3.146 -[ "$root" ] && echo "Checking option: root=$root"
   3.147 -[ ! "$1" ] && echo "Check options: $(basename $0) --forced --root=/dev/null"
   3.148 +[ -n "$forced" ] && echo "Checking option: forced=$forced"
   3.149 +[ -n "$root" ] && echo "Checking option: root=$root"
   3.150 +[ -z "$1" ] && echo "Check options: $(basename $0) --forced --root=/dev/null"
   3.151  exit 0