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

Edit docs
author Paul Issott <paul@slitaz.org>
date Sat May 12 21:08:08 2012 +0100 (2012-05-12)
parents dd17cc10cd6d
children 1db99ad848a8
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 class='done'>$okmsg</span>"
43 error=" <span class='error'>$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 # Indent text $1 spaces
87 indent() {
88 local in="$1"
89 shift
90 echo -e "\033["$in"G $@";
91 }
93 # Check if user is logged as root.
94 check_root() {
95 if [ $(id -u) != 0 ]; then
96 gettext "You must be root to execute:" && echo " $(basename $0) $@"
97 exit 1
98 fi
99 }
101 # Gettextize yes/no.
102 translate_query() {
103 case $1 in
104 y) gettext "y" ;;
105 Y) gettext "Y" ;;
106 n) gettext "n" ;;
107 N) gettext "N" ;;
108 # Support other cases but keep them untranslated.
109 *) echo "$1" ;;
110 esac
111 }
113 # Usage: echo -n "The question" && confirm
114 confirm() {
115 [ "$yes" ] && true
116 echo -n " ($(translate_query y)/$(translate_query N)) ? "
117 read answer
118 [ "$answer" == "$(translate_query y)" ]
119 }
121 # Log activities. $activity should be set by the script. The log format
122 # is suitable for web interfaces like cook. Usage: log "String"
123 log() {
124 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
125 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
126 }