#!/bin/bash
#
# Regular Colors
prt='\e[0;30m' # Black
ver='\e[0;31m' # Red
ved='\e[0;32m' # Green
ama='\e[0;33m' # Yellow
azu='\e[0;34m' # Blue
pur='\e[0;35m' # Purple
cia='\e[0;36m' # Cyan
bra='\e[0;37m' # White
fim='\e[0m'    # Normal
# Bold
bpre='\e[1;30m' # Black
bver='\e[1;31m' # Red
bved='\e[1;32m' # Green
bama='\e[1;33m' # Yellow
bazu='\e[1;34m' # Blue
bpur='\e[1;35m' # Purple
bcia='\e[1;36m' # Cyan
bbra='\e[1;37m' # White
bfim='\e[0m'    # Normal
#
function msg {
    tmb=80
    nc=$(echo "${1}" | grep -Fo '\e[' | wc -l)
    #
    if [[ $nc <  "1" ]]; then
       tmh1=$((${#1} + 2)) # tamanho da msg + 2 '#'
    else
       tmh1=$((${#1} - ($nc * 8)+2)) # tamanho da msg -'nc' caracteres da cor + 2 '#'
    fi
    #
    if [[ $tm = 1 ]]; then
       tmb=$tmah
       tm=0
    elif [[ $tmh1 -gt $tmb  ]]; then
         tmb=$(($tmh1 + 2))
         tmah=$tmb
         tm=1
    fi
    #
    msi1_spaces=$(printf "%*s" $((($tmb - $tmh1)/2)) "") # espacos antes da msg
    msf1_spaces=$(printf "%*s" $(($tmb - ($tmh1 + ($tmb - $tmh1)/2)))"") # espacos depois da msg
    repetec="${bved}$(printf '%.0s#' $(seq $tmb))${bfim}"
    #
    if [[ -n $2 ]]; then
       nc=$(echo "${2}" | grep -Fo '\e[' | wc -l)
       if [[ $nc <  "1" ]]; then
          tmh2=$((${#2} + 2)) # tamanho da msg + 2 '#'
       else
          tmh2=$((${#2} - ($nc * 8)+2)) # tamanho da msg -'nc' caracteres da cor + 2 '#'
       fi
       msi2_spaces=$(printf "%*s" $((($tmb - $tmh2)/2)) "")
       msf2_spaces=$(printf "%*s" $(($tmb - ($tmh2 + ($tmb - $tmh2)/2)))"")
    fi
    #
    if [[ $2 != 1 ]]; then # se a variavel $2 não tiver 1 mostra o #
       if [[ $3 != 1 ]]; then # se a variavel $3 não tiver 1 mostra o #
          echo -e ${repetec}
       fi
    fi
    #
    echo -e "${bved}#${msi1_spaces}${1}${msf1_spaces}${bved}#"
    #
    if [[ -n $2 ]]; then
       if [[ $2 != 1 ]]; then # se a variavel $2 não tiver 1 mostra o #
       echo -e "${bved}#${msi2_spaces}${2}${msf2_spaces}${bved}#"
       fi
    fi
    echo -e ${repetec}
}
#
