#!/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 [[ -n $3 ]]; then
       nc=$(echo "${3}" | grep -Fo '\e[' | wc -l)
       if [[ $nc <  "1" ]]; then
          tmh3=$((${#3} + 2)) # tamanho da msg + 2 '#'
       else
          tmh3=$((${#3} - ($nc * 8)+2)) # tamanho da msg -'nc' caracteres da cor + 2 '#'
       fi
       msi3_spaces=$(printf "%*s" $((($tmb - $tmh3)/2)) "")
       msf3_spaces=$(printf "%*s" $(($tmb - ($tmh3 + ($tmb - $tmh3)/2)))"")
    fi
    #
    if [[ -n $4 ]]; then
       nc=$(echo "${4}" | grep -Fo '\e[' | wc -l)
       if [[ $nc <  "1" ]]; then
          tmh4=$((${#4} + 2)) # tamanho da msg + 2 '#'
       else
          tmh4=$((${#4} - ($nc * 8)+2)) # tamanho da msg -'nc' caracteres da cor + 2 '#'
       fi
       msi4_spaces=$(printf "%*s" $((($tmb - $tmh4)/2)) "")
       msf4_spaces=$(printf "%*s" $(($tmb - ($tmh4 + ($tmb - $tmh4)/2)))"")
    fi
    #
    if [[ -n $5 ]]; then
       nc=$(echo "${5}" | grep -Fo '\e[' | wc -l)
       if [[ $nc <  "1" ]]; then
          tmh5=$((${#5} + 2)) # tamanho da msg + 2 '#'
       else
          tmh5=$((${#5} - ($nc * 8)+2)) # tamanho da msg -'nc' caracteres da cor + 2 '#'
       fi
       msi5_spaces=$(printf "%*s" $((($tmb - $tmh5)/2)) "")
       msf5_spaces=$(printf "%*s" $(($tmb - ($tmh5 + ($tmb - $tmh5)/2)))"")
    fi
    #
    if [[ $2 != 1 ]] && [[ $3 != 1 ]] && [[ $4 != 1 ]] && [[ $5 != 1 ]]  && [[ $6 != 1 ]]; then
       echo -e ${repetec}
    fi
    #
       echo -e "${bved}#${msi1_spaces}${1}${msf1_spaces}${bved}#"
    #
    if [[ -n $2 ]] && [[ $2 != 1 ]] && [[ $2 != 2 ]]; then
       echo -e "${bved}#${msi2_spaces}${2}${msf2_spaces}${bved}#"
    fi
    #
    if [[ -n $3 ]] && [[ $3 != 1 ]] && [[ $3 != 2 ]]; then
       echo -e "${bved}#${msi3_spaces}${3}${msf3_spaces}${bved}#"
    fi

    if [[ -n $4 ]] && [[ $4 != 1 ]] && [[ $4 != 2 ]]; then
       echo -e "${bved}#${msi4_spaces}${4}${msf4_spaces}${bved}#"
    fi
    #
    if [[ -n $5 ]] && [[ $5 != 1 ]] && [[ $5 != 2 ]]; then
       echo -e "${bved}#${msi5_spaces}${5}${msf5_spaces}${bved}#"
    fi
    #
    if [[ $2 != 2 ]] && [[ $3 != 2 ]] && [[ $4 != 2 ]] && [[ $5 != 2 ]]  && [[ $6 != 2 ]]; then
       echo -e ${repetec}
    fi
    #
}
#
