Skip to content

Instantly share code, notes, and snippets.

@caruccio
Last active April 26, 2023 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caruccio/56b2b98577a60790232c1b3a548a14f9 to your computer and use it in GitHub Desktop.
Save caruccio/56b2b98577a60790232c1b3a548a14f9 to your computer and use it in GitHub Desktop.
Create env vars for somes ANSI colors
function mkcolor()
{
export $1="${2}" ${1}_E="\[${2}\]"
case "$1" in
BOLD|RESET)
return
;;
*)
export ${1}_B="$(tput bold)${2}" # Bold
export ${1}_E="\[${2}\]" # PS1-escaped
export ${1}_BE="\[$(tput bold)\]\[${2}\]" # Bold + PS1-escaped
esac
}
mkcolor COLOR_BOLD "$(tput bold)"
mkcolor COLOR_RESET "$(tput sgr0)"
## darks
mkcolor COLOR_BLACK "$(tput setf 0)"
mkcolor COLOR_RED "$(tput setaf 1)"
mkcolor COLOR_GREEN "$(tput setaf 2)"
mkcolor COLOR_YELLOW "$(tput setaf 3)"
mkcolor COLOR_BLUE "$(tput setaf 4)"
mkcolor COLOR_MAGENTA "$(tput setaf 5)"
mkcolor COLOR_CYAN "$(tput setaf 6)"
mkcolor COLOR_WHITE "$(tput setaf 7)"
## lights
mkcolor COLOR_GRAY "$(tput setaf 8)"
mkcolor COLOR_LIGHT_RED "$(tput setaf 9)"
mkcolor COLOR_LIGHT_GREEN "$(tput setaf 10)"
mkcolor COLOR_LIGHT_YELLOW "$(tput setaf 11)"
mkcolor COLOR_LIGHT_BLUE "$(tput setaf 12)"
mkcolor COLOR_LIGHT_MAGENTA "$(tput setaf 13)"
mkcolor COLOR_LIGHT_CYAN "$(tput setaf 14)"
mkcolor COLOR_LIGHT_WHITE "$(tput setaf 15)"
### Test with:
# echo -e "${COLOR_YELLOW}Hello, I'm an yellow message${COLOR_RESET}"
# echo -e "${COLOR_RED_B}And I'm a bold (_B) red message${COLOR_RESET}"
# echo -e "${COLOR_CYAN_E}I'm good for PS1 prompts only because I'm escaped (\[...\])${COLOR_RESET_E}"
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment