2018-01-04 20:27:11 -09:00
#!/bin/bash
2018-01-06 20:30:09 -09:00
# doc msg {
#
# NAME
# msg
#
# DESCRIPTION
# Messaging framework for scripts
#
# USAGE
# _msg <command> <arg>
#
# COMMAND
# INDENT +/-<unit>
# Indents string.
#
# EXEC <message>
# Prints EXEC line with message
#
# OK <lines>
# Updates the status of an EXEC message to OK. Set <lines> when the EXEC
# status is higher than directly above the current line.
#
# WARN <message> <lines>
# Updates the status of an EXEC message to WARN, a non-fatal error. If the
# output to the warning command redirects to ${STDERR}. it will be
# outputted. Set <lines> when the EXEC line is higher than directly
# above the current line.
#
# FAIL <message> <lines>
# Updates the status to an EXEC message to FAIL, a fatal error. If the
# output to the failed command redirects to ${STDERR}, it will be
# outputted. The bail() function will then be executed, which defaults to
# returning with an exit code 1, than exits. Set <lines> when the EXEC line
# is higher than directly above the current line.
#
# ECHO <message>
# Prints a normal message
# }
2018-01-04 20:27:11 -09:00
STDERR = $( mktemp /tmp/STDERR.XXXXXXXXXX)
2018-01-06 20:30:09 -09:00
INDENT = 0
bail( ) {
return 1
exit
}
2018-01-04 20:27:11 -09:00
2018-01-05 21:02:41 -09:00
_msg( ) {
2018-01-04 20:27:11 -09:00
if [ ${ 1 } = "INDENT" ] ; then
if [ -z " ${ 1 } " ] ; then
INDENT = "0"
else
shift
INDENT = $(( ${ INDENT } ${ 1 } * 6 ))
fi
elif [ ${ 1 } = "EXEC" ] ; then
shift
2018-01-06 20:30:09 -09:00
echo -en " $( _ansi rt " ${ INDENT } " ) [ ] ${ 1 } \n "
2018-01-04 20:27:11 -09:00
elif [ ${ 1 } = "OK" ] ; then
if [ -z " ${ 2 } " ] ; then
HEIGHT = 1
else
HEIGHT = $(( ${ 2 } + 1 ))
fi
2018-01-06 20:30:09 -09:00
echo -en " $( _ansi up " ${ HEIGHT } " ) $( _ansi rt " ${ INDENT } " ) [ $( _ansi bf grn) OK $( _ansi rs) ] $( _ansi lt 100) $( _ansi dn " ${ HEIGHT } " ) "
2018-01-04 20:27:11 -09:00
elif [ ${ 1 } = "WARN" ] ; then
shift;
if [ -z " ${ 2 } " ] ; then
HEIGHT = 1
else
HEIGHT = $(( ${ 2 } + 1 ))
fi
2018-01-06 20:30:09 -09:00
echo -en " $( _ansi up " ${ HEIGHT } " ) $( _ansi rt " ${ INDENT } " ) [ $( _ansi bf yel) WARN $( _ansi rs) ] "
echo -en " \n $( _ansi rt " ${ INDENT } " ) [>>>>>>] ${ 1 } \n "
2018-01-04 20:27:11 -09:00
if [ -n " ${ STDERR } " ] ; then
cat ${ STDERR }
fi
rm ${ STDERR }
STDERR = $( mktemp /tmp/STDERR.XXXXXXXXXX)
2018-01-06 20:30:09 -09:00
echo -en " $( _ansi lt 100) $( _ansi dn " ${ HEIGHT } " ) "
2018-01-04 20:27:11 -09:00
elif [ ${ 1 } = "FAIL" ] ; then
shift;
if [ -z " ${ 2 } " ] ; then
HEIGHT = 1
else
HEIGHT = $(( ${ 2 } + 1 ))
fi
2018-01-06 20:30:09 -09:00
echo -en " $( _ansi up " ${ HEIGHT } " ) $( _ansi rt " ${ INDENT } " ) [ $( _ansi bf red) FAIL $( _ansi rs) ] "
echo -en " \n $( _ansi rt " ${ INDENT } " ) [>>>>>>] ${ 1 } \n "
2018-01-04 20:27:11 -09:00
if [ -n " ${ STDERR } " ] ; then
cat ${ STDERR }
fi
rm ${ STDERR }
STDERR = $( mktemp /tmp/STDERR.XXXXXXXXXX)
2018-01-06 20:30:09 -09:00
echo -en " $( _ansi lt 100) $( _ansi dn " ${ HEIGHT } " ) "
bail
2018-01-04 20:27:11 -09:00
elif [ ${ 1 } = "ECHO" ] ; then
shift
2018-01-06 20:30:09 -09:00
echo -e " $( _ansi rt " ${ INDENT } " ) [======] ${ 1 } "
2018-01-04 20:27:11 -09:00
fi
}
# vim: filetype=bash