Last active
November 30, 2022 10:20
-
-
Save danielcarr/e4c17f9d22910fa70d49047889bda418 to your computer and use it in GitHub Desktop.
ANSI Codes for exporting in bash scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export CLEAR=`printf '\033[0m'` | |
# EFFECTS / FORMATTING | |
export BOLD=`printf '\033[1m'` | |
export DIM=`printf '\033[2m'` | |
export UNDERLINE=`printf '\033[4m'` | |
# FOREGROUND | |
export BLACK=`printf '\033[30m'` | |
export RED=`printf '\033[31m'` | |
export GREEN=`printf '\033[32m'` | |
export YELLOW=`printf '\033[33m'` | |
export BLUE=`printf '\033[34m'` | |
export MAGENTA=`printf '\033[35m'` | |
export CYAN=`printf '\033[36m'` | |
export WHITE=`printf '\033[37m'` | |
# BACKGROUND | |
export BLACK_BG=`printf '\033[40m'` | |
export RED_BG=`printf '\033[41m'` | |
export GREEN_BG=`printf '\033[42m'` | |
export YELLOW_BG=`printf '\033[43m'` | |
export BLUE_BG=`printf '\033[44m'` | |
export MAGENTA_BG=`printf '\033[45m'` | |
export CYAN_BG=`printf '\033[46m'` | |
export WHITE_BG=`printf '\033[47m'` | |
# BRIGHT/LIGHT FOREGROUND | |
export GREY=`printf '\033[90m'` | |
export BRED=`printf '\033[91m'` | |
export BGREEN=`printf '\033[92m'` | |
export BYELLOW=`printf '\033[93m'` | |
export BBLUE=`printf '\033[94m'` | |
export BMAGENTA=`printf '\033[95m'` | |
export BCYAN=`printf '\033[96m'` | |
export BWHITE=`printf '\033[97m'` | |
# BRIGHT/LIGHT BACKGROUND | |
export GREY_BG=`printf '\033[100m'` | |
export BRED_BG=`printf '\033[101m'` | |
export BGREEN_BG=`printf '\033[102m'` | |
export BYELLOW_BG=`printf '\033[103m'` | |
export BBLUE_BG=`printf '\033[104m'` | |
export BMAGENTA_BG=`printf '\033[105m'` | |
export BCYAN_BG=`printf '\033[106m'` | |
export BWHITE_BG=`printf '\033[107m'` | |
style_described_by() { | |
while (($#)) ; do | |
case $1 in | |
BOLD|BOLDED|bold|bolded) | |
VALUE=1 | |
;; | |
DIM|dim) | |
VALUE=2 | |
;; | |
UNDERLINE|UNDERLINED|underline|underlined) | |
VALUE=4 | |
;; | |
BLACK|black) | |
VALUE=30 | |
;; | |
RED|red) | |
VALUE=31 | |
;; | |
GREEN|green) | |
VALUE=32 | |
;; | |
YELLOW|yellow) | |
VALUE=33 | |
;; | |
BLUE|blue) | |
VALUE=34 | |
;; | |
MAGENTA|magenta) | |
VALUE=35 | |
;; | |
CYAN|cyan) | |
VALUE=36 | |
;; | |
WHITE|white) | |
VALUE=37 | |
;; | |
GREY|grey) | |
VALUE=90 | |
;; | |
BRIGHT|bright) | |
brightness=60 | |
shift | |
continue | |
;; | |
BACKGROUND|BG|background|bg) | |
background=10 | |
;; | |
*) | |
# just ignore invalid codes | |
shift | |
if (($#)) ; then | |
continue | |
else | |
# strip extraneous semicolon | |
SEQUENCE="${SEQUENCE%;}" | |
break | |
fi | |
;; | |
esac | |
if ((brightness && VALUE >= 30 && VALUE < 90)) ; then | |
((VALUE += brightness)) | |
brightness=0 | |
fi | |
if ((background)) ; then | |
if ((VALUE >= 30)) ; then | |
SEQUENCE="${SEQUENCE%${VALUE};}" | |
((VALUE += background)) | |
else | |
# strip most recent code | |
SEQUENCE="${SEQUENCE%;${VALUE};}" | |
# strip all other codes to have only the previous code | |
lastcode="${SEQUENCE##*;}" | |
SEQUENCE="${SEQUENCE%;*};" | |
((VALUE = lastcode + background)) | |
fi | |
background=0 | |
fi | |
SEQUENCE="${SEQUENCE}${VALUE}" | |
shift | |
# add a semi-colon between codes | |
if (($#)) ; then | |
SEQUENCE="${SEQUENCE};" | |
fi | |
done | |
printf '\033[%sm' ${SEQUENCE} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env sh | |
. ansi.bash | |
echo -e "${RED}this is red text${CLEAR}" | |
# bare language | |
style=`style_described_by grey background bright green bold underline` | |
# natural language | |
style=`style_described_by a grey background with bright bold green underlined text` | |
echo -e "${style}hello world${CLEAR}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment