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

New function unboldify(); I think, they will be handy
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon May 21 11:34:18 2012 +0300 (2012-05-21)
parents efb8e0291f24
children a98c41a45fa4
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.
14 . /usr/bin/gettext.sh
15 # We can't export TEXTDOMAIN because this script includes to other scripts
16 # with other TEXTDOMAIN exported
17 ## TEXTDOMAIN='slitaz-base'
18 ## export TEXTDOMAIN
20 # xgettext (from Makefile) can't extract strings from above example:
21 # gettext -d 'slitaz-base' 'Done'
22 # so, I define own function (and add it as option to xgettext to Makefile)
23 lgettext() {
24 gettext -d 'slitaz-base' $1
25 }
27 # Internal variables.
28 okmsg="$(lgettext 'Done')"
29 ermsg="$(lgettext 'Failed')"
30 : ${okcolor=32}
31 : ${ercolor=31}
32 : ${decolor=36}
34 # Parse cmdline options and store values in a variable.
35 for opt in "$@"
36 do
37 case "$opt" in
38 --*=*) export ${opt#--} ;;
39 --*) export ${opt#--}="yes" ;;
40 esac
41 done
42 [ "$HTTP_REFERER" ] && output="html"
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 cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
56 local scol=$(($cols - 10))
57 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
58 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
59 esac
60 if [ $check = 0 ]; then
61 echo -e "$done"
62 else
63 echo -e "$error"
64 fi
65 }
67 # Line separator.
68 separator() {
69 local sepchar="="
70 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
71 case $output in
72 raw|gtk) local sepchar="-" && local cols="8" ;;
73 html) local sepchar="<hr />" ;;
74 *) local cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}') ;;
75 esac
76 for c in $(seq 1 $cols); do
77 echo -n "$sepchar"
78 done && echo ""
79 }
81 # New line for echo -n or gettext.
82 newline() {
83 echo ""
84 }
86 # Display a bold message. GTK Yad: Works only in --text=""
87 boldify() {
88 case $output in
89 raw) echo "$@" ;;
90 gtk) echo "<b>$@</b>" ;;
91 html) echo "<strong>$@</strong>" ;;
92 *) echo -e "\\033[1m$@\\033[0m" ;;
93 esac
94 }
96 # Better to keep messages unsplitted
97 # Example: unboldify "My <b>pretty</b> function ;)"
98 unboldify() {
99 case $output in
100 raw) echo "$@" | sed -e 's|<b>||g;s|</b>||g' ;;
101 gtk) echo "$@" ;;
102 html) echo "$@" | sed -e 's|<b>|<strong>|g;s|</b>|</strong>|g' ;;
103 *) echo -e "$(echo "$@" | sed -e 's|<b>|\\033[1m|g;s|</b>|\\033[0m|g')" ;;
104 esac
105 }
107 # Usage: colorize "Message" colorNB or use --color=NB option
108 # when running a tool. Default to white/38 and no html or gtk.
109 colorize() {
110 : ${color=$1}
111 shift
112 local content="$@"
113 case $output in
114 raw|gtk|html) echo "$content" ;;
115 *)
116 [ "$color" ] || color=38
117 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
118 esac
119 unset color
120 }
122 # Indent text $1 spaces.
123 indent() {
124 local in="$1"
125 shift
126 echo -e "\033["$in"G $@";
127 }
129 # Check if user is logged as root.
130 check_root() {
131 if [ $(id -u) != 0 ]; then
132 lgettext "You must be root to execute:" && echo " $(basename $0) $@"
133 exit 1
134 fi
135 }
137 # Display debug info when --debug is used.
138 debug() {
139 [ "$debug" ] && echo "$(colorize $decolor "DEBUG:") $1"
140 }
142 # Gettextize yes/no.
143 translate_query() {
144 case $1 in
145 y) lgettext "y" ;;
146 Y) lgettext "Y" ;;
147 n) lgettext "n" ;;
148 N) lgettext "N" ;;
149 # Support other cases but keep them untranslated.
150 *) echo "$1" ;;
151 esac
152 }
154 # Usage: echo -n "The question" && confirm
155 confirm() {
156 [ "$yes" ] && true
157 echo -n " ($(translate_query y)/$(translate_query N)) ? "
158 read answer
159 [ "$answer" == "$(translate_query y)" ]
160 }
162 # Log activities. $activity should be set by the script. The log format
163 # is suitable for web interfaces like cook. Usage: log "String"
164 log() {
165 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
166 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
167 }