144
|
1 #!/bin/sh
|
|
2
|
145
|
3 # Usage:
|
|
4 # _c [bold_|]<color> <text>
|
|
5 #
|
|
6 # <color> can be one of:
|
|
7 # black, blue, green, cyan, red, purple, brown, gray
|
|
8 # Prefix the color with bold_ to use bold style/light color.
|
|
9 #
|
|
10 # Example:
|
|
11 # To print word "Hello" in bold blue:
|
|
12 # _c bold_blue "Hello"
|
|
13 #
|
|
14 # To print text with only one word colored:
|
|
15 # echo "Hello, $(c green world)."
|
|
16 #
|
|
17 # To do:
|
|
18 # Support for background color, underline, etc.
|
|
19 #
|
239
|
20 # Reference:
|
|
21 # Colours, http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
|
|
22 #
|
145
|
23
|
144
|
24 _c() {
|
|
25 color_name="${1}"
|
|
26 shift
|
|
27 text="${*}"
|
|
28 esq="\033["
|
|
29 r="${esq}0m" # r for reset
|
|
30 case "${color_name}" in
|
|
31 bold_*) color="1;";;
|
|
32 *) color="0;";;
|
|
33 esac
|
|
34 case "${color_name}" in
|
|
35 *black) color="${color}30";;
|
|
36 *blue) color="${color}34";;
|
|
37 *green) color="${color}32";;
|
|
38 *cyan) color="${color}36";;
|
|
39 *red) color="${color}31";;
|
|
40 *purple) color="${color}35";;
|
|
41 *brown) color="${color}33";;
|
|
42 *gray) color="${color}37";;
|
|
43 esac
|
|
44 color="${esq}${color}m"
|
|
45 printf "${color}%s${r}" "${text}"
|
|
46 }
|
|
47
|