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

libpkg.sh: unset TAGS too in unset_receipt(); and code reformatting
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Nov 27 16:26:29 2014 +0200 (2014-11-27)
parents 02334b7ae2f7
children d3bc87df0f19
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-2014 SliTaz GNU/Linux - BSD License
11 #
13 . /usr/bin/gettext.sh
15 # short names for common i18n functions (like 'echo' and 'echo -n')
16 _() { eval_gettext "$@"; echo; }
17 _n() { eval_gettext "$@"; }
19 # internal i18n
20 lgettext() { gettext -d 'slitaz-base' "$@"; }
22 # Internal variables.
23 okmsg="$(lgettext 'Done')"
24 ermsg="$(lgettext 'Failed')"
25 : ${okcolor=32}
26 : ${ercolor=31}
27 : ${decolor=36}
29 # Parse cmdline options and store values in a variable.
30 for opt in "$@"
31 do
32 case "$opt" in
33 --*=*) export "${opt#--}" ;;
34 --*) export ${opt#--}="yes" ;;
35 esac
36 done
37 [ "$HTTP_REFERER" ] && output="html"
39 # Get terminal columns
40 get_cols() {
41 stty size 2>/dev/null | busybox cut -d " " -f 2
42 }
44 # Return command status. Default to colored console output.
45 status() {
46 local check=$?
47 case $output in
48 raw|gtk)
49 done=" $okmsg"
50 error=" $ermsg" ;;
51 html)
52 done=" <span style='color: $okcolor;'>$okmsg</span>"
53 error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
54 *)
55 local cols=$(get_cols)
56 [ "$cols" ] || cols=80
57 local scol=$(($cols - 10))
58 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
59 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
60 esac
61 if [ $check = 0 ]; then
62 echo -e "$done"
63 else
64 echo -e "$error"
65 fi
66 }
68 # Line separator.
69 separator() {
70 local sepchar="="
71 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
72 case $output in
73 raw|gtk) local sepchar="-"; local cols="8" ;;
74 html) local sepchar="<hr />" ;;
75 *)
76 local cols=$(get_cols)
77 [ "$cols" ] || cols=80 ;;
78 esac
79 for c in $(seq 1 $cols); do
80 echo -n "$sepchar"
81 done && echo ""
82 }
84 # New line for echo -n or gettext.
85 newline() {
86 echo ""
87 }
89 # Display a bold message. GTK Yad: Works only in --text=""
90 boldify() {
91 case $output in
92 raw) echo "$@" ;;
93 gtk) echo "<b>$@</b>" ;;
94 html) echo "<strong>$@</strong>" ;;
95 *) echo -e "\\033[1m$@\\033[0m" ;;
96 esac
97 }
99 # Usage: colorize colorNB "Message" or use --color=NB option
100 # when running a tool. Default to white/38 and no html or gtk.
101 colorize() {
102 : ${color=$1}
103 shift
104 local content="$@"
105 case $output in
106 raw|gtk|html) echo "$content" ;;
107 *)
108 [ "$color" ] || color=38
109 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
110 esac
111 unset color
112 }
114 # Indent text $1 spaces.
115 indent() {
116 local in="$1"
117 shift
118 echo -e "\033["$in"G $@";
119 }
121 # Extended MeSsaGe output
122 emsg() {
123 local sep="\n--------\n"
124 case $output in
125 raw)
126 echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \
127 s|</c>||g; s|<->|'$sep'|g; s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
128 gtk)
129 echo "$@" | sed -e 's|<c [0-9]*>||g; s|</c>||g; s|<->|'$sep'|g; \
130 s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
131 html)
132 echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
133 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
134 *)
135 local sep="\n"
136 local cols=$(get_cols)
137 [ "$cols" ] || cols=80
138 for c in $(seq 1 $cols); do
139 sep="${sep}="
140 done
141 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g; \
142 s|<c \([0-9]*\)>|\\033[1;\1m|g; s|</c>|\\033[0;39m|g; s|<n>|\n|g; \
143 s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
144 [ "$1" != "-n" ] && echo
145 ;;
146 esac
147 }
149 # Check if user is logged as root.
150 check_root() {
151 if [ $(id -u) != 0 ]; then
152 lgettext "You must be root to execute:"; echo " $(basename $0) $@"
153 exit 1
154 fi
155 }
157 # Display debug info when --debug is used.
158 debug() {
159 [ "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
160 }
162 # Gettextize yes/no.
163 translate_query() {
164 case $1 in
165 y) lgettext "y" ;;
166 Y) lgettext "Y" ;;
167 n) lgettext "n" ;;
168 N) lgettext "N" ;;
169 # Support other cases but keep them untranslated.
170 *) echo "$1" ;;
171 esac
172 }
174 # Usage: echo -n "The question" && confirm
175 confirm() {
176 [ "$yes" ] && true
177 echo -n " ($(translate_query y)/$(translate_query N)) ? "
178 read answer
179 [ "$answer" == "$(translate_query y)" ]
180 }
182 # Log activities. $activity should be set by the script. The log format
183 # is suitable for web interfaces like cook. Usage: log "String"
184 log() {
185 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
186 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
187 }
189 # Sophisticated function to print two-column list of options with descriptions
190 # Be UTF-8 friendly, not use `wc -L`, `awk length`, `${#string}`
191 optlist() {
192 local in cols col1=1 line
193 in="$(echo "$1" | sed 's| *| |g')"
194 cols=$(get_cols); [ "$cols" ] || cols=80
195 IFS=$'\n'
196 for line in $in; do
197 col=$(echo -n "$line" | cut -f1 | wc -m)
198 [ $col -gt $col1 ] && col1=$col
199 done
200 echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \
201 sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}"
202 }
204 # Wrap words in long terminal message
205 longline() {
206 cols=$(get_cols); [ "$cols" ] || cols=80
207 echo -e "$@" | fold -sw$cols
208 }