Skip to content

Instantly share code, notes, and snippets.

@ryangoree
Created March 30, 2026 21:13
Show Gist options
  • Select an option

  • Save ryangoree/9e48de3a3470fa4e6956fe2144ce0a5d to your computer and use it in GitHub Desktop.

Select an option

Save ryangoree/9e48de3a3470fa4e6956fe2144ce0a5d to your computer and use it in GitHub Desktop.
POSIX-compliant formatting functions.
# ANSI Select Graphic Rendition (SGR) Formatting Syntax:
#
# ESC [ Ps [; Ps]* m
#
# Where:
# ESC = '\033' (octal) / '\u001b' (unicode) / '\x1b' (hex)
# '[' = Control Sequence Introducer (CSI)
# Ps = one or more numeric parameters Values(e.g. '1', '2', '31', '44')
#
# Example: '\033[4;31mHello\033[0m' → underlined (4) red (31) 'Hello'
# Select Graphic Rendition attribute codes for text formatting.
sgr() {
case "$1" in
reset) echo "0" ;;
bold) echo "1" ;;
dim) echo "2" ;;
reset_weight) echo "22" ;; # Resets bold + dim
italic) echo "3" ;;
reset_italic) echo "23" ;;
underline) echo "4" ;;
reset_underline) echo "24" ;;
strikethrough) echo "9" ;;
reset_strikethrough) echo "29" ;;
# Common foreground colors, based on an old hardware-based color scheme that
# used a 3-bit mask in blue-green-red order.
#
# e.g., 6 = 110 = blue + green, no red.
black) echo "30" ;;
red) echo "31" ;;
green) echo "32" ;;
yellow) echo "33" ;;
blue) echo "34" ;;
magenta) echo "35" ;;
cyan) echo "36" ;;
white) echo "37" ;;
default) echo "39" ;;
# Common background colors.
black_bg) echo "40" ;;
red_bg) echo "41" ;;
green_bg) echo "42" ;;
yellow_bg) echo "43" ;;
blue_bg) echo "44" ;;
magenta_bg) echo "45" ;;
cyan_bg) echo "46" ;;
white_bg) echo "47" ;;
default_bg) echo "49" ;;
# Common bright foreground colors.
black_bright) echo "90" ;;
red_bright) echo "91" ;;
green_bright) echo "92" ;;
yellow_bright) echo "93" ;;
blue_bright) echo "94" ;;
magenta_bright) echo "95" ;;
cyan_bright) echo "96" ;;
white_bright) echo "97" ;;
default_bright) echo "39" ;;
*) echo "" ;;
esac
}
# ANSI escape codes for colors and styles.
ansi() {
_ansi_result=""
_ansi_did_esc=0
_ansi_did_csi=0
_ansi_did_sgr=0
while [ $# -gt 0 ]; do
case "$1" in
esc)
# Only add if we're not in the middle of building an SGR sequence
if [ "$_ansi_did_sgr" = 0 ]; then
_ansi_result="${_ansi_result}\033"
_ansi_did_esc=1
fi
;;
csi)
# Only add if we're not in the middle of building an SGR sequence
if [ "$_ansi_did_sgr" = 0 ]; then
_ansi_result="${_ansi_result}["
_ansi_did_csi=1
fi
;;
*)
_sgr_code=$(sgr "$1")
if [ -z "$_sgr_code" ]; then
shift
continue
fi
if [ "$_ansi_did_esc" = 0 ]; then
_ansi_result="${_ansi_result}$(ansi esc)"
_ansi_did_esc=1
fi
if [ "$_ansi_did_csi" = 0 ]; then
_ansi_result="${_ansi_result}$(ansi csi)"
_ansi_did_csi=1
fi
if [ "$_ansi_did_sgr" = 1 ]; then
_ansi_result="${_ansi_result};"
fi
_ansi_result="${_ansi_result}${_sgr_code}"
_ansi_did_sgr=1
;;
esac
shift
done
if [ "$_ansi_did_sgr" = 1 ]; then
_ansi_result="${_ansi_result}m"
fi
printf '%b\n' "$_ansi_result"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment