seb 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 1f29c7b8a4ce
children 99319202f0f0
files libseb.sh
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libseb.sh	Mon Mar 06 16:40:46 2017 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# libseb.sh: Simple functions without gettext for seb cmdline tools
     1.7 +#
     1.8 +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License
     1.9 +#
    1.10 +
    1.11 +# Parse cmdline options and store values in a variable.
    1.12 +for opt in "$@"; do
    1.13 +	opt_name="${opt%%=*}"
    1.14 +	opt_name="$(echo -n "${opt_name#--}" | tr -c 'a-zA-Z0-9' '_')"
    1.15 +	case "$opt" in
    1.16 +		--*=*)		export  $opt_name="${opt#*=}" ;;
    1.17 +		--*)		export  $opt_name="yes" ;;
    1.18 +	esac
    1.19 +done
    1.20 +
    1.21 +# Exit if command ruturn 1
    1.22 +check() {
    1.23 +	case "$?" in
    1.24 +		0) info 32 "Done" ;;
    1.25 +		1) colorize 31 "--> Error" && exit 1 ;;
    1.26 +	esac
    1.27 +}
    1.28 +
    1.29 +# Check if user is logged as root
    1.30 +check_root() {
    1.31 +	if [ $(id -u) -ne 0 ]; then
    1.32 +		echo "You must be root to execute: $(basename $0) $@"; exit 1
    1.33 +	fi
    1.34 +}
    1.35 +
    1.36 +# Get console cols
    1.37 +get_cols() {
    1.38 +	if ! stty size | cut -d " " -f 2; then
    1.39 +		echo 80
    1.40 +	fi
    1.41 +}
    1.42 +
    1.43 +# Indent text
    1.44 +indent() {
    1.45 +	local in="$1"
    1.46 +	shift
    1.47 +	echo -e "\033["${in}"G $@";
    1.48 +}
    1.49 +
    1.50 +# Display a bold message
    1.51 +boldify() {
    1.52 +	case "$output" in
    1.53 +		html) echo "<strong>$@</strong>" ;;
    1.54 +		*) echo -e "\\033[1m$@\\033[0m" ;;
    1.55 +	esac
    1.56 +}
    1.57 +
    1.58 +# Colorize message
    1.59 +colorize() {
    1.60 +	: ${color=$1}
    1.61 +	shift
    1.62 +	case "$color" in
    1.63 +		0*) echo -e "\\033[${color:-38}m$@\\033[39m" ;;
    1.64 +		*)  echo -e "\\033[1;${color:-38}m$@\\033[0;39m" ;;
    1.65 +	esac; unset color
    1.66 +}
    1.67 +
    1.68 +# Last command status
    1.69 +status() {
    1.70 +	local code="$?"
    1.71 +	case "$code" in
    1.72 +		0) indent $(($(get_cols) - 11)) "[ $(colorize 32 'Done') ]" ;;
    1.73 +		1) indent $(($(get_cols) - 11)) "[ $(colorize 31 'Fail') ]" ;;
    1.74 +	esac
    1.75 +}
    1.76 +
    1.77 +# Print info a la status way: info [color] [content]
    1.78 +info() {
    1.79 +	local info="$2"
    1.80 +	local char="$(echo $info | wc -L)"
    1.81 +	local in=$((7 + ${char}))
    1.82 +	indent $(($(get_cols) - ${in})) "[ $(colorize $1 $info) ]"
    1.83 +}
    1.84 +
    1.85 +# Line separator
    1.86 +separator() {
    1.87 +	case "$output" in
    1.88 +		html) echo -n '<hr />' ;;
    1.89 +		*) printf "%$(get_cols)s\n" | tr ' ' "${1:-=}" ;;
    1.90 +	esac
    1.91 +}
    1.92 +
    1.93 +title() {
    1.94 +	echo ""; colorize 33 "$@"; separator
    1.95 +}
    1.96 +
    1.97 +footer() {
    1.98 +	separator "-"; [ "$1" ] && colorize 035 "$1"; echo ""
    1.99 +}
   1.100 +
   1.101 +# Testsuite
   1.102 +if [ $(basename $0) == "libseb.sh" ]; then
   1.103 +	title "libseb.sh title()"
   1.104 +	echo -n "Checking status() 0"; status
   1.105 +	echo -n "Checking status() 1"; ls /a/a 2>/dev/null; status
   1.106 +	echo -n "Checking info()"; info 035 "3.4K"
   1.107 +	echo -n "Checking colorize()"; colorize 33 " message"
   1.108 +	echo -n "Checking colorize()"; colorize 033 " message"
   1.109 +	echo "Checking footer()"; footer "Footer message"
   1.110 +fi