#!/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
#tm=1
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
    ct1="\u2554"
    ct2="\u2557"
    cb1="\u255A"
    cb2="\u255D"
    #
    if [[ "${2}${3}${4}${5}${6}" == "1" ]]; then
       ct1="\u2560"
       ct2="\u2563"
    fi
    #
    if [[ "${2}${3}${4}${5}${6}" == "3" ]]; then
       ct1="\u2560"
       ct2="\u2563"
    fi
    #
    if [[ "${2}${3}${4}${5}${6}" == "4" ]]; then
       ct1="\u2560"
       ct2="\u2563"
    fi
    #
    repetec1="${bved}${ct1}$(printf '%.0s\u2550' $(seq $((tmb - 2))) )${ct2}${bfim}"
    #
    if [[ "${2}${3}${4}${5}${6}" == "2" ]]; then
       cb1="\u2554"
       cb2="\u2557"
    fi
    #
    if [[ "${2}${3}${4}${5}${6}" == "3" ]]; then
       cb1="\u2560"
       cb2="\u2563"
    fi

    if [[ "${2}${3}${4}${5}${6}" == "4" ]]; then
       cb1="\u255A"
       cb2="\u255D"
    fi
    #
    repetec2="${bved}${cb1}$(printf '%.0s\u2550' $(seq $((tmb - 2))) )${cb2}${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 ${repetec1}
    fi
    #
       echo -e "${bved}\u2551${msi1_spaces}${1}${msf1_spaces}${bved}\u2551"
    #
    if [[ -n $2 ]] && [[ $2 != 1 ]] && [[ $2 != 2 ]] && [[ $2 != 3 ]] && [[ $2 != 4 ]]; then
       echo -e "${bved}\u2551${msi2_spaces}${2}${msf2_spaces}${bved}\u2551"
    fi
    #
    if [[ -n $3 ]] && [[ $3 != 1 ]] && [[ $3 != 2 ]] && [[ $3 != 3 ]] && [[ $3 != 4 ]]; then
       echo -e "${bved}\u2551${msi3_spaces}${3}${msf3_spaces}${bved}\u2551"
    fi

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