# HG changeset patch # User Aleksej Bobylev # Date 1430348425 -10800 # Node ID 29f028ec8077e24f3066eb3e823d92206914a507 # Parent 3395a6b92646cbcb1b8e1775a715abf9c5c2443a Small improvements. libtaz.sh: move notes from script to readme, re-organize and simplify code, different separators allowed; libtaz.txt: wrote few new examples. diff -r 3395a6b92646 -r 29f028ec8077 doc/libtaz.txt --- a/doc/libtaz.txt Wed Apr 29 01:00:24 2015 +0300 +++ b/doc/libtaz.txt Thu Apr 30 02:00:25 2015 +0300 @@ -3,6 +3,7 @@ . /lib/libtaz.sh function + DESCRIPTION LibTaz is the base SliTaz SHell library used by almost all tools and utilities. It provides common SHell script functions, parses the cmdline @@ -11,11 +12,12 @@ a standard terminal supporting colors. LibTaz is in the directory /lib/libtaz.sh since it is used when /usr may not be mounted. + FUNCTIONS _ 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 + separator [] Display a line separator newline Echo a new line if gettext or echo -n is used boldify Use a bold font for term, html or GTK output colorize NB Colorize a string in term mode @@ -28,17 +30,56 @@ optlist Prints two-column list (of options, or functions, etc.) longline Wrap words in long terminal message + OPTIONS --output=[raw|gtk|html] --activity=/path/files/activity + EXAMPLES + _ 'Hello, $USER!' + _ 'Hello, %s!' $USER + + echo -n 'Removing...'; rm $file; status + + separator + separator '*' + + newline + + boldify 'Bold text' + + colorize 32 "Message" + myprogram --color=32 + + indent 32 "Message" + + emsg "bold red separator<-> newline indent" + + check_root + + debug "A='$A'" + + echo -n "The question"; confirm + confirm "The question (y/N)?" + + activity='/var/log/my.log' 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')" + option1 Description1 (after one or any number of tab symbols) +-a option2 $(_ 'Description2')" + + longline $(busybox --help) + longline "$(cat /usr/share/licenses/lgpl.txt)" + + +USAGE NOTES + SliTaz Base functions used from boot scripts to end user tools. + Use gettext and not echo for messages. Keep output suitable for GTK boxes + and Ncurses dialog. LibTaz should not depend on any configuration file. + No bloated code here, functions must be used by at least 3-4 tools. + AUTHORS Christophe Lincoln diff -r 3395a6b92646 -r 29f028ec8077 rootfs/lib/libtaz.sh --- a/rootfs/lib/libtaz.sh Wed Apr 29 01:00:24 2015 +0300 +++ b/rootfs/lib/libtaz.sh Thu Apr 30 02:00:25 2015 +0300 @@ -1,27 +1,25 @@ #!/bin/sh # -# SliTaz Base functions used from boot scripts to end user tools. Use -# gettext and not echo for messages. Keep output suitable for GTK boxes -# and Ncurses dialog. LibTaz should not depend on any configuration file. -# No bloated code here, functions must be used by at least 3-4 tools. +# SliTaz Base functions. +# Documentation: `man libtaz` or /usr/share/doc/slitaz/libtaz.txt # -# Documentation: man libtaz or /usr/share/doc/slitaz/libtaz.txt -# -# Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License +# Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License # . /usr/bin/gettext.sh -# short names for common i18n functions (like 'echo' and 'echo -n') -_() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; } -_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; } -# usage #1: _ 'Hello, $USER!' -# usage #2: _ 'Hello, %s!' $USER - -# internal i18n +# Internal lgettext() { gettext -d 'slitaz-base' "$@"; } - -# Internal variables. +translate_query() { + case $1 in + y) lgettext "y";; + Y) lgettext "Y";; + n) lgettext "n";; + N) lgettext "N";; + # Support other cases but keep them untranslated. + *) echo "$1" ;; + esac +} okmsg="$(lgettext 'Done')" ermsg="$(lgettext 'Failed')" : ${okcolor=32} @@ -29,66 +27,61 @@ : ${decolor=36} # Parse cmdline options and store values in a variable. -for opt in "$@" -do +for opt in "$@"; do case "$opt" in - --*=*) export "${opt#--}" ;; - --*) export ${opt#--}="yes" ;; + --*=*) export "${opt#--}";; + --*) export ${opt#--}='yes';; esac done -[ "$HTTP_REFERER" ] && output="html" +[ "$HTTP_REFERER" ] && output='html' + + + + +# i18n functions +_() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; } +_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; } # Get terminal columns -get_cols() { - stty size 2>/dev/null | busybox cut -d " " -f 2 -} +get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; } -# Return command status. Default to colored console output. +# Last command status status() { local check=$? case $output in raw|gtk) - done=" $okmsg" - error=" $ermsg" ;; + done=" $okmsg" + error=" $ermsg";; html) - done=" $okmsg" - error=" $ermsg" ;; + done=" $okmsg" + error=" $ermsg";; *) local cols=$(get_cols) - [ "$cols" ] || cols=80 - local scol=$(($cols - 10)) - done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]" - error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;; + local scol=$((${cols:-80} - 10)) + done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]" + error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";; esac - if [ $check = 0 ]; then - echo -e "$done" - else - echo -e "$error" - fi + case $check in + 0) echo -e "$done";; + *) echo -e "$error";; + esac } -# Line separator. +# Line separator separator() { - local sepchar="=" - [ "$HTTP_REFERER" ] && local sepchar="
" case $output in - raw|gtk) local sepchar="-"; local cols="8" ;; - html) local sepchar="
" ;; + raw|gtk) echo '--------';; + html) echo -n '
';; *) local cols=$(get_cols) - [ "$cols" ] || cols=80 ;; + printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";; esac - for c in $(seq 1 $cols); do - echo -n "$sepchar" - done && echo "" } -# New line for echo -n or gettext. -newline() { - echo "" -} +# New line +newline() { echo; } -# Display a bold message. GTK Yad: Works only in --text="" +# Display a bold message boldify() { case $output in raw) echo "$@" ;; @@ -98,22 +91,19 @@ esac } -# Usage: colorize colorNB "Message" or use --color=NB option -# when running a tool. Default to white/38 and no html or gtk. +# Colorize message colorize() { : ${color=$1} shift - local content="$@" case $output in - raw|gtk|html) echo "$content" ;; - *) - [ "$color" ] || color=38 - echo -e "\\033[1;${color}m${content}\\033[0;39m" ;; + raw|gtk) echo "$@";; + html) echo -n "$@";; + *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;; esac unset color } -# Indent text $1 spaces. +# Indent text indent() { local in="$1" shift @@ -132,6 +122,7 @@ s||\n|g; s|| |g' ;; html) echo "$@" | sed -e 's|||g; s|||g; \ + s|||g; s|||g; \ s||
|g; s|<->|
|g; s|| |g' ;; *) local sep="\n" @@ -148,7 +139,7 @@ esac } -# Check if user is logged as root. +# Check if user is logged as root check_root() { if [ $(id -u) != 0 ]; then lgettext "You must be root to execute:"; echo " $(basename $0) $@" @@ -158,23 +149,10 @@ # Display debug info when --debug is used. debug() { - [ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1" + [ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1" } -# Gettextize yes/no. -translate_query() { - case $1 in - y) lgettext "y" ;; - Y) lgettext "Y" ;; - n) lgettext "n" ;; - N) lgettext "N" ;; - # Support other cases but keep them untranslated. - *) echo "$1" ;; - esac -} - -# Usage 1: echo -n "The question"; confirm -# Usage 2: confirm "The question (y/N)?" +# Confirmation confirm() { if [ -n "$yes" ]; then true @@ -189,15 +167,12 @@ fi } -# Log activities. $activity should be set by the script. The log format -# is suitable for web interfaces like cook. Usage: log "String" +# Log activities log() { - [ "$activity" ] || activity=/var/log/slitaz/libtaz.log - echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity + echo "$(date '+%Y-%m-%d %H:%M') : $@" >> ${activity:-/var/log/slitaz/libtaz.log} } -# Sophisticated function to print two-column list of options with descriptions -# Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}` +# Print two-column list of options with descriptions optlist() { local in cols col1=1 line in="$(echo "$1" | sed 's| *| |g')" @@ -213,6 +188,6 @@ # Wrap words in long terminal message longline() { - cols=$(get_cols); [ "$cols" ] || cols=80 - echo -e "$@" | fold -sw$cols + cols=$(get_cols) + echo -e "$@" | fold -sw${cols:-80} }