Search code examples
bashshellfiglet

I would like to concatenate two figlet outputs (with different colors)


Currently I have this kind of output format:

{ echo "$(figlet buddhi)"; echo "$(figlet lw)"; }
 _               _     _ _     _
| |__  _   _  __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|

 _
| |_      __
| \ \ /\ / /
| |\ V  V /
|_| \_/\_/


And I would like to have this output format:

figlet buddhi lw
 _               _     _ _     _   _
| |__  _   _  __| | __| | |__ (_) | |_      __
| '_ \| | | |/ _` |/ _` | '_ \| | | \ \ /\ / /
| |_) | |_| | (_| | (_| | | | | | | |\ V  V /
|_.__/ \__,_|\__,_|\__,_|_| |_|_| |_| \_/\_/

The reason is: I would like to color each name (buddhi, lw) with a different color. But, retain the format of a continuous string, or at maximum space-separated, as above.

Example:

 #COMMANDS CREATED INSIDE /ETC/BASH.BASHRC FILE
 # USING ANSI COLORS
 RED="\e[31m"
 ORANGE="\e[33m"
 BLUE="\e[94m"
 GREEN="\e[92m"
 STOP="\e[0m"


 printf "${GREEN}"
 printf "=================================\n"
 printf "${ORANGE}"
 figlet -f standard "Buddhi"
 printf "${BLUE}"
 figlet -f  small "LW"
 printf "${GREEN}"
 printf "=================================\n"
 printf "${STOP}"

Solution

  • Store the lines of each word in arrays, output both the arrays line by line. As the first line of "Buddhi" seems to be one character shorter, I stored the longest line length of the first word in a variable, and used the %-s format to pad each line.

    #! /bin/bash
    RED="\e[31m"
    ORANGE="\e[33m"
    BLUE="\e[94m"
    GREEN="\e[92m"
    STOP="\e[0m"
    
    mapfile -t left  < <(figlet -f standard "Buddhi")
    mapfile -t right < <(figlet -f small    "LW")
    
    maxlength=0
    for line in "${left[@]}" ; do
        if (( ${#line} > maxlength )) ; then
            maxlength=${#line}
        fi
    done
    
    printf "${GREEN}"
    printf "=================================\n"
    
    for ((i=0; i<=${#left[@]}; ++i)) ; do
        printf "${ORANGE}%-${maxlength}s ${GREEN}%s\n" "${left[i]}" "${right[i]}"
    done
    
    printf "${GREEN}"
    printf "=================================\n"
    printf "${STOP}"