# HG changeset patch # User Aleksej Bobylev # Date 1432571579 -10800 # Node ID 3f16616140ea29ded77294ce547f6952526a8236 # Parent 29f028ec8077e24f3066eb3e823d92206914a507 libtaz.sh: add _p(), title(), footer(), action(), itemize(); update doc/libtaz.sh and testsuite.sh diff -r 29f028ec8077 -r 3f16616140ea doc/libtaz.txt --- a/doc/libtaz.txt Thu Apr 30 02:00:25 2015 +0300 +++ b/doc/libtaz.txt Mon May 25 19:32:59 2015 +0300 @@ -29,6 +29,10 @@ log Log activity, $activity must be set optlist Prints two-column list (of options, or functions, etc.) longline Wrap words in long terminal message + title Print localized title + footer [] Print footer + action Print action + itemize Print long line as list item (check for :-* markers) OPTIONS @@ -73,6 +77,15 @@ longline $(busybox --help) longline "$(cat /usr/share/licenses/lgpl.txt)" + title "Summary" + footer "$(_ 'No errors found')" + action 'Checking settings...'; checking_settings; status + + itemize " - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed d\ +o eiusmod tempor incididunt ut labore et dolore magna aliqua." + itemize " * Ut enim ad minim veniam, quis nostrud exercitation ullamco lab\ +oris nisi ut aliquip ex ea commodo consequat." + USAGE NOTES SliTaz Base functions used from boot scripts to end user tools. diff -r 29f028ec8077 -r 3f16616140ea rootfs/lib/libtaz.sh --- a/rootfs/lib/libtaz.sh Thu Apr 30 02:00:25 2015 +0300 +++ b/rootfs/lib/libtaz.sh Mon May 25 19:32:59 2015 +0300 @@ -41,6 +41,7 @@ # i18n functions _() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; } _n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; } +_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; } @@ -169,7 +170,7 @@ # Log activities log() { - echo "$(date '+%Y-%m-%d %H:%M') : $@" >> ${activity:-/var/log/slitaz/libtaz.log} + echo "$(date '+%F %R') : $@" >> ${activity:-/var/log/slitaz/libtaz.log} } # Print two-column list of options with descriptions @@ -191,3 +192,39 @@ cols=$(get_cols) echo -e "$@" | fold -sw${cols:-80} } + +# Print localized title +title() { + case $output in + html) echo "
$(_ "$@")
";;
+		*)    newline; boldify "$(_ "$@")"; separator;;
+	esac
+}
+
+# Print footer
+footer() {
+	case $output in
+		html) echo "
$1
";; + *) separator; echo "$1"; [ -n "$1" ] && newline;; + esac +} + +# Print current action +action() { + case $output in + raw|gtk|html) _n "$@";; + *) echo -ne "\033[0;33m"$(_ "$@")"\033[0m";; + esac +} + +# Print long line as list item +itemize() { + local inp="$@" cols=$(get_cols) first offset + cols="${cols:-80}" + first="$(echo -e "$inp" | fold -sw$cols | 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 \ + '($0){printf "%'$offset's%s\n","",$0}' +} diff -r 29f028ec8077 -r 3f16616140ea testsuite.sh --- a/testsuite.sh Thu Apr 30 02:00:25 2015 +0300 +++ b/testsuite.sh Mon May 25 19:32:59 2015 +0300 @@ -8,11 +8,13 @@ the initial files are described in the SliTaz Scratchbook: http://www.slitaz.\ org/en/doc/scratchbook/" - newline; boldify Available functions list: - separator + + title 'Available functions list:' + 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. +_ Alias for gettext function with newline. Can be used with success both instead of gettext and eval_gettext. +_n Alias for gettext function without newline at end. +_p Alias for plural gettext function. 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. @@ -27,78 +29,102 @@ Note that 'y' and 'N' can be localized and this function knows 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 Doesn't break words into two lines of terminal when displaying long messages." - separator; newline +longline Doesn't break words into two lines of terminal when displaying long messages. +title Print localized title. +footer Print footer. +action Print action. +itemize Print long line as list item, check for markers: colon (:), dash (-), and asterisk (*)." + separator '~'; newline - echo -n "Checking libtaz.sh: status() 0" + action 'Checking libtaz.sh: status() 0' status - echo -n "Checking libtaz.sh: status() 1" + action 'Checking libtaz.sh: status() 1' touch /tmp/1/2/2/4 2>/dev/null status - echo -n "Checking libtaz.sh: boldify() " + action 'Checking libtaz.sh: boldify() ' boldify "Message" - echo -n "Checking libtaz.sh: colorize() " + action 'Checking libtaz.sh: colorize() ' echo -n $(colorize 33 "Message ") echo -n $(colorize 35 "Message ") colorize 36 "Message" - echo "Checking libtaz.sh: separator" + action 'Checking libtaz.sh: separator'; newline separator - echo -n "Checking libtaz.sh: emsg() " - emsg "bold color: bold red bold green separator:<->newline: message withindent" + action 'Checking libtaz.sh: emsg() ' + emsg 'bold color: bold red bold green separator:<->newline: message withindent' + + itemize "Fish: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed\ + do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad mini\ +m veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commo\ +do consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ci\ +llum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non pro\ +ident, sunt in culpa qui officia deserunt mollit anim id est laborum." + + newline + + itemize " * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed d\ +o eiusmod tempor incididunt ut labore et dolore magna aliqua." + itemize " * Ut enim ad minim veniam, quis nostrud exercitation ullamco lab\ +oris nisi ut aliquip ex ea commodo consequat." + itemize " * Duis aute irure dolor in reprehenderit in voluptate velit esse\ + cillum dolore eu fugiat nulla pariatur." + itemize " * Excepteur sint occaecat cupidatat non proident, sunt in culpa \ +qui officia deserunt mollit anim id est laborum." + + + newline; echo 'Using itemize() in the tazpkg:' + tazpkg info gtk+ } # Usage: check_functions path/to/lib.sh check_functions() { - lib=$1 + lib="$1" echo -n "$(boldify "Checking: $(basename $lib) functions")" - indent 34 "$(colorize 32 $(grep "[a-z]() {" $lib | wc -l))" + indent 34 "$(colorize 32 $(grep "[a-z_]() {" $lib | wc -l))" separator - grep "[a-z]() {" $lib | while read line - do - func=`echo "$line" | cut -d '(' -f 1` - count=0 - usage=0 - echo -n "Checking: ${func}()" + grep "[a-z_]() *{" $lib | while read line; do + func=`echo "$line" | cut -d'(' -f1` + count='0' + usage='0' for tool in /usr/bin/cook* /usr/bin/taz* /usr/bin/spk* /usr/sbin/spk* \ /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) + count=$(grep "$func[^a-z]" $tool | wc -l) usage=$(($usage + $count)) done - indent 34 "$usage" + printf '%-34s%4d\n' "Checking: ${func}()" "$usage" done separator } #clear -echo -n "Checking libtaz.sh: log()" -activity=/tmp/testsuite.log -log "Message from SliTaz testsuite" +action 'Checking libtaz.sh: log()' +activity='/tmp/testsuite.log' +log 'Message from SliTaz testsuite' status cat $activity rm -f $activity check_libtaz -output="raw" -echo -e "\nChecking libtaz.sh: --output=raw" +output='raw' +title 'Checking libtaz.sh: --output=raw' check_libtaz # Check libtaz.sh functions usage -output="term" +output='term' check_functions 'rootfs/lib/libtaz.sh' # Check libpkg.sh functions usage check_functions 'rootfs/usr/lib/slitaz/libpkg.sh' -[ "$forced" ] && echo "Checking option: forced=$forced" -[ "$root" ] && echo "Checking option: root=$root" -[ ! "$1" ] && echo "Check options: $(basename $0) --forced --root=/dev/null" +[ -n "$forced" ] && echo "Checking option: forced=$forced" +[ -n "$root" ] && echo "Checking option: root=$root" +[ -z "$1" ] && echo "Check options: $(basename $0) --forced --root=/dev/null" exit 0