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

libtaz.sh: use /dev/stdin and improve testsuite.sh
author Christophe Lincoln <pankso@slitaz.org>
date Tue Apr 17 01:03:14 2012 +0200 (2012-04-17)
parents fdcc019189d9
children 7dd241a1171b
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, function must be used by at least 3-4 tools. See
7 # libtaz() for a list of functions and options or run: tazdev libtaz.sh
8 # Libtaz is located in /lib/libtaz.sh since it is used when /usr may not
9 # be mounted.
10 #
11 # Copyright (C) 2012 SliTaz GNU/Linux - BSD License
12 #
14 # Internationalization.
15 . /usr/bin/gettext.sh
16 TEXTDOMAIN='slitaz-base'
17 export TEXTDOMAIN
19 # Internal variables.
20 okmsg="$(gettext "Done")"
21 ermsg="$(gettext "Failed")"
22 okcolor=32
23 ercolor=31
25 # Parse cmdline options.
26 for opt in "$@"
27 do
28 case "$opt" in
29 --raw-out)
30 output="raw" ;;
31 --gtk-out)
32 output="gtk" ;;
33 --html-out)
34 output="html" ;;
35 esac
36 done
37 [ "$HTTP_REFERER" ] && output="html"
39 # Help and usage.
40 libtaz() {
41 cat << EOT
43 Include this library in a script:
44 . /lib/libtaz.sh
46 Functions:
47 status
48 separator
49 boldify string
50 check_root
52 Options:
53 --raw-out
54 --gtk-out
55 --html-out
57 EOT
58 }
60 # Return command status. Default to colored console output.
61 status() {
62 local check=$?
63 case $output in
64 raw|gtk)
65 done=" $okmsg"
66 error=" $ermsg" ;;
67 html)
68 done=" <span class='done'>$okmsg</span>"
69 error=" <span class='error'>$ermsg</span>" ;;
70 *)
71 cols=$(stty -a -F /dev/stdin | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
72 local scol=$(($cols - 10))
73 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
74 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
75 esac
76 if [ $check = 0 ]; then
77 echo -e "$done"
78 else
79 echo -e "$error"
80 fi
81 }
83 # Line separator.
84 separator() {
85 local sepchar="="
86 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
87 case $output in
88 raw|gtk) local sepchar="-" && local cols="8" ;;
89 html) local sepchar="<hr />" ;;
90 *) local cols=$(stty -a -F /dev/stdin | head -n 1 | cut -d ";" -f 3 | awk '{print $2}') ;;
91 esac
92 for c in $(seq 1 $cols); do
93 echo -n "$sepchar"
94 done && echo ""
95 }
97 # Display a bold message. GTK Yad: Works only in --text=""
98 boldify() {
99 case $output in
100 raw) echo "$1" ;;
101 gtk) echo "<b>$1</b>" ;;
102 html) echo "<strong>$1</strong>" ;;
103 *) echo -e "\\033[1m${1}\\033[0m" ;;
104 esac
105 }
107 # Check if user is logged as root.
108 check_root() {
109 if [ $(id -u) != 0 ]; then
110 gettext "You must be root to execute:" && echo " $(basename $0) $@"
111 exit 1
112 fi
113 }