slitaz-base-files annotate rootfs/lib/libtaz.sh @ rev 174

Colorize DEBUG and let use --decolor=NB
author Christophe Lincoln <pankso@slitaz.org>
date Thu May 17 14:53:51 2012 +0200 (2012-05-17)
parents 076cd48b7ee8
children fa75f9c09dd8
rev   line source
pankso@125 1 #!/bin/sh
pankso@125 2 #
pankso@125 3 # SliTaz Base functions used from boot scripts to end user tools. Use
pankso@125 4 # gettext and not echo for messages. Keep output suitable for GTK boxes
pankso@125 5 # and Ncurses dialog. LibTaz should not depend on any configuration file.
paul@166 6 # No bloated code here, functions must be used by at least 3-4 tools.
pankso@139 7 #
pankso@139 8 # Documentation: man libtaz or /usr/share/doc/slitaz/libtaz.txt
pankso@125 9 #
pankso@125 10 # Copyright (C) 2012 SliTaz GNU/Linux - BSD License
pankso@125 11 #
pankso@125 12
pankso@125 13 # Internationalization.
pankso@125 14 . /usr/bin/gettext.sh
pankso@125 15 TEXTDOMAIN='slitaz-base'
pankso@125 16 export TEXTDOMAIN
pankso@125 17
pankso@125 18 # Internal variables.
pankso@125 19 okmsg="$(gettext "Done")"
pankso@125 20 ermsg="$(gettext "Failed")"
pankso@174 21 : ${okcolor=32}
pankso@174 22 : ${ercolor=31}
pankso@174 23 : ${decolor=36}
pankso@125 24
pankso@137 25 # Parse cmdline options and store values in a variable.
pankso@125 26 for opt in "$@"
pankso@125 27 do
pankso@125 28 case "$opt" in
pankso@137 29 --*=*) export ${opt#--} ;;
pankso@137 30 --*) export ${opt#--}="yes" ;;
pankso@125 31 esac
pankso@125 32 done
pankso@131 33 [ "$HTTP_REFERER" ] && output="html"
pankso@125 34
pankso@125 35 # Return command status. Default to colored console output.
pankso@125 36 status() {
pankso@125 37 local check=$?
pankso@131 38 case $output in
pankso@161 39 raw|gtk)
pankso@161 40 done=" $okmsg"
pankso@131 41 error=" $ermsg" ;;
pankso@131 42 html)
pankso@167 43 done=" <span style='color: $okcolor;'>$okmsg</span>"
pankso@167 44 error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
pankso@136 45 *)
pankso@141 46 cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
pankso@131 47 local scol=$(($cols - 10))
pankso@131 48 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
pankso@131 49 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
pankso@131 50 esac
pankso@125 51 if [ $check = 0 ]; then
pankso@125 52 echo -e "$done"
pankso@125 53 else
pankso@125 54 echo -e "$error"
pankso@125 55 fi
pankso@125 56 }
pankso@125 57
pankso@125 58 # Line separator.
pankso@125 59 separator() {
pankso@132 60 local sepchar="="
pankso@132 61 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
pankso@132 62 case $output in
pankso@132 63 raw|gtk) local sepchar="-" && local cols="8" ;;
pankso@132 64 html) local sepchar="<hr />" ;;
pankso@141 65 *) local cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}') ;;
pankso@132 66 esac
pankso@125 67 for c in $(seq 1 $cols); do
pankso@131 68 echo -n "$sepchar"
pankso@125 69 done && echo ""
pankso@125 70 }
pankso@125 71
pankso@164 72 # New line for echo -n or gettext.
pankso@164 73 newline() {
pankso@164 74 echo ""
pankso@164 75 }
pankso@164 76
pankso@125 77 # Display a bold message. GTK Yad: Works only in --text=""
pankso@125 78 boldify() {
pankso@125 79 case $output in
meshca@149 80 raw) echo "$@" ;;
meshca@149 81 gtk) echo "<b>$@</b>" ;;
meshca@149 82 html) echo "<strong>$@</strong>" ;;
meshca@149 83 *) echo -e "\\033[1m$@\\033[0m" ;;
pankso@125 84 esac
pankso@125 85 }
pankso@125 86
pankso@167 87 # Usage: colorize "Message" colorNB or use --color=NB option
pankso@167 88 # when running a tool. Default to white/38 and no html or gtk.
pankso@167 89 colorize() {
pankso@167 90 : ${color=$2}
pankso@167 91 case $output in
pankso@167 92 raw|gtk|html) echo "$1" ;;
pankso@167 93 *)
pankso@168 94 [ "$color" ] || color=38
pankso@167 95 echo -e "\\033[1;${color}m${1}\\033[0;39m" ;;
pankso@167 96 esac
pankso@167 97 unset color
pankso@167 98 }
pankso@167 99
pankso@174 100 # Indent text $1 spaces.
pankso@161 101 indent() {
meshca@158 102 local in="$1"
meshca@158 103 shift
pankso@161 104 echo -e "\033["$in"G $@";
meshca@158 105 }
meshca@158 106
pankso@125 107 # Check if user is logged as root.
pankso@125 108 check_root() {
pankso@125 109 if [ $(id -u) != 0 ]; then
pankso@125 110 gettext "You must be root to execute:" && echo " $(basename $0) $@"
pankso@125 111 exit 1
pankso@125 112 fi
pankso@125 113 }
meshca@158 114
pankso@174 115 # Display debug info when --debug is used.
meshca@169 116 debug() {
pankso@174 117 [ "$debug" ] && echo "$(colorize "DEBUG:" $decolor) $1"
meshca@169 118 }
meshca@169 119
pankso@161 120 # Gettextize yes/no.
meshca@158 121 translate_query() {
meshca@158 122 case $1 in
meshca@158 123 y) gettext "y" ;;
meshca@158 124 Y) gettext "Y" ;;
meshca@158 125 n) gettext "n" ;;
meshca@158 126 N) gettext "N" ;;
meshca@158 127 # Support other cases but keep them untranslated.
meshca@158 128 *) echo "$1" ;;
meshca@158 129 esac
meshca@158 130 }
meshca@160 131
pankso@161 132 # Usage: echo -n "The question" && confirm
pankso@161 133 confirm() {
pankso@161 134 [ "$yes" ] && true
pankso@161 135 echo -n " ($(translate_query y)/$(translate_query N)) ? "
pankso@161 136 read answer
pankso@161 137 [ "$answer" == "$(translate_query y)" ]
meshca@160 138 }
pankso@161 139
pankso@164 140 # Log activities. $activity should be set by the script. The log format
pankso@164 141 # is suitable for web interfaces like cook. Usage: log "String"
pankso@162 142 log() {
pankso@163 143 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
pankso@164 144 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
pankso@162 145 }