seb view libseb.sh @ rev 4

Add /lib/libseb.sh (a libtaz.sh version witout i18n)
author Christophe Lincoln <pankso@slitaz.org>
date Mon Mar 06 16:40:46 2017 +0100 (2017-03-06)
parents
children 052432697dd3
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 ruturn 1
19 check() {
20 case "$?" in
21 0) info 32 "Done" ;;
22 1) colorize 31 "--> Error" && exit 1 ;;
23 esac
24 }
26 # Check if user is logged as root
27 check_root() {
28 if [ $(id -u) -ne 0 ]; then
29 echo "You must be root to execute: $(basename $0) $@"; exit 1
30 fi
31 }
33 # Get console cols
34 get_cols() {
35 if ! stty size | cut -d " " -f 2; then
36 echo 80
37 fi
38 }
40 # Indent text
41 indent() {
42 local in="$1"
43 shift
44 echo -e "\033["${in}"G $@";
45 }
47 # Display a bold message
48 boldify() {
49 case "$output" in
50 html) echo "<strong>$@</strong>" ;;
51 *) echo -e "\\033[1m$@\\033[0m" ;;
52 esac
53 }
55 # Colorize message
56 colorize() {
57 : ${color=$1}
58 shift
59 case "$color" in
60 0*) echo -e "\\033[${color:-38}m$@\\033[39m" ;;
61 *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;;
62 esac; unset color
63 }
65 # Last command status
66 status() {
67 local code="$?"
68 case "$code" in
69 0) indent $(($(get_cols) - 11)) "[ $(colorize 32 'Done') ]" ;;
70 1) indent $(($(get_cols) - 11)) "[ $(colorize 31 'Fail') ]" ;;
71 esac
72 }
74 # Print info a la status way: info [color] [content]
75 info() {
76 local info="$2"
77 local char="$(echo $info | wc -L)"
78 local in=$((7 + ${char}))
79 indent $(($(get_cols) - ${in})) "[ $(colorize $1 $info) ]"
80 }
82 # Line separator
83 separator() {
84 case "$output" in
85 html) echo -n '<hr />' ;;
86 *) printf "%$(get_cols)s\n" | tr ' ' "${1:-=}" ;;
87 esac
88 }
90 title() {
91 echo ""; colorize 33 "$@"; separator
92 }
94 footer() {
95 separator "-"; [ "$1" ] && colorize 035 "$1"; echo ""
96 }
98 # Testsuite
99 if [ $(basename $0) == "libseb.sh" ]; then
100 title "libseb.sh title()"
101 echo -n "Checking status() 0"; status
102 echo -n "Checking status() 1"; ls /a/a 2>/dev/null; status
103 echo -n "Checking info()"; info 035 "3.4K"
104 echo -n "Checking colorize()"; colorize 33 " message"
105 echo -n "Checking colorize()"; colorize 033 " message"
106 echo "Checking footer()"; footer "Footer message"
107 fi