slitaz-base-files diff rootfs/lib/libtaz.sh @ rev 270

Small improvements. libtaz.sh: move notes from script to readme, re-organize and simplify code, different separators allowed; libtaz.txt: wrote few new examples.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Apr 30 02:00:25 2015 +0300 (2015-04-30)
parents e9258bc4e858
children 3f16616140ea
line diff
     1.1 --- a/rootfs/lib/libtaz.sh	Sat Mar 28 16:54:21 2015 +0200
     1.2 +++ b/rootfs/lib/libtaz.sh	Thu Apr 30 02:00:25 2015 +0300
     1.3 @@ -1,27 +1,25 @@
     1.4  #!/bin/sh
     1.5  #
     1.6 -# SliTaz Base functions used from boot scripts to end user tools. Use
     1.7 -# gettext and not echo for messages. Keep output suitable for GTK boxes
     1.8 -# and Ncurses dialog. LibTaz should not depend on any configuration file.
     1.9 -# No bloated code here, functions must be used by at least 3-4 tools.
    1.10 +# SliTaz Base functions.
    1.11 +# Documentation: `man libtaz` or /usr/share/doc/slitaz/libtaz.txt
    1.12  #
    1.13 -# Documentation: man libtaz or /usr/share/doc/slitaz/libtaz.txt
    1.14 -#
    1.15 -# Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License
    1.16 +# Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License
    1.17  #
    1.18  
    1.19  . /usr/bin/gettext.sh
    1.20  
    1.21 -# short names for common i18n functions (like 'echo' and 'echo -n')
    1.22 -_()  { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
    1.23 -_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
    1.24 -# usage #1: _ 'Hello, $USER!'
    1.25 -# usage #2: _ 'Hello, %s!' $USER
    1.26 -
    1.27 -# internal i18n
    1.28 +# Internal
    1.29  lgettext() { gettext -d 'slitaz-base' "$@"; }
    1.30 -
    1.31 -# Internal variables.
    1.32 +translate_query() {
    1.33 +	case $1 in
    1.34 +		y) lgettext "y";;
    1.35 +		Y) lgettext "Y";;
    1.36 +		n) lgettext "n";;
    1.37 +		N) lgettext "N";;
    1.38 +		# Support other cases but keep them untranslated.
    1.39 +		*) echo "$1" ;;
    1.40 +	esac
    1.41 +}
    1.42  okmsg="$(lgettext 'Done')"
    1.43  ermsg="$(lgettext 'Failed')"
    1.44  : ${okcolor=32}
    1.45 @@ -29,66 +27,61 @@
    1.46  : ${decolor=36}
    1.47  
    1.48  # Parse cmdline options and store values in a variable.
    1.49 -for opt in "$@"
    1.50 -do
    1.51 +for opt in "$@"; do
    1.52  	case "$opt" in
    1.53 -		--*=*) export "${opt#--}" ;;
    1.54 -		--*)   export  ${opt#--}="yes" ;;
    1.55 +		--*=*) export "${opt#--}";;
    1.56 +		--*)   export  ${opt#--}='yes';;
    1.57  	esac
    1.58  done
    1.59 -[ "$HTTP_REFERER" ] && output="html"
    1.60 +[ "$HTTP_REFERER" ] && output='html'
    1.61 +
    1.62 +
    1.63 +
    1.64 +
    1.65 +# i18n functions
    1.66 +_()  { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
    1.67 +_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
    1.68  
    1.69  # Get terminal columns
    1.70 -get_cols() {
    1.71 -	stty size 2>/dev/null | busybox cut -d " " -f 2
    1.72 -}
    1.73 +get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; }
    1.74  
    1.75 -# Return command status. Default to colored console output.
    1.76 +# Last command status
    1.77  status() {
    1.78  	local check=$?
    1.79  	case $output in
    1.80  		raw|gtk)
    1.81 -			done=" $okmsg"
    1.82 -			error=" $ermsg" ;;
    1.83 +			 done=" $okmsg"
    1.84 +			error=" $ermsg";;
    1.85  		html)
    1.86 -			done=" <span style='color: $okcolor;'>$okmsg</span>"
    1.87 -			error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
    1.88 +			 done=" <span class=\"float-right color$okcolor\">$okmsg</span>"
    1.89 +			error=" <span class=\"float-right color$ercolor\">$ermsg</span>";;
    1.90  		*)
    1.91  			local cols=$(get_cols)
    1.92 -			[ "$cols" ] || cols=80
    1.93 -			local scol=$(($cols - 10))
    1.94 -			done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
    1.95 -			error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
    1.96 +			local scol=$((${cols:-80} - 10))
    1.97 +			 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
    1.98 +			error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";;
    1.99  	esac
   1.100 -	if [ $check = 0 ]; then
   1.101 -		echo -e "$done"
   1.102 -	else
   1.103 -		echo -e "$error"
   1.104 -	fi
   1.105 +	case $check in
   1.106 +		0) echo -e "$done";;
   1.107 +		*) echo -e "$error";;
   1.108 +	esac
   1.109  }
   1.110  
   1.111 -# Line separator.
   1.112 +# Line separator
   1.113  separator() {
   1.114 -	local sepchar="="
   1.115 -	[ "$HTTP_REFERER" ] && local sepchar="<hr />"
   1.116  	case $output in
   1.117 -		raw|gtk) local sepchar="-"; local cols="8" ;;
   1.118 -		html)    local sepchar="<hr />" ;;
   1.119 +		raw|gtk) echo '--------';;
   1.120 +		html)    echo -n '<hr/>';;
   1.121  		*)
   1.122  			local cols=$(get_cols)
   1.123 -			[ "$cols" ] || cols=80 ;;
   1.124 +			printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";;
   1.125  	esac
   1.126 -	for c in $(seq 1 $cols); do
   1.127 -		echo -n "$sepchar"
   1.128 -	done && echo ""
   1.129  }
   1.130  
   1.131 -# New line for echo -n or gettext.
   1.132 -newline() {
   1.133 -	echo ""
   1.134 -}
   1.135 +# New line
   1.136 +newline() { echo; }
   1.137  
   1.138 -# Display a bold message. GTK Yad: Works only in --text=""
   1.139 +# Display a bold message
   1.140  boldify() {
   1.141  	case $output in
   1.142  		raw)  echo "$@" ;;
   1.143 @@ -98,22 +91,19 @@
   1.144  	esac
   1.145  }
   1.146  
   1.147 -# Usage: colorize colorNB "Message"  or use --color=NB option
   1.148 -# when running a tool. Default to white/38 and no html or gtk.
   1.149 +# Colorize message
   1.150  colorize() {
   1.151  	: ${color=$1}
   1.152  	shift
   1.153 -	local content="$@"
   1.154  	case $output in
   1.155 -		raw|gtk|html) echo "$content" ;;
   1.156 -		*)
   1.157 -			[ "$color" ] || color=38
   1.158 -			echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
   1.159 +		raw|gtk) echo "$@";;
   1.160 +		html)    echo -n "<span class=\"color$color\">$@</span>";;
   1.161 +		*)       echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;;
   1.162  	esac
   1.163  	unset color
   1.164  }
   1.165  
   1.166 -# Indent text $1 spaces.
   1.167 +# Indent text
   1.168  indent() {
   1.169  	local in="$1"
   1.170  	shift
   1.171 @@ -132,6 +122,7 @@
   1.172  			s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
   1.173  		html)
   1.174  			echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
   1.175 +			s|<c \([0-9]*\)>|<span class="color\1">|g; s|</c>|</span>|g; \
   1.176  			s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
   1.177  		*)
   1.178  			local sep="\n"
   1.179 @@ -148,7 +139,7 @@
   1.180  	esac
   1.181  }
   1.182  
   1.183 -# Check if user is logged as root.
   1.184 +# Check if user is logged as root
   1.185  check_root() {
   1.186  	if [ $(id -u) != 0 ]; then
   1.187  		lgettext "You must be root to execute:"; echo " $(basename $0) $@"
   1.188 @@ -158,23 +149,10 @@
   1.189  
   1.190  # Display debug info when --debug is used.
   1.191  debug() {
   1.192 -	[ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
   1.193 +	[ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
   1.194  }
   1.195  
   1.196 -# Gettextize yes/no.
   1.197 -translate_query() {
   1.198 -	case $1 in
   1.199 -		y) lgettext "y" ;;
   1.200 -		Y) lgettext "Y" ;;
   1.201 -		n) lgettext "n" ;;
   1.202 -		N) lgettext "N" ;;
   1.203 -		# Support other cases but keep them untranslated.
   1.204 -		*) echo "$1" ;;
   1.205 -	esac
   1.206 -}
   1.207 -
   1.208 -# Usage 1: echo -n "The question"; confirm
   1.209 -# Usage 2: confirm "The question (y/N)?"
   1.210 +# Confirmation
   1.211  confirm() {
   1.212  	if [ -n "$yes" ]; then
   1.213  		true
   1.214 @@ -189,15 +167,12 @@
   1.215  	fi
   1.216  }
   1.217  
   1.218 -# Log activities. $activity should be set by the script. The log format
   1.219 -# is suitable for web interfaces like cook. Usage: log "String"
   1.220 +# Log activities
   1.221  log() {
   1.222 -	[ "$activity" ] || activity=/var/log/slitaz/libtaz.log
   1.223 -	echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
   1.224 +	echo "$(date '+%Y-%m-%d %H:%M') : $@" >> ${activity:-/var/log/slitaz/libtaz.log}
   1.225  }
   1.226  
   1.227 -# Sophisticated function to print two-column list of options with descriptions
   1.228 -# Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}`
   1.229 +# Print two-column list of options with descriptions
   1.230  optlist() {
   1.231  	local in cols col1=1 line
   1.232  	in="$(echo "$1" | sed 's|		*|	|g')"
   1.233 @@ -213,6 +188,6 @@
   1.234  
   1.235  # Wrap words in long terminal message
   1.236  longline() {
   1.237 -	cols=$(get_cols); [ "$cols" ] || cols=80
   1.238 -	echo -e "$@" | fold -sw$cols
   1.239 +	cols=$(get_cols)
   1.240 +	echo -e "$@" | fold -sw${cols:-80}
   1.241  }