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

libtaz.sh: add log function (used in cooer and tazpanel actually and soon by spk)
author Christophe Lincoln <pankso@slitaz.org>
date Sat May 12 01:53:06 2012 +0200 (2012-05-12)
parents 8e29b36366d3
children 1c9fc309b4ad
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.
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 # Display a bold message. GTK Yad: Works only in --text=""
72 boldify() {
73 case $output in
74 raw) echo "$@" ;;
75 gtk) echo "<b>$@</b>" ;;
76 html) echo "<strong>$@</strong>" ;;
77 *) echo -e "\\033[1m$@\\033[0m" ;;
78 esac
79 }
81 # Indent text $1 spaces
82 indent() {
83 local in="$1"
84 shift
85 echo -e "\033["$in"G $@";
86 }
88 # Check if user is logged as root.
89 check_root() {
90 if [ $(id -u) != 0 ]; then
91 gettext "You must be root to execute:" && echo " $(basename $0) $@"
92 exit 1
93 fi
94 }
96 # Gettextize yes/no.
97 translate_query() {
98 case $1 in
99 y) gettext "y" ;;
100 Y) gettext "Y" ;;
101 n) gettext "n" ;;
102 N) gettext "N" ;;
103 # Support other cases but keep them untranslated.
104 *) echo "$1" ;;
105 esac
106 }
108 # Usage: echo -n "The question" && confirm
109 confirm() {
110 [ "$yes" ] && true
111 echo -n " ($(translate_query y)/$(translate_query N)) ? "
112 read answer
113 [ "$answer" == "$(translate_query y)" ]
114 }
116 # New line for echo -n or gettext.
117 newline() {
118 echo ""
119 }
121 # Log activities, we want first letter capitalized. $logfile should be set
122 # by the script. The log format is suitable for web interfaces like cook.
123 # Usage: echo "Message" | log
124 log() {
125 [ "$logfile" ] || logfile=/var/log/slitaz/libtaz.log
126 grep ^[A-Z] | \
127 sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" >> $logfile
128 }