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

remove unboldify(), add emsg(); small fix i18n; make pot && make msgmerge, up ru.po
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Jun 03 14:09:18 2012 +0000 (2012-06-03)
parents 63ecdd740288
children e8ff99eab4e6
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 SliTaz GNU/Linux - BSD License
11 #
13 # Internationalization. We can't export TEXTDOMAIN because this script
14 # includes to other scripts with other TEXTDOMAIN exported
15 . /usr/bin/gettext.sh
17 # xgettext (from Makefile) can't extract strings from above example:
18 # gettext -d 'slitaz-base' 'Done'
19 # so, I define own function (and add it as option to xgettext to Makefile)
20 lgettext() {
21 gettext -d 'slitaz-base' $1
22 }
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 | 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)
141 do
142 sep="${sep}="
143 done
144 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g; \
145 s|<c \([0-9]*\)>|\\033[1;\1m|g; s|</c>|\\033[0;39m|g; s|<n>|\n|g; \
146 s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
147 [ "$1" != "-n" ] && echo
148 ;;
149 esac
150 }
152 # Check if user is logged as root.
153 check_root() {
154 if [ $(id -u) != 0 ]; then
155 lgettext "You must be root to execute:" && echo " $(basename $0) $@"
156 exit 1
157 fi
158 }
160 # Display debug info when --debug is used.
161 debug() {
162 [ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
163 }
165 # Gettextize yes/no.
166 translate_query() {
167 case $1 in
168 y) lgettext "y" ;;
169 Y) lgettext "Y" ;;
170 n) lgettext "n" ;;
171 N) lgettext "N" ;;
172 # Support other cases but keep them untranslated.
173 *) echo "$1" ;;
174 esac
175 }
177 # Usage: echo -n "The question" && confirm
178 confirm() {
179 [ "$yes" ] && true
180 echo -n " ($(translate_query y)/$(translate_query N)) ? "
181 read answer
182 [ "$answer" == "$(translate_query y)" ]
183 }
185 # Log activities. $activity should be set by the script. The log format
186 # is suitable for web interfaces like cook. Usage: log "String"
187 log() {
188 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
189 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
190 }