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

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