Created
November 10, 2013 21:38
-
-
Save zvyn/7404317 to your computer and use it in GitHub Desktop.
Functions to color sh-output. Useful for logging or bash-prompt.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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