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

Add newline function to libtaz to make code easier to read
author Christian Mesh <meshca@clarkson.edu>
date Fri May 11 18:53:55 2012 -0500 (2012-05-11)
parents 71fb195c4f3e
children 8e29b36366d3
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 yes_no() {
97 [ "$autoyes" ] && true
98 echo -n " ($(translate_query y)/$(translate_query N)) ? "
99 read answer
100 [ "$answer" == "$(translate_query y)" ]
101 }
103 translate_query() {
104 case $1 in
105 y) gettext "y" ;;
106 Y) gettext "Y" ;;
107 n) gettext "n" ;;
108 N) gettext "N" ;;
109 # Support other cases but keep them untranslated.
110 *) echo "$1" ;;
111 esac
112 }
114 newline () {
115 echo
116 }