refact with bes-color dependency

This commit is contained in:
a-sansara 2018-12-22 01:12:59 +01:00
parent 84df431633
commit 6269f31733
2 changed files with 46 additions and 87 deletions

View File

@ -1,10 +1,13 @@
[project] [project]
vendor = bes vendor = bes
name = echo name = echo
version = 1.1 version = 1.2
license = "GNU GPL v3" license = "GNU GPL v3"
author = a-Sansara author = a-Sansara
type = library type = library
homepage = "https://git.pluie.org/meta-tech/bes-echo" homepage = "https://git.pluie.org/meta-tech/bes-echo"
description = "bash bes display utility library" description = "bash bes display utility library"
keywords = "bash, bes" keywords = "bash, bes"
[require]
bes.color = 1.1

View File

@ -1,4 +1,3 @@
#!/bin/bash
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# #
# @author a-Sansara - https://git.pluie.org/meta-tech/bes-echo # @author a-Sansara - https://git.pluie.org/meta-tech/bes-echo
@ -9,80 +8,49 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.boot () function bes.echo.boot ()
{ {
BES_TERM_WIDTH=${BES_TERM_WIDTH:-105} BES_TERM_WIDTH=${COLUMNS:-130}
BES_NOCOLOR=${BES_NOCOLOR:-0} BES_TERM_WIDTH=$(($BES_TERM_WIDTH - 20))
if [ "$BES_NOCOLOR" -eq 0 ]; then
Cok="\033[0;38;5;43m"; Cko="\033[0;38;5;217m"
Coff="\033[m"; Ctitle="\033[1;48;5;24;1;38;5;15m"
Cdone="\033[1;48;5;36;1;38;5;15m"; Cfail="\033[1;48;5;196;1;38;5;15m"
Cspe="\033[1;38;5;223m"; Citem="\033[1;38;5;214m"
Cval="\033[1;38;5;215m"; Cusa="\033[1;38;5;214m"
Cbra="\033[1;38;5;203m"; Crepo="\033[1;38;5;223m"
Cmeta="\033[1;38;5;30m"; Ctext="\033[1;38;5;30m"
Copt="\033[1;38;5;81m"; Csep="\033[1;38;5;241m"
Cerr="\033[1;38;5;196m"; Ccom="\033[0;38;5;139m"
Csection="\033[1;38;5;97m"; Caction="\033[0;38;5;37m"
fi
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo () function echo.msg ()
{ {
local msg=${1:-''} local c=${2:-}
local isAction=${2:-'0'} local m=${1:-}
local symbol=${3:-' *'} echo -e " $c$m$Coff"
if [ ! "$BES_NOCOLOR" = 1 ]; then
local c=" "
if [ -z "$isAction" ] || [ "$isAction" = 1 ]; then
c=$Caction
fi
if [ ! "$isAction" = 0 ]; then
c=" $Citem$symbol $c"
fi
echo -e " $c$msg$Coff"
else
if [ ! "$isAction" = 0 ]; then
msg=" $symbol $msg"
fi
echo -e "$msg"
fi
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.action () function echo.state ()
{ {
bes.echo "$1" 1 local len=14
} echo -en " $Csep"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ printf "%0.s-" $(seq 1 $(($BES_TERM_WIDTH-${len})))
function bes.echo.title ()
{
echo
bes.echo " ${Citem}${Csection}$1 ${Cspe}$2${Coff}"
echo
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.keyval ()
{
local c=': '
if [ ! "$BES_NOCOLOR" = 1 ]; then
c="$Citem: ${Cval}"
fi
local len="%-15s "
# printf "%s %s [UP]\n" $PROC_NAME "${line:${#PROC_NAME}}"
bes.echo "$(printf $len $1) $c$2 " 1 " "
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.state ()
{
local len=8
printf "%0.s " $(seq 1 $(($BES_TERM_WIDTH-${len})))
if [ "$1" = 0 ]; then if [ "$1" = 0 ]; then
echo -e "${Cdone} OK ${Coff}" echo -e "$Coff $Cdone OK $Coff\n"
else else
echo -e "${Cfail} KO ${Coff}" echo -e "$Coff $Cfail KO $Coff\n"
fi fi
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.rs () function echo.action ()
{
local symbol=${3:-*}
local color=${4:-Citem}
echo -e " $Csymbol$symbol ${Caction}$1 ${!color}$2$Coff"
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function echo.title ()
{
echo -e "\n $Csymbol$Ctitle$1 $Coff$Cspe$2$Coff\n"
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function echo.keyval ()
{
local len=${3:-20}
local c="$Ckey: ${Cval}"
echo -e " $Ckey $(printf "%-${len}s " "$1") ${c}${2} $Coff"
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function echo.rs ()
{ {
local rs=${1:-0} local rs=${1:-0}
if [ "$rs" -eq 0 ]; then if [ "$rs" -eq 0 ]; then
@ -92,7 +60,7 @@ function bes.echo.rs ()
fi fi
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.error () function echo.error ()
{ {
local leave=${2:-0} local leave=${2:-0}
echo -e "\n${Cerr} error : ${Coff}\n\t$1 ${Coff}\n" echo -e "\n${Cerr} error : ${Coff}\n\t$1 ${Coff}\n"
@ -101,42 +69,30 @@ function bes.echo.error ()
fi fi
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.sepline () function echo.sepline ()
{ {
local char=${1:-'_'} local char=${1:-'_'}
local width=${2:-$BES_TERM_WIDTH} local width=${2:-$BES_TERM_WIDTH}
echo -ne "${Csep} " echo -ne "${Cheadline} "
printf "%0.s$char" $(seq 1 $width) printf "%0.s$char" $(seq 1 $width)
echo -e "${Coff}" echo -e "${Coff}"
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.app () function echo.app ()
{ {
local msg=${1:-''} local msg=${1:-''}
local version=${2:-''} local version=${2:-''}
local author=${3:-'a-Sansara'} local author=${3:-'a-Sansara'}
local license=${3:-'GNU GPL v3'}
if [ ! -z "$2" ]; then if [ ! -z "$2" ]; then
msg="$msg ${Cval}v$version" msg="$msg ${Cval}v$version"
fi fi
local len="$1${version}license : GNU GPL v3 author:$author" local len="$1${version}license : $license author:$author"
bes.echo.sepline echo.sepline
echo -ne "\n $Ctitle $msg $Coff" echo -ne "\n $Chead $msg $Coff"
printf "%0.s " $(seq 1 $(($BES_TERM_WIDTH-${#len}-15))) printf "%0.s " $(seq 1 $(($BES_TERM_WIDTH-${#len}-15)))
echo -e " ${Cmeta}license : ${Coff}GNU GPL v3 ${Cmeta}author : ${Cval}$author" echo -e " ${Cmeta}license : ${Coff}$license ${Cmeta}author : ${Cval}$author"
bes.echo.sepline echo.sepline
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bes.echo.colormap ()
{
for fgbg in 38 48 ; do
for color in {0..256} ; do
echo -en "\e[${fgbg};5;${color}m ${color}\t\e[0m"
if [ $((($color + 1) % 7)) == 0 ] ; then
echo
fi
done
echo
done
} }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bes.echo.boot bes.echo.boot