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

libtaz.sh: cols should be fixes now
author Christophe Lincoln <pankso@slitaz.org>
date Sat May 26 00:37:54 2012 +0200 (2012-05-26)
parents d8adb319ac56
children 0933e039429b
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 -a 2>/dev/null | head -n 1 | cut -d ";" -f 3 | awk '{print $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 # Better to keep messages unsplitted
102 # Example: unboldify "My <b>pretty</b> function ;)"
103 unboldify() {
104 case $output in
105 raw) echo "$@" | sed -e 's|<b>||g;s|</b>||g' ;;
106 gtk) echo "$@" ;;
107 html) echo "$@" | sed -e 's|<b>|<strong>|g;s|</b>|</strong>|g' ;;
108 *) echo -e "$(echo "$@" | sed -e 's|<b>|\\033[1m|g;s|</b>|\\033[0m|g')" ;;
109 esac
110 }
112 # Usage: colorize "Message" colorNB or use --color=NB option
113 # when running a tool. Default to white/38 and no html or gtk.
114 colorize() {
115 : ${color=$1}
116 shift
117 local content="$@"
118 case $output in
119 raw|gtk|html) echo "$content" ;;
120 *)
121 [ "$color" ] || color=38
122 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
123 esac
124 unset color
125 }
127 # Indent text $1 spaces.
128 indent() {
129 local in="$1"
130 shift
131 echo -e "\033["$in"G $@";
132 }
134 # Check if user is logged as root.
135 check_root() {
136 if [ $(id -u) != 0 ]; then
137 lgettext "You must be root to execute:" && echo " $(basename $0) $@"
138 exit 1
139 fi
140 }
142 # Display debug info when --debug is used.
143 debug() {
144 [ "$debug" ] && echo "$(colorize $decolor "DEBUG:") $1"
145 }
147 # Gettextize yes/no.
148 translate_query() {
149 case $1 in
150 y) lgettext "y" ;;
151 Y) lgettext "Y" ;;
152 n) lgettext "n" ;;
153 N) lgettext "N" ;;
154 # Support other cases but keep them untranslated.
155 *) echo "$1" ;;
156 esac
157 }
159 # Usage: echo -n "The question" && confirm
160 confirm() {
161 [ "$yes" ] && true
162 echo -n " ($(translate_query y)/$(translate_query N)) ? "
163 read answer
164 [ "$answer" == "$(translate_query y)" ]
165 }
167 # Log activities. $activity should be set by the script. The log format
168 # is suitable for web interfaces like cook. Usage: log "String"
169 log() {
170 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
171 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
172 }