Skip to content

Instantly share code, notes, and snippets.

@timotheeandres
Created August 6, 2022 06:42
Show Gist options
  • Save timotheeandres/8b35ebb2c7473d0bce2245dd35746a7f to your computer and use it in GitHub Desktop.
Save timotheeandres/8b35ebb2c7473d0bce2245dd35746a7f to your computer and use it in GitHub Desktop.
Script to print ANSI colors in the console
#!/bin/bash
readonly TEXT="xYz" # You may change this if you wish
readonly WIDTH=$((${#TEXT}+2))
FOREGROUND=(0 {30..37} {90..97})
BACKGROUND=('' {40..47} {100..107})
MODS=({0..8})
readonly FOREGROUND BACKGROUND MODS
print_line_bg() {
local bg
printf "%7s \033[%s %${#TEXT}s " "$1" "$1" "$TEXT"
for bg in $BGs; do
printf '\033[%s\033[%s %s \033[0m' "$1" "$bg" "$TEXT"
done
echo
}
print_bg() {
local fg
local BGs=''
for bg in ${BACKGROUND[@]+"${BACKGROUND[@]}"}; do
BGs="$(printf "%s%${WIDTH}s" "$BGs" "${bg:+${bg}m}")"
done
readonly BGs
printf '\033[1mFOREGROUND/BACKGROUND\033[0m\n'
echo " $BGs"
for fg in ${FOREGROUND[@]+"${FOREGROUND[@]}"}; do
print_line_bg "${fg}m"
done
echo
}
print_mods() {
local mod
printf '\033[1mFONT MODIFIERS\033[0m\n'
for mod in ${MODS[@]+"${MODS[@]}"}; do
printf '%7s \033[%sm %s \033[0m\n' "${mod}m" "$mod" "$TEXT"
done
echo
}
print_mods
print_bg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment