Skip to content

Instantly share code, notes, and snippets.

@jonjitsu
Created November 3, 2023 14:39
Show Gist options
  • Save jonjitsu/a43ea3897eceedde4d4de4e20acd20df to your computer and use it in GitHub Desktop.
Save jonjitsu/a43ea3897eceedde4d4de4e20acd20df to your computer and use it in GitHub Desktop.
bash color codes
# -*- mode: sh -*-
colors.show-ansi() {
# If you would like to customize your colors, use
# DEV: 30-39 lines up 0-9 from `tput`
for i in $(seq 0 109); do
echo -n -e "\033[1;${i}mText$(tput sgr0) "
echo -n "\033[1;${i}m"
done
}
colors.show-tput() {
for i in $(seq 0 $(tput colors)); do
echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) \$(tput setaf $i)"
done
}
colors.show-color() {
local var="COLOR_$color"
echo -n -e "${!var}$color${COLOR_RESET}"
}
colors.show-current() {
for color in BLACK RED GREEN YELLOW BLUE PURPLE CYAN GREY WHITE
do
colors.show-color "$color"
echo -n " "
done
echo
}
colors.generate-codes-256() {
echo '@TODO'
declare -A colors=([BLACK]=0 [])
}
colors.generate-codes-8() {
echo Generating tput 8 codes
local colors=(BLACK RED GREEN YELLOW BLUE PURPLE CYAN GREY WHITE)
for (( i=0; i<${#colors[@]}; i++ )); do
eval "export COLOR_${colors[$i]}=$(tput setaf $i)"
# eval "COLOR_${colors[$i]}_BOLD=$(tput sgr0)$(tput bold)$(tput setaf $i)"
eval "export COLOR_${colors[$i]}_BOLD=$(tput bold)$(tput setaf $i)"
done
COLOR_BOLD="$(tput bold)"
COLOR_RESET="$(tput sgr0)"
COLOR_NORMAL=$COLOR_RESET
}
colors.generate-codes-ansi() {
echo Generating ANSI codes
COLOR_BOLD="\033[1;1m"
COLOR_RESET="\033[m"
COLOR_NORMAL=$COLOR_RESET
COLOR_BLACK="\033[1;30m"
COLOR_RED="\033[1;31m"
COLOR_GREEN="\033[1;32m"
COLOR_YELLOW="\033[1;33m"
COLOR_BLUE="\033[1;34m"
COLOR_PURPLE="\033[1;35m"
COLOR_CYAN="\033[1;36m"
COLOR_GREY="\033[1;37m"
COLOR_WHITE="\033[1;97m"
# @TODO fixme
COLOR_BOLD_BLACK=$COLOR_BLACK
COLOR_BOLD_RED=$COLOR_RED
COLOR_BOLD_GREEN=$COLOR_GREEN
COLOR_BOLD_YELLOW=$COLOR_YELLOW
COLOR_BOLD_BLUE=$COLOR_BLUE
COLOR_BOLD_PURPLE=$COLOR_PURPLE
COLOR_BOLD_CYAN=$COLOR_CYAN
COLOR_BOLD_GREY=$COLOR_GREY
COLOR_BOLD_WHITE=$COLOR_WHITE
# local colors=(BLACK RED GREEN YELLOW BLUE PURPLE CYAN GREY WHITE)
# for (( i=0; i<${#colors[@]}; i++ )); do
# eval "COLOR_${colors[$i]}_BOLD=${COLOR_BOLD}$(eval $COLOR_${colors[$i]})"
# done
}
colors.generate-codes() {
# @TODO fixme
colors.generate-codes-ansi
return
if tput setaf 1 &> /dev/null; then
# Reset the shell from our `if` check
tput sgr0 &> /dev/null
colors.generate-codes-8
# if [[ "$(tput colors)" -ge 256 ]] &> /dev/null; then
# colors.generate_256_codes
# else
# # Only 8 colors available
# colors.generate-codes-8
# fi
else
# Otherwise, use ANSI escape sequences for coloring
colors.generate-codes-ansi
fi
}
colors.generate-codes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment