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

libtaz.sh: add (and use) short gettext aliases: _ and _n
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Aug 09 15:38:43 2013 +0300 (2013-08-09)
parents e8ff99eab4e6
children 259c174621c7
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-2013 SliTaz GNU/Linux - BSD License
11 #
13 . /usr/bin/gettext.sh
15 # short names for common i18n functions (like 'echo' and 'echo -n')
16 _() { eval_gettext "$@"; echo; }
17 _n() { eval_gettext "$@"; }
19 # internal i18n
20 lgettext() { gettext -d 'slitaz-base' "$@"; }
22 # Internal variables.
23 okmsg="$(lgettext 'Done')"
24 ermsg="$(lgettext 'Failed')"
25 : ${okcolor=32}
26 : ${ercolor=31}
27 : ${decolor=36}
29 # Parse cmdline options and store values in a variable.
30 for opt in "$@"
31 do
32 case "$opt" in
33 --*=*) export ${opt#--} ;;
34 --*) export ${opt#--}="yes" ;;
35 esac
36 done
37 [ "$HTTP_REFERER" ] && output="html"
39 # Get terminal columns
40 get_cols() {
41 stty size 2>/dev/null | cut -d " " -f 2
42 }
44 # Return command status. Default to colored console output.
45 status() {
46 local check=$?
47 case $output in
48 raw|gtk)
49 done=" $okmsg"
50 error=" $ermsg" ;;
51 html)
52 done=" <span style='color: $okcolor;'>$okmsg</span>"
53 error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
54 *)
55 local cols=$(get_cols)
56 [ "$cols" ] || cols=80
57 local scol=$(($cols - 10))
58 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
59 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
60 esac
61 if [ $check = 0 ]; then
62 echo -e "$done"
63 else
64 echo -e "$error"
65 fi
66 }
68 # Line separator.
69 separator() {
70 local sepchar="="
71 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
72 case $output in
73 raw|gtk) local sepchar="-" && local cols="8" ;;
74 html) local sepchar="<hr />" ;;
75 *)
76 local cols=$(get_cols)
77 [ "$cols" ] || cols=80 ;;
78 esac
79 for c in $(seq 1 $cols); do
80 echo -n "$sepchar"
81 done && echo ""
82 }
84 # New line for echo -n or gettext.
85 newline() {
86 echo ""
87 }
89 # Display a bold message. GTK Yad: Works only in --text=""
90 boldify() {
91 case $output in
92 raw) echo "$@" ;;
93 gtk) echo "<b>$@</b>" ;;
94 html) echo "<strong>$@</strong>" ;;
95 *) echo -e "\\033[1m$@\\033[0m" ;;
96 esac
97 }
99 # Usage: colorize colorNB "Message" or use --color=NB option
100 # when running a tool. Default to white/38 and no html or gtk.
101 colorize() {
102 : ${color=$1}
103 shift
104 local content="$@"
105 case $output in
106 raw|gtk|html) echo "$content" ;;
107 *)
108 [ "$color" ] || color=38
109 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
110 esac
111 unset color
112 }
114 # Indent text $1 spaces.
115 indent() {
116 local in="$1"
117 shift
118 echo -e "\033["$in"G $@";
119 }
121 # Extended MeSsaGe output
122 emsg() {
123 local sep="\n--------\n"
124 case $output in
125 raw)
126 echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \
127 s|</c>||g; s|<->|'$sep'|g; s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
128 gtk)
129 echo "$@" | sed -e 's|<c [0-9]*>||g; s|</c>||g; s|<->|'$sep'|g; \
130 s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
131 html)
132 echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
133 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
134 *)
135 local sep="\n"
136 local cols=$(get_cols)
137 [ "$cols" ] || cols=80
138 for c in $(seq 1 $cols)
139 do
140 sep="${sep}="
141 done
142 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g; \
143 s|<c \([0-9]*\)>|\\033[1;\1m|g; s|</c>|\\033[0;39m|g; s|<n>|\n|g; \
144 s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
145 [ "$1" != "-n" ] && echo
146 ;;
147 esac
148 }
150 # Check if user is logged as root.
151 check_root() {
152 if [ $(id -u) != 0 ]; then
153 lgettext "You must be root to execute:" && echo " $(basename $0) $@"
154 exit 1
155 fi
156 }
158 # Display debug info when --debug is used.
159 debug() {
160 [ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
161 }
163 # Gettextize yes/no.
164 translate_query() {
165 case $1 in
166 y) lgettext "y" ;;
167 Y) lgettext "Y" ;;
168 n) lgettext "n" ;;
169 N) lgettext "N" ;;
170 # Support other cases but keep them untranslated.
171 *) echo "$1" ;;
172 esac
173 }
175 # Usage: echo -n "The question" && confirm
176 confirm() {
177 [ "$yes" ] && true
178 echo -n " ($(translate_query y)/$(translate_query N)) ? "
179 read answer
180 [ "$answer" == "$(translate_query y)" ]
181 }
183 # Log activities. $activity should be set by the script. The log format
184 # is suitable for web interfaces like cook. Usage: log "String"
185 log() {
186 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
187 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
188 }