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

Update colorize to handle args easier
author Christian Mesh <meshca@clarkson.edu>
date Sat May 19 03:35:25 2012 -0500 (2012-05-19)
parents 159284129417
children c09b461ada93
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 TEXTDOMAIN='slitaz-base'
16 export TEXTDOMAIN
18 # Internal variables.
19 okmsg="$(gettext "Done")"
20 ermsg="$(gettext "Failed")"
21 : ${okcolor=32}
22 : ${ercolor=31}
23 : ${decolor=36}
25 # Parse cmdline options and store values in a variable.
26 for opt in "$@"
27 do
28 case "$opt" in
29 --*=*) export ${opt#--} ;;
30 --*) export ${opt#--}="yes" ;;
31 esac
32 done
33 [ "$HTTP_REFERER" ] && output="html"
35 # Return command status. Default to colored console output.
36 status() {
37 local check=$?
38 case $output in
39 raw|gtk)
40 done=" $okmsg"
41 error=" $ermsg" ;;
42 html)
43 done=" <span style='color: $okcolor;'>$okmsg</span>"
44 error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
45 *)
46 cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
47 local scol=$(($cols - 10))
48 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
49 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
50 esac
51 if [ $check = 0 ]; then
52 echo -e "$done"
53 else
54 echo -e "$error"
55 fi
56 }
58 # Line separator.
59 separator() {
60 local sepchar="="
61 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
62 case $output in
63 raw|gtk) local sepchar="-" && local cols="8" ;;
64 html) local sepchar="<hr />" ;;
65 *) local cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}') ;;
66 esac
67 for c in $(seq 1 $cols); do
68 echo -n "$sepchar"
69 done && echo ""
70 }
72 # New line for echo -n or gettext.
73 newline() {
74 echo ""
75 }
77 # Display a bold message. GTK Yad: Works only in --text=""
78 boldify() {
79 case $output in
80 raw) echo "$@" ;;
81 gtk) echo "<b>$@</b>" ;;
82 html) echo "<strong>$@</strong>" ;;
83 *) echo -e "\\033[1m$@\\033[0m" ;;
84 esac
85 }
87 # Usage: colorize "Message" colorNB or use --color=NB option
88 # when running a tool. Default to white/38 and no html or gtk.
89 colorize() {
90 : ${color=$1}
91 shift
92 local content="$@"
93 case $output in
94 raw|gtk|html) echo "$content" ;;
95 *)
96 [ "$color" ] || color=38
97 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
98 esac
99 unset color
100 }
102 # Indent text $1 spaces.
103 indent() {
104 local in="$1"
105 shift
106 echo -e "\033["$in"G $@";
107 }
109 # Check if user is logged as root.
110 check_root() {
111 if [ $(id -u) != 0 ]; then
112 gettext "You must be root to execute:" && echo " $(basename $0) $@"
113 exit 1
114 fi
115 }
117 # Display debug info when --debug is used.
118 debug() {
119 [ "$debug" ] && echo "$(colorize "DEBUG:" $decolor) $1"
120 }
122 # Gettextize yes/no.
123 translate_query() {
124 case $1 in
125 y) gettext "y" ;;
126 Y) gettext "Y" ;;
127 n) gettext "n" ;;
128 N) gettext "N" ;;
129 # Support other cases but keep them untranslated.
130 *) echo "$1" ;;
131 esac
132 }
134 # Usage: echo -n "The question" && confirm
135 confirm() {
136 [ "$yes" ] && true
137 echo -n " ($(translate_query y)/$(translate_query N)) ? "
138 read answer
139 [ "$answer" == "$(translate_query y)" ]
140 }
142 # Log activities. $activity should be set by the script. The log format
143 # is suitable for web interfaces like cook. Usage: log "String"
144 log() {
145 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
146 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
147 }