Skip to content

Instantly share code, notes, and snippets.

@nilox94
Created June 13, 2018 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilox94/a9ac14e3a54d8f0dc45cb872076d0e00 to your computer and use it in GitHub Desktop.
Save nilox94/a9ac14e3a54d8f0dc45cb872076d0e00 to your computer and use it in GitHub Desktop.
Terminal colors for common outputs and bash prompts with a colored prompt example
# styles
declare -A styles=(
["normal"]="0"
["bold"]="1"
["dark"]="2"
["under"]="4"
["invert"]="7"
["background"]="8"
["negate"]="9"
)
declare -A colors=(
# normal colors (bold style is light)
["black"]="30"
["red"]="31"
["green"]="32"
["yellow"]="33" # orange
["blue"]="34"
["pink"]="35"
["cyan"]="36"
["gray"]="37"
["white"]="38"
)
declare -A background=(
# background
["black"]="40"
["red"]="41"
["green"]="42"
["orange"]="43"
["blue"]="44"
["pink"]="45"
["cyan"]="46"
["gray"]="47"
["lightblack"]="100"
["lightred"]="101"
["lightgreen"]="102"
["lightorange"]="103"
["lightblue"]="104"
["lightpink"]="105"
["lightcyan"]="106"
["lightgray"]="107" # white
)
function color()
{
foreground=$1
background=$2
style=$3
code="\e["
if [[ ${colors[$foreground]} ]]; then
code="${code}${colors[$foreground]}"
else
echo "bad color $foreground"
return 1
fi
count=0
for param in $@; do
if [[ $count != 0 ]]; then
if [[ ${styles[$param]} ]]; then
code="${code};${styles[$param]}"
else
echo "undefined option '${param}'"
return 1
fi
else
(( count++ ))
fi
done
code="${code}m"
echo ${code}
}
function prompt_color()
{
echo "\[$(color $*)\]"
}
# ## Colored prompt example
# #
#
# function retval()
# {
# code=$?
# bracket_col=$1
# code_col=$2
# [ $code != 0 ] && echo "${bracket_col}[${code_col}${code}${bracket_col}]"
# code=0
# }
#
#
# symbol_col="$(prompt_color gray normal)"
#
# user="$(prompt_color green bold)\u"
# host="$(prompt_color pink bold)\h"
# path="$(prompt_color blue bold)\w"
#
# at="${symbol_col}@"
# colon="${symbol_col}:"
# dollar="${symbol_col}\$"
#
# exit_code="\`retval '${symbol_col}' '$(prompt_color red bold)'\`"
#
# PS1="${user}${at}${host}${colon}${path}${exit_code}${dollar} "
# unset user host path at colon dollar exit_code symbol_col
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment