slitaz-base-files view rootfs/lib/libtaz.sh @ rev 263

libtaz.sh: fix '_' and '_n' when used space-separated arguments: _ 'Run "%s" in /.' "tazpkg -i $pkg"
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Dec 04 16:50:29 2014 +0200 (2014-12-04)
parents a410c3feb222
children e9258bc4e858
line source
1 #!/bin/sh
2 #
3 # SliTaz Base functions used from boot scripts to end user tools. Use
4 # gettext and not echo for messages. Keep output suitable for GTK boxes
5 # and Ncurses dialog. LibTaz should not depend on any configuration file.
6 # No bloated code here, functions must be used by at least 3-4 tools.
7 #
8 # Documentation: man libtaz or /usr/share/doc/slitaz/libtaz.txt
9 #
10 # Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License
11 #
13 . /usr/bin/gettext.sh
15 # short names for common i18n functions (like 'echo' and 'echo -n')
16 _() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
17 _n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
18 # usage #1: _ 'Hello, $USER!'
19 # usage #2: _ 'Hello, %s!' $USER
21 # internal i18n
22 lgettext() { gettext -d 'slitaz-base' "$@"; }
24 # Internal variables.
25 okmsg="$(lgettext 'Done')"
26 ermsg="$(lgettext 'Failed')"
27 : ${okcolor=32}
28 : ${ercolor=31}
29 : ${decolor=36}
31 # Parse cmdline options and store values in a variable.
32 for opt in "$@"
33 do
34 case "$opt" in
35 --*=*) export "${opt#--}" ;;
36 --*) export ${opt#--}="yes" ;;
37 esac
38 done
39 [ "$HTTP_REFERER" ] && output="html"
41 # Get terminal columns
42 get_cols() {
43 stty size 2>/dev/null | busybox cut -d " " -f 2
44 }
46 # Return command status. Default to colored console output.
47 status() {
48 local check=$?
49 case $output in
50 raw|gtk)
51 done=" $okmsg"
52 error=" $ermsg" ;;
53 html)
54 done=" <span style='color: $okcolor;'>$okmsg</span>"
55 error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
56 *)
57 local cols=$(get_cols)
58 [ "$cols" ] || cols=80
59 local scol=$(($cols - 10))
60 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
61 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
62 esac
63 if [ $check = 0 ]; then
64 echo -e "$done"
65 else
66 echo -e "$error"
67 fi
68 }
70 # Line separator.
71 separator() {
72 local sepchar="="
73 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
74 case $output in
75 raw|gtk) local sepchar="-"; local cols="8" ;;
76 html) local sepchar="<hr />" ;;
77 *)
78 local cols=$(get_cols)
79 [ "$cols" ] || cols=80 ;;
80 esac
81 for c in $(seq 1 $cols); do
82 echo -n "$sepchar"
83 done && echo ""
84 }
86 # New line for echo -n or gettext.
87 newline() {
88 echo ""
89 }
91 # Display a bold message. GTK Yad: Works only in --text=""
92 boldify() {
93 case $output in
94 raw) echo "$@" ;;
95 gtk) echo "<b>$@</b>" ;;
96 html) echo "<strong>$@</strong>" ;;
97 *) echo -e "\\033[1m$@\\033[0m" ;;
98 esac
99 }
101 # Usage: colorize colorNB "Message" or use --color=NB option
102 # when running a tool. Default to white/38 and no html or gtk.
103 colorize() {
104 : ${color=$1}
105 shift
106 local content="$@"
107 case $output in
108 raw|gtk|html) echo "$content" ;;
109 *)
110 [ "$color" ] || color=38
111 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
112 esac
113 unset color
114 }
116 # Indent text $1 spaces.
117 indent() {
118 local in="$1"
119 shift
120 echo -e "\033["$in"G $@";
121 }
123 # Extended MeSsaGe output
124 emsg() {
125 local sep="\n--------\n"
126 case $output in
127 raw)
128 echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \
129 s|</c>||g; s|<->|'$sep'|g; s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
130 gtk)
131 echo "$@" | sed -e 's|<c [0-9]*>||g; s|</c>||g; s|<->|'$sep'|g; \
132 s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
133 html)
134 echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
135 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
136 *)
137 local sep="\n"
138 local cols=$(get_cols)
139 [ "$cols" ] || cols=80
140 for c in $(seq 1 $cols); do
141 sep="${sep}="
142 done
143 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g; \
144 s|<c \([0-9]*\)>|\\033[1;\1m|g; s|</c>|\\033[0;39m|g; s|<n>|\n|g; \
145 s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
146 [ "$1" != "-n" ] && echo
147 ;;
148 esac
149 }
151 # Check if user is logged as root.
152 check_root() {
153 if [ $(id -u) != 0 ]; then
154 lgettext "You must be root to execute:"; echo " $(basename $0) $@"
155 exit 1
156 fi
157 }
159 # Display debug info when --debug is used.
160 debug() {
161 [ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
162 }
164 # Gettextize yes/no.
165 translate_query() {
166 case $1 in
167 y) lgettext "y" ;;
168 Y) lgettext "Y" ;;
169 n) lgettext "n" ;;
170 N) lgettext "N" ;;
171 # Support other cases but keep them untranslated.
172 *) echo "$1" ;;
173 esac
174 }
176 # Usage 1: echo -n "The question"; confirm
177 # Usage 2: confirm "The question (y/N)?"
178 confirm() {
179 if [ -n "$yes" ]; then
180 true
181 else
182 if [ -n "$1" ]; then
183 echo -n "$1 "
184 else
185 echo -n " ($(translate_query y)/$(translate_query N)) ? "
186 fi
187 read answer
188 [ "$answer" == "$(translate_query y)" ]
189 fi
190 }
192 # Log activities. $activity should be set by the script. The log format
193 # is suitable for web interfaces like cook. Usage: log "String"
194 log() {
195 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
196 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
197 }
199 # Sophisticated function to print two-column list of options with descriptions
200 # Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}`
201 optlist() {
202 local in cols col1=1 line
203 in="$(echo "$1" | sed 's| *| |g')"
204 cols=$(get_cols); [ "$cols" ] || cols=80
205 IFS=$'\n'
206 for line in $in; do
207 col=$(echo -n "$line" | cut -f1 | wc -m)
208 [ $col -gt $col1 ] && col1=$col
209 done
210 echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \
211 sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}"
212 }
214 # Wrap words in long terminal message
215 longline() {
216 cols=$(get_cols); [ "$cols" ] || cols=80
217 echo -e "$@" | fold -sw$cols
218 }