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

libtaz.sh: use /dev/ttx to get term size
author Christophe Lincoln <pankso@slitaz.org>
date Tue Apr 24 14:26:47 2012 +0200 (2012-04-24)
parents 524eab3120ae
children 628884aa895d
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 "$1" ;;
75 gtk) echo "<b>$1</b>" ;;
76 html) echo "<strong>$1</strong>" ;;
77 *) echo -e "\\033[1m${1}\\033[0m" ;;
78 esac
79 }
81 # Check if user is logged as root.
82 check_root() {
83 if [ $(id -u) != 0 ]; then
84 gettext "You must be root to execute:" && echo " $(basename $0) $@"
85 exit 1
86 fi
87 }