Skip to content

Instantly share code, notes, and snippets.

@zvyn
Created November 10, 2013 21:38
Show Gist options
  • Save zvyn/7404317 to your computer and use it in GitHub Desktop.
Save zvyn/7404317 to your computer and use it in GitHub Desktop.
Functions to color sh-output. Useful for logging or bash-prompt.
changeColor () {
## Usage
local help_message="\
Usage:
changeColor ((fg|bg|light|light-bg)? color (bold|dim|underlined|inverted)*)+
Example:
changeColor fg red light-bg green underlined bold"
#/>
## local properties
local codes='0;'
local prefix=3
local color=''
local format=''
#/>
## parse command-line
while [[ $1 ]]; do
case $1 in
-h|--help)
echo -e "$help_message"
return 0;;
fg)
prefix=3;;
bg)
prefix=4;;
light)
prefix=9;;
light-bg)
prefix=10;;
black)
color=0;;
red)
color=1;;
green)
color=2;;
yellow)
color=3;;
blue)
color=4;;
magenta)
color=5;;
cyan)
color=6;;
gray)
color=7;;
bold)
format="1;";;
dim)
format="2;";;
underlined)
format="4;";;
blink)
format="5;";;
inverted)
format="7;";;
esac
codes+="$format"
if [[ "$color" ]]; then
codes+="${prefix}${color};"
fi
shift
done
#/>
echo -en "\e[${codes%?}m"
}
padWithEscape () {
echo -en "\[$*\]"
}
paddedColor () {
echo -n $(padWithEscape $(changeColor $*))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment