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

libtaz.sh: do not fail processing --options-with-dashes or --weird%option$. Add custom tag "x-details" support to slitaz-doc.{css,js}
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Jun 16 20:47:25 2015 +0300 (2015-06-16)
parents 3f16616140ea
children 3ce00b87e88e
line source
1 #!/bin/sh
2 #
3 # SliTaz Base functions.
4 # Documentation: `man libtaz` or /usr/share/doc/slitaz/libtaz.txt
5 #
6 # Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License
7 #
9 . /usr/bin/gettext.sh
11 # Internal
12 lgettext() { gettext -d 'slitaz-base' "$@"; }
13 translate_query() {
14 case $1 in
15 y) lgettext "y";;
16 Y) lgettext "Y";;
17 n) lgettext "n";;
18 N) lgettext "N";;
19 # Support other cases but keep them untranslated.
20 *) echo "$1" ;;
21 esac
22 }
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 "$@"; do
31 opt_name="${opt%%=*}"; opt_name="$(echo "${opt_name#--}" | tr -c 'a-zA-Z0-9' '_')"
32 case "$opt_name" in
33 [0-9]*) opt_name="_$opt_name";;
34 esac
35 opt_value="${opt#*=}"; opt_value="${opt_value:-yes}"
36 export "$opt_name=$opt_value"
37 done
38 [ "$HTTP_REFERER" ] && output='html'
43 # i18n functions
44 _() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
45 _n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
46 _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; }
48 # Get terminal columns
49 get_cols() { stty size 2>/dev/null | busybox cut -d' ' -f2; }
51 # Last command status
52 status() {
53 local check=$?
54 case $output in
55 raw|gtk)
56 done=" $okmsg"
57 error=" $ermsg";;
58 html)
59 done=" <span class=\"float-right color$okcolor\">$okmsg</span>"
60 error=" <span class=\"float-right color$ercolor\">$ermsg</span>";;
61 *)
62 local cols=$(get_cols)
63 local scol=$((${cols:-80} - 10))
64 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
65 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]";;
66 esac
67 case $check in
68 0) echo -e "$done";;
69 *) echo -e "$error";;
70 esac
71 }
73 # Line separator
74 separator() {
75 case $output in
76 raw|gtk) echo '--------';;
77 html) echo -n '<hr/>';;
78 *)
79 local cols=$(get_cols)
80 printf "%${cols:-80}s\n" | tr ' ' "${1:-=}";;
81 esac
82 }
84 # New line
85 newline() { echo; }
87 # Display a bold message
88 boldify() {
89 case $output in
90 raw) echo "$@" ;;
91 gtk) echo "<b>$@</b>" ;;
92 html) echo "<strong>$@</strong>" ;;
93 *) echo -e "\\033[1m$@\\033[0m" ;;
94 esac
95 }
97 # Colorize message
98 colorize() {
99 : ${color=$1}
100 shift
101 case $output in
102 raw|gtk) echo "$@";;
103 html) echo -n "<span class=\"color$color\">$@</span>";;
104 *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;;
105 esac
106 unset color
107 }
109 # Indent text
110 indent() {
111 local in="$1"
112 shift
113 echo -e "\033["$in"G $@";
114 }
116 # Extended MeSsaGe output
117 emsg() {
118 local sep="\n--------\n"
119 case $output in
120 raw)
121 echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \
122 s|</c>||g; s|<->|'$sep'|g; s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
123 gtk)
124 echo "$@" | sed -e 's|<c [0-9]*>||g; s|</c>||g; s|<->|'$sep'|g; \
125 s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
126 html)
127 echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
128 s|<c \([0-9]*\)>|<span class="color\1">|g; s|</c>|</span>|g; \
129 s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
130 *)
131 local sep="\n"
132 local cols=$(get_cols)
133 [ "$cols" ] || cols=80
134 for c in $(seq 1 $cols); do
135 sep="${sep}="
136 done
137 echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g;
138 s|<c \([0-9]*\)>|\\033[1;\1m|g; s|</c>|\\033[0;39m|g; s|<n>|\n|g;
139 s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
140 [ "$1" != "-n" ] && echo
141 ;;
142 esac
143 }
145 # Check if user is logged as root
146 check_root() {
147 if [ $(id -u) != 0 ]; then
148 lgettext "You must be root to execute:"; echo " $(basename $0) $@"
149 exit 1
150 fi
151 }
153 # Display debug info when --debug is used.
154 debug() {
155 [ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
156 }
158 # Confirmation
159 confirm() {
160 if [ -n "$yes" ]; then
161 true
162 else
163 if [ -n "$1" ]; then
164 echo -n "$1 "
165 else
166 echo -n " ($(translate_query y)/$(translate_query N)) ? "
167 fi
168 read answer
169 [ "$answer" == "$(translate_query y)" ]
170 fi
171 }
173 # Log activities
174 log() {
175 echo "$(date '+%F %R') : $@" >> ${activity:-/var/log/slitaz/libtaz.log}
176 }
178 # Print two-column list of options with descriptions
179 optlist() {
180 local in cols col1=1 line
181 in="$(echo "$1" | sed 's| *| |g')"
182 cols=$(get_cols); [ "$cols" ] || cols=80
183 IFS=$'\n'
184 for line in $in; do
185 col=$(echo -n "$line" | cut -f1 | wc -m)
186 [ $col -gt $col1 ] && col1=$col
187 done
188 echo "$in" | sed 's|\t|&\n|' | fold -sw$((cols - col1 - 4)) | \
189 sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}"
190 }
192 # Wrap words in long terminal message
193 longline() {
194 cols=$(get_cols)
195 echo -e "$@" | fold -sw${cols:-80}
196 }
198 # Print localized title
199 title() {
200 case $output in
201 html) echo "<section><header>$(_ "$@")</header><pre class=\"scroll\">";;
202 *) newline; boldify "$(_ "$@")"; separator;;
203 esac
204 }
206 # Print footer
207 footer() {
208 case $output in
209 html) echo "</pre><footer>$1</footer></section>";;
210 *) separator; echo "$1"; [ -n "$1" ] && newline;;
211 esac
212 }
214 # Print current action
215 action() {
216 case $output in
217 raw|gtk|html) _n "$@";;
218 *) echo -ne "\033[0;33m"$(_ "$@")"\033[0m";;
219 esac
220 }
222 # Print long line as list item
223 itemize() {
224 local inp="$@" cols=$(get_cols) first offset
225 cols="${cols:-80}"
226 first="$(echo -e "$inp" | fold -sw$cols | head -n1)"
227 echo "$first"
228 cols1="$(echo "${first:1}" | wc -c)"
229 offset=$(echo "$first" | sed -n 's|^\([^:\*-]*[:\*-]\).*$|\1|p' | wc -m)
230 echo "${inp:$cols1}" | fold -sw$((cols - offset)) | awk \
231 '($0){printf "%'$offset's%s\n","",$0}'
232 }