Skip to content

Instantly share code, notes, and snippets.

@paulscott
Created January 24, 2019 19:46
Show Gist options
  • Save paulscott/6f21886c462a2ac7a9ce18d2f8a32cf4 to your computer and use it in GitHub Desktop.
Save paulscott/6f21886c462a2ac7a9ce18d2f8a32cf4 to your computer and use it in GitHub Desktop.
A colors utility - a guide and a tool
#!/bin/bash
color() {
out=""
saved_in="$@"
saved_in=${saved_in[@]//test} # remove the word 'test'
while [ "${1-}" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
bold) out="${out};01" ;;
dim) out="${out};02" ;;
underline) out="${out};04" ;;
blink) out="${out};05" ;;
reverse) out="${out};07" ;;
hidden) out="${out};08" ;;
black) out="${out};30" ;;
red) out="${out};31" ;;
green) out="${out};32" ;;
yellow) out="${out};33" ;;
blue) out="${out};34" ;;
magenta) out="${out};35" ;;
cyan) out="${out};36" ;;
gray | grey) out="${out};37" ;;
lblack | dgray | dgrey ) out="${out};90" ;;
lred) out="${out};91" ;;
lgreen) out="${out};92" ;;
lyellow) out="${out};93" ;;
lblue) out="${out};94" ;;
lmagenta) out="${out};95" ;;
lcyan) out="${out};96" ;;
white) out="${out};97" ;;
bg_black) out="${out};40" ;;
bg_red) out="${out};41" ;;
bg_green) out="${out};42" ;;
bg_yellow) out="${out};43" ;;
bg_blue) out="${out};44" ;;
bg_magenta) out="${out};45" ;;
bg_cyan) out="${out};46" ;;
bg_gray | bg_grey) out="${out};47" ;;
bg_lblack | bg_dgray | bg_dgrey ) out="${out};100" ;;
bg_lred) out="${out};101" ;;
bg_lgreen) out="${out};102" ;;
bg_lyellow) out="${out};103" ;;
bg_lblue) out="${out};104" ;;
bg_lmagenta) out="${out};105" ;;
bg_lcyan) out="${out};106" ;;
bg_white) out="${out};107" ;;
reset_color | fg_reset | default) out="${out};39" ;;
bg_reset_color | bg_reset) out="${out};49" ;;
reset | reset_all) out="${out};0" ;;
test) print_test_output=1 ;;
*)
;;
esac
shift
done
out="\033[${out:1}m"
if [ "$print_test_output" == 1 ]; then
color=$(echo -en $out)
reset=$(echo -en '\033[0m')
printf "${saved_in}: \\${out} \t ${color}This is a test of ${saved_in}.${reset}\n"
else
echo -en "${out}"
fi
}
usage() {
echo """
Outputs the requested terminal color escape sequences, by name, changing the current color.
If 'test' is one of the color names requested, then the current is not changed,
instead the escape sequence is displayed, along with a brief demo.
example:
> bash_colors cyan underline test
cyan underline: \033[36;04m $(bash_colors cyan underline)This is a test of cyan underline.$(bash_colors reset)
example usage in a script:
echo \$(bash_colors blue)This text will be blue
echo \$(bash_colors yellow bg_red)This text will be yellow on a red background
echo \$(bash_colors green)This text will be green on a red background
echo \$(bash_colors bg_reset cyan)This text will be cyan on a normal background
echo \$(bash_colors reset)This text will be normal. You should usually reset before a newline.
"""
}
if [ -z "$1" ]; then usage; exit -1; fi
color $@
# sed -e '$d' "$0" # command to echo the source of the script minus this last line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment