Skip to content

Instantly share code, notes, and snippets.

@flamewing
Last active December 21, 2021 10:33
Show Gist options
  • Save flamewing/607c3c9228bd2ad668c5bffc4e0a05e0 to your computer and use it in GitHub Desktop.
Save flamewing/607c3c9228bd2ad668c5bffc4e0a05e0 to your computer and use it in GitHub Desktop.
Public domain script to test ANSI escape codes for styles and colors
#!/bin/bash
# ANSI Escape Codes
# Enable styles
stySetNormal=$'\e[0m'
stySetBright=$'\e[1m'
stySetDim=$'\e[2m'
stySetBold=$'\e[1;2m'
stySetItalic=$'\e[3m'
stySetUnderline=$'\e[4m'
stySetBlink=$'\e[5m'
stySetFastBlink=$'\e[6m'
stySetReverse=$'\e[7m'
stySetHidden=$'\e[8m'
stySetCrossed=$'\e[9m'
stySetDoubleUnderline=$'\e[21m'
stySetOverline=$'\e[53m'
# Disable styles
styClearBright=$'\e[22m'
styClearDim=$'\e[22m'
styClearBold=$'\e[22m'
styClearItalic=$'\e[23m'
styClearUnderline=$'\e[24m'
styClearBlink=$'\e[25m'
styClearReverse=$'\e[27m'
styClearHidden=$'\e[28m'
styClearCrossed=$'\e[29m'
stySetDoubleUnderline=$'\e[24m'
styClearOverline=$'\e[55m'
styClearAll=$'\e[0m'
styReset=$'\e[m'
# Set foreground color
fgBlack=$'\e[30m'
fgDkRed=$'\e[31m'
fgDkGreen=$'\e[32m'
fgDkYellow=$'\e[33m'
fgDkBlue=$'\e[34m'
fgDkMagenta=$'\e[35m'
fgDkCyan=$'\e[36m'
fgLtGray=$'\e[37m'
fgDkGray=$'\e[90m'
fgLtRed=$'\e[91m'
fgLtGreen=$'\e[92m'
fgLtYellow=$'\e[93m'
fgLtBlue=$'\e[94m'
fgLtMagenta=$'\e[95m'
fgLtCyan=$'\e[96m'
fgWhite=$'\e[97m'
fgDefault=$'\e[39m'
# Set background color
bgBlack=$'\e[40m'
bgDkRed=$'\e[41m'
bgDkGreen=$'\e[42m'
bgDkYellow=$'\e[43m'
bgDkBlue=$'\e[44m'
bgDkMagenta=$'\e[45m'
bgDkCyan=$'\e[46m'
bgLtGray=$'\e[47m'
bgDkGray=$'\e[100m'
bgLtRed=$'\e[101m'
bgLtGreen=$'\e[102m'
bgLtYellow=$'\e[103m'
bgLtBlue=$'\e[104m'
bgLtMagenta=$'\e[105m'
bgLtCyan=$'\e[106m'
bgWhite=$'\e[107m'
bgDefault=$'\e[49m'
export stySetNormal stySetBright stySetDim stySetBold stySetItalic stySetUnderline stySetBlink stySetFastBlink stySetReverse stySetHidden stySetCrossed stySetDoubleUnderline stySetOverline
export styClearBright styClearDim styClearBold styClearItalic styClearUnderline styClearBlink styClearReverse styClearHidden styClearCrossed styClearOverline styClearAll styReset
export fgBlack fgDkRed fgDkGreen fgDkYellow fgDkBlue fgDkMagenta fgDkCyan fgLtGray fgDkGray fgLtRed fgLtGreen fgLtYellow fgLtBlue fgLtMagenta fgLtCyan fgWhite fgDefault
export bgBlack bgDkRed bgDkGreen bgDkYellow bgDkBlue bgDkMagenta bgDkCyan bgLtGray bgDkGray bgLtRed bgLtGreen bgLtYellow bgLtBlue bgLtMagenta bgLtCyan bgWhite bgDefault
AllColors="Default"
for clr in Black DkGray LtGray White DkRed LtRed DkYellow LtYellow DkGreen LtGreen DkCyan LtCyan DkBlue LtBlue DkMagenta LtMagenta; do
clrvar=fg${clr}
AllColors="${AllColors} ${!clrvar}${clr}"
done
AllColors="${AllColors} ${styReset}"
chBar=$'\u2503'
chCross=$'\u254B'
chTBar=$'\u2533'
chInvT=$'\u253B'
chTLeft=$'\u2523'
chTRight=$'\u252B'
chDash=$'\u2501'
chTLCorner=$'\u250F'
chTRCorner=$'\u2513'
chBLCorner=$'\u2517'
chBRCorner=$'\u251B'
chDashCol1=$(head -c 10 < /dev/zero | tr '\0' '#' | sed -r "s/#/$chDash/g")
chDashCol2=$(head -c 15 < /dev/zero | tr '\0' '#' | sed -r "s/#/$chDash/g")
chDashCol3=$(head -c 128 < /dev/zero | tr '\0' '#' | sed -r "s/#/$chDash/g")
format="${chBar}%-10s${chBar}%-15s${chBar}%-128s${chBar}\n"
echo "${chTLCorner}${chDashCol1}${chTBar}${chDashCol2}${chTBar}${chDashCol3}${chTRCorner}"
# shellcheck disable=SC2059
printf "${format}" "Background" "Style" "Text Colors"
for sty in Normal Bright Dim Bold Italic Underline Blink FastBlink Reverse Hidden Crossed DoubleUnderline Overline ; do
echo "${chTLeft}${chDashCol1}${chCross}${chDashCol2}${chCross}${chDashCol3}${chTRight}"
styvar=stySet${sty}
for clr in Default Black DkGray LtGray White DkRed LtRed DkYellow LtYellow DkGreen LtGreen DkCyan LtCyan DkBlue LtBlue DkMagenta LtMagenta; do
clrvar=bg${clr}
# shellcheck disable=SC2059
printf "${format}" "${clr}" "${sty}" "${styReset}${!styvar}${!clrvar}${AllColors}"
done
done
echo "${chBLCorner}${chDashCol1}${chInvT}${chDashCol2}${chInvT}${chDashCol3}${chBRCorner}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment