slitaz-base-files 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 | 3395a6b92646 |
children | 3f16616140ea |
files | doc/libtaz.txt rootfs/lib/libtaz.sh |
line diff
1.1 --- a/doc/libtaz.txt Wed Apr 29 01:00:24 2015 +0300 1.2 +++ b/doc/libtaz.txt Thu Apr 30 02:00:25 2015 +0300 1.3 @@ -3,6 +3,7 @@ 1.4 . /lib/libtaz.sh 1.5 function 1.6 1.7 + 1.8 DESCRIPTION 1.9 LibTaz is the base SliTaz SHell library used by almost all tools and 1.10 utilities. It provides common SHell script functions, parses the cmdline 1.11 @@ -11,11 +12,12 @@ 1.12 a standard terminal supporting colors. LibTaz is in the directory 1.13 /lib/libtaz.sh since it is used when /usr may not be mounted. 1.14 1.15 + 1.16 FUNCTIONS 1.17 _ <string> Short name for "eval_gettext string; echo" 1.18 _n <string> Short name for "eval_gettext string" 1.19 status Return command status [Done|Failed] 1.20 - separator Display a line separator 1.21 + separator [<char>] Display a line separator 1.22 newline Echo a new line if gettext or echo -n is used 1.23 boldify <string> Use a bold font for term, html or GTK output 1.24 colorize NB <string> Colorize a string in term mode 1.25 @@ -28,17 +30,56 @@ 1.26 optlist <lines> Prints two-column list (of options, or functions, etc.) 1.27 longline <string> Wrap words in long terminal message 1.28 1.29 + 1.30 OPTIONS 1.31 --output=[raw|gtk|html] 1.32 --activity=/path/files/activity 1.33 1.34 + 1.35 EXAMPLES 1.36 + _ 'Hello, $USER!' 1.37 + _ 'Hello, %s!' $USER 1.38 + 1.39 + echo -n 'Removing...'; rm $file; status 1.40 + 1.41 + separator 1.42 + separator '*' 1.43 + 1.44 + newline 1.45 + 1.46 + boldify 'Bold text' 1.47 + 1.48 + colorize 32 "Message" 1.49 + myprogram --color=32 1.50 + 1.51 + indent 32 "Message" 1.52 + 1.53 + emsg "<b>bold</b> <c 31>red</c> separator<-> newline<n> <i 26>indent" 1.54 + 1.55 + check_root 1.56 + 1.57 + debug "A='$A'" 1.58 + 1.59 + echo -n "The question"; confirm 1.60 + confirm "The question (y/N)?" 1.61 + 1.62 + activity='/var/log/my.log' 1.63 log "Message" 1.64 - check_root 1.65 - emsg "<b>bold</b> <c 31>red</c> <c 32>green</c> separator<-> newline<n> <i 26>indent" 1.66 + 1.67 optlist "\ 1.68 -option1 Description1 (after one or any number of tab symbols) 1.69 -format $(_ 'disk') $(_ 'Format a specified disk')" 1.70 + option1 Description1 (after one or any number of tab symbols) 1.71 +-a option2 $(_ 'Description2')" 1.72 + 1.73 + longline $(busybox --help) 1.74 + longline "$(cat /usr/share/licenses/lgpl.txt)" 1.75 + 1.76 + 1.77 +USAGE NOTES 1.78 + SliTaz Base functions used from boot scripts to end user tools. 1.79 + Use gettext and not echo for messages. Keep output suitable for GTK boxes 1.80 + and Ncurses dialog. LibTaz should not depend on any configuration file. 1.81 + No bloated code here, functions must be used by at least 3-4 tools. 1.82 + 1.83 1.84 AUTHORS 1.85 Christophe Lincoln
2.1 --- a/rootfs/lib/libtaz.sh Wed Apr 29 01:00:24 2015 +0300 2.2 +++ b/rootfs/lib/libtaz.sh Thu Apr 30 02:00:25 2015 +0300 2.3 @@ -1,27 +1,25 @@ 2.4 #!/bin/sh 2.5 # 2.6 -# SliTaz Base functions used from boot scripts to end user tools. Use 2.7 -# gettext and not echo for messages. Keep output suitable for GTK boxes 2.8 -# and Ncurses dialog. LibTaz should not depend on any configuration file. 2.9 -# No bloated code here, functions must be used by at least 3-4 tools. 2.10 +# SliTaz Base functions. 2.11 +# Documentation: `man libtaz` or /usr/share/doc/slitaz/libtaz.txt 2.12 # 2.13 -# Documentation: man libtaz or /usr/share/doc/slitaz/libtaz.txt 2.14 -# 2.15 -# Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License 2.16 +# Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License 2.17 # 2.18 2.19 . /usr/bin/gettext.sh 2.20 2.21 -# short names for common i18n functions (like 'echo' and 'echo -n') 2.22 -_() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; } 2.23 -_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; } 2.24 -# usage #1: _ 'Hello, $USER!' 2.25 -# usage #2: _ 'Hello, %s!' $USER 2.26 - 2.27 -# internal i18n 2.28 +# Internal 2.29 lgettext() { gettext -d 'slitaz-base' "$@"; } 2.30 - 2.31 -# Internal variables. 2.32 +translate_query() { 2.33 + case $1 in 2.34 + y) lgettext "y";; 2.35 + Y) lgettext "Y";; 2.36 + n) lgettext "n";; 2.37 + N) lgettext "N";; 2.38 + # Support other cases but keep them untranslated. 2.39 + *) echo "$1" ;; 2.40 + esac 2.41 +} 2.42 okmsg="$(lgettext 'Done')" 2.43 ermsg="$(lgettext 'Failed')" 2.44 : ${okcolor=32} 2.45 @@ -29,66 +27,61 @@ 2.46 : ${decolor=36} 2.47 2.48 # Parse cmdline options and store values in a variable. 2.49 -for opt in "$@" 2.50 -do 2.51 +for opt in "$@"; do 2.52 case "$opt" in 2.53 - --*=*) export "${opt#--}" ;; 2.54 - --*) export ${opt#--}="yes" ;; 2.55 + --*=*) export "${opt#--}";; 2.56 + --*) export ${opt#--}='yes';; 2.57 esac 2.58 done 2.59 -[ "$HTTP_REFERER" ] && output="html" 2.60 +[ "$HTTP_REFERER" ] && output='html' 2.61 + 2.62 + 2.63 + 2.64 + 2.65 +# i18n functions 2.66 +_() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; } 2.67 +_n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; } 2.68 2.69 # Get terminal columns 2.70 -get_cols() { 2.71 - stty size 2>/dev/null | busybox cut -d " " -f 2 2.72 -} 2.73 +get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; } 2.74 2.75 -# Return command status. Default to colored console output. 2.76 +# Last command status 2.77 status() { 2.78 local check=$? 2.79 case $output in 2.80 raw|gtk) 2.81 - done=" $okmsg" 2.82 - error=" $ermsg" ;; 2.83 + done=" $okmsg" 2.84 + error=" $ermsg";; 2.85 html) 2.86 - done=" <span style='color: $okcolor;'>$okmsg</span>" 2.87 - error=" <span style='color: $ercolor;'>$ermsg</span>" ;; 2.88 + done=" <span class=\"float-right color$okcolor\">$okmsg</span>" 2.89 + error=" <span class=\"float-right color$ercolor\">$ermsg</span>";; 2.90 *) 2.91 local cols=$(get_cols) 2.92 - [ "$cols" ] || cols=80 2.93 - local scol=$(($cols - 10)) 2.94 - done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]" 2.95 - error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;; 2.96 + local scol=$((${cols:-80} - 10)) 2.97 + done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]" 2.98 + error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";; 2.99 esac 2.100 - if [ $check = 0 ]; then 2.101 - echo -e "$done" 2.102 - else 2.103 - echo -e "$error" 2.104 - fi 2.105 + case $check in 2.106 + 0) echo -e "$done";; 2.107 + *) echo -e "$error";; 2.108 + esac 2.109 } 2.110 2.111 -# Line separator. 2.112 +# Line separator 2.113 separator() { 2.114 - local sepchar="=" 2.115 - [ "$HTTP_REFERER" ] && local sepchar="<hr />" 2.116 case $output in 2.117 - raw|gtk) local sepchar="-"; local cols="8" ;; 2.118 - html) local sepchar="<hr />" ;; 2.119 + raw|gtk) echo '--------';; 2.120 + html) echo -n '<hr/>';; 2.121 *) 2.122 local cols=$(get_cols) 2.123 - [ "$cols" ] || cols=80 ;; 2.124 + printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";; 2.125 esac 2.126 - for c in $(seq 1 $cols); do 2.127 - echo -n "$sepchar" 2.128 - done && echo "" 2.129 } 2.130 2.131 -# New line for echo -n or gettext. 2.132 -newline() { 2.133 - echo "" 2.134 -} 2.135 +# New line 2.136 +newline() { echo; } 2.137 2.138 -# Display a bold message. GTK Yad: Works only in --text="" 2.139 +# Display a bold message 2.140 boldify() { 2.141 case $output in 2.142 raw) echo "$@" ;; 2.143 @@ -98,22 +91,19 @@ 2.144 esac 2.145 } 2.146 2.147 -# Usage: colorize colorNB "Message" or use --color=NB option 2.148 -# when running a tool. Default to white/38 and no html or gtk. 2.149 +# Colorize message 2.150 colorize() { 2.151 : ${color=$1} 2.152 shift 2.153 - local content="$@" 2.154 case $output in 2.155 - raw|gtk|html) echo "$content" ;; 2.156 - *) 2.157 - [ "$color" ] || color=38 2.158 - echo -e "\\033[1;${color}m${content}\\033[0;39m" ;; 2.159 + raw|gtk) echo "$@";; 2.160 + html) echo -n "<span class=\"color$color\">$@</span>";; 2.161 + *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;; 2.162 esac 2.163 unset color 2.164 } 2.165 2.166 -# Indent text $1 spaces. 2.167 +# Indent text 2.168 indent() { 2.169 local in="$1" 2.170 shift 2.171 @@ -132,6 +122,7 @@ 2.172 s|<n>|\n|g; s|<i [0-9]*>| |g' ;; 2.173 html) 2.174 echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \ 2.175 + s|<c \([0-9]*\)>|<span class="color\1">|g; s|</c>|</span>|g; \ 2.176 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;; 2.177 *) 2.178 local sep="\n" 2.179 @@ -148,7 +139,7 @@ 2.180 esac 2.181 } 2.182 2.183 -# Check if user is logged as root. 2.184 +# Check if user is logged as root 2.185 check_root() { 2.186 if [ $(id -u) != 0 ]; then 2.187 lgettext "You must be root to execute:"; echo " $(basename $0) $@" 2.188 @@ -158,23 +149,10 @@ 2.189 2.190 # Display debug info when --debug is used. 2.191 debug() { 2.192 - [ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1" 2.193 + [ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1" 2.194 } 2.195 2.196 -# Gettextize yes/no. 2.197 -translate_query() { 2.198 - case $1 in 2.199 - y) lgettext "y" ;; 2.200 - Y) lgettext "Y" ;; 2.201 - n) lgettext "n" ;; 2.202 - N) lgettext "N" ;; 2.203 - # Support other cases but keep them untranslated. 2.204 - *) echo "$1" ;; 2.205 - esac 2.206 -} 2.207 - 2.208 -# Usage 1: echo -n "The question"; confirm 2.209 -# Usage 2: confirm "The question (y/N)?" 2.210 +# Confirmation 2.211 confirm() { 2.212 if [ -n "$yes" ]; then 2.213 true 2.214 @@ -189,15 +167,12 @@ 2.215 fi 2.216 } 2.217 2.218 -# Log activities. $activity should be set by the script. The log format 2.219 -# is suitable for web interfaces like cook. Usage: log "String" 2.220 +# Log activities 2.221 log() { 2.222 - [ "$activity" ] || activity=/var/log/slitaz/libtaz.log 2.223 - echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity 2.224 + echo "$(date '+%Y-%m-%d %H:%M') : $@" >> ${activity:-/var/log/slitaz/libtaz.log} 2.225 } 2.226 2.227 -# Sophisticated function to print two-column list of options with descriptions 2.228 -# Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}` 2.229 +# Print two-column list of options with descriptions 2.230 optlist() { 2.231 local in cols col1=1 line 2.232 in="$(echo "$1" | sed 's| *| |g')" 2.233 @@ -213,6 +188,6 @@ 2.234 2.235 # Wrap words in long terminal message 2.236 longline() { 2.237 - cols=$(get_cols); [ "$cols" ] || cols=80 2.238 - echo -e "$@" | fold -sw$cols 2.239 + cols=$(get_cols) 2.240 + echo -e "$@" | fold -sw${cols:-80} 2.241 }