seb view libseb.sh @ rev 32

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Feb 27 17:54:18 2019 +0100 (2019-02-27)
parents 33333de28e73
children
line source
1 #!/bin/sh
2 #
3 # libseb.sh: Simple functions without gettext for seb cmdline tools
4 #
5 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
6 #
8 # Parse cmdline options and store values in a variable.
9 for opt in "$@"; do
10 opt_name="${opt%%=*}"
11 opt_name="$(echo -n "${opt_name#--}" | tr -c 'a-zA-Z0-9' '_')"
12 case "$opt" in
13 --*=*) export $opt_name="${opt#*=}" ;;
14 --*) export $opt_name="yes" ;;
15 esac
16 done
18 # Exit if command returns 1
19 check() {
20 local code="$?"
21 case "$output" in
22 html)
23 case "$code" in
24 0) echo " <span style='color: green;'>Done</span>" ;;
25 1)
26 echo " <span style='color: red;'>--> Error</span>"
27 exit 1 ;;
28 esac ;;
29 *)
30 case "$code" in
31 0) info 32 "Done" ;;
32 1) colorize 31 "--> Error" && exit 1 ;;
33 esac ;;
34 esac
35 }
37 # Check if user is logged as root
38 check_root() {
39 if [ $(id -u) -ne 0 ]; then
40 echo "You must be root to execute: $(basename $0) $@"; exit 1
41 fi
42 }
44 # Get console cols
45 get_cols() {
46 if ! stty size | cut -d " " -f 2; then
47 echo 80
48 fi
49 }
51 # Indent text
52 indent() {
53 local in="$1"
54 shift
55 case "$output" in
56 html) echo ": $@" ;;
57 *) echo -e "\033["${in}"G $@" ;;
58 esac
59 }
61 # Display a bold message
62 boldify() {
63 case "$output" in
64 html) echo "<strong>$@</strong>" ;;
65 *) echo -e "\\033[1m$@\\033[0m" ;;
66 esac
67 }
69 # Colorize message
70 colorize() {
71 : ${color=$1}
72 shift
73 case "$output" in
74 html|raw) echo "$@" ;;
75 *)
76 case "$color" in
77 0*) echo -e "\\033[${color:-38}m$@\\033[39m" ;;
78 *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;;
79 esac ;;
80 esac; unset color
81 }
83 # Last command status
84 status() {
85 local code="$?"
86 case "$output" in
87 html)
88 case "$code" in
89 0) echo " <span style='color: green;'>Done</span>" ;;
90 1) echo " <span style='color: red;'>Fail</span>" ;;
91 esac ;;
92 *)
93 case "$code" in
94 0) info 32 "Done" ;;
95 1) info 31 "Fail" ;;
96 esac ;;
97 esac
98 }
100 # Print info a la status way: info [color] [content]
101 info() {
102 local info="$2"
103 case "$output" in
104 html) echo " <span class='info'>$info</span>" ;;
105 *)
106 local char="$(echo $info | wc -L)"
107 local in=$((7 + ${char}))
108 indent $(($(get_cols) - ${in})) "[ $(colorize $1 $info) ]" ;;
109 esac
110 }
112 # Line separator
113 separator() {
114 case "$output" in
115 html) echo -n '<hr />' ;;
116 *) printf "%$(get_cols)s\n" | tr ' ' "${1:-=}" ;;
117 esac
118 }
120 title() {
121 echo ""; colorize 33 "$@"; separator
122 }
124 footer() {
125 separator "-"; [ "$1" ] && colorize 035 "$1"; echo ""
126 }
128 # Testsuite
129 if [ $(basename $0) = "libseb.sh" ]; then
130 title "libseb.sh title()"
131 echo -n "Checking status() 0"; status
132 echo -n "Checking status() 1"; ls /a/a 2>/dev/null; status
133 echo -n "Checking info()"; info 035 "3.4K"
134 echo -n "Checking colorize()"; colorize 33 " message"
135 echo -n "Checking colorize()"; colorize 033 " message"
136 echo "Checking footer()"; footer "Footer message"
137 fi