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

Add /lib/libtaz.sh
author Christophe Lincoln <pankso@slitaz.org>
date Thu Apr 12 21:53:58 2012 +0200 (2012-04-12)
parents
children 32c560235098
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 done=" $okmsg"
32 error=" $ermsg" ;;
33 --gtk-out)
34 # Yad/GTK TextView bold or colored text ?
35 output="gtk"
36 done=" $okmsg"
37 error=" $ermsg" ;;
38 --html-out)
39 output="html"
40 done=" <span class='done'>$okmsg</span>"
41 error=" <span class='error'>$ermsg</span>" ;;
42 esac
43 done
45 # Help and usage.
46 libtaz() {
47 cat << EOT
49 Include this library in a script:
50 . /usr/lib/slitaz/libtaz.sh
52 Functions:
53 status
54 separator
55 boldify string
56 check_root
58 Options:
59 --raw-out
60 --gtk-out
61 --html-out
63 EOT
64 }
66 # Return command status. Default to colored console output.
67 status() {
68 local check=$?
69 if [ ! "$output" ]; then
70 local cols=$(stty -a | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
71 local scol=$(($cols - 10))
72 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
73 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]"
74 fi
75 if [ $check = 0 ]; then
76 echo -e "$done"
77 else
78 echo -e "$error"
79 fi
80 }
82 # Line separator.
83 separator() {
84 local cols=$(stty -a | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
85 for c in $(seq 1 $cols); do
86 echo -n "="
87 done && echo ""
88 }
90 # Display a bold message. GTK Yad: Works only in --text=""
91 boldify() {
92 case $output in
93 raw) echo "$1" ;;
94 gtk) echo "<b>$1</b>" ;;
95 html) echo "<strong>$1</strong>" ;;
96 *) echo -e "\\033[1m${1}\\033[0m" ;;
97 esac
98 }
100 # Check if user is logged as root.
101 check_root() {
102 if [ $(id -u) != 0 ]; then
103 gettext "You must be root to execute:" && echo " $(basename $0) $@"
104 exit 1
105 fi
106 }