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 #
|
|
20
|
144
|
21 _c() {
|
|
22 color_name="${1}"
|
|
23 shift
|
|
24 text="${*}"
|
|
25 esq="\033["
|
|
26 r="${esq}0m" # r for reset
|
|
27 case "${color_name}" in
|
|
28 bold_*) color="1;";;
|
|
29 *) color="0;";;
|
|
30 esac
|
|
31 case "${color_name}" in
|
|
32 *black) color="${color}30";;
|
|
33 *blue) color="${color}34";;
|
|
34 *green) color="${color}32";;
|
|
35 *cyan) color="${color}36";;
|
|
36 *red) color="${color}31";;
|
|
37 *purple) color="${color}35";;
|
|
38 *brown) color="${color}33";;
|
|
39 *gray) color="${color}37";;
|
|
40 esac
|
|
41 color="${esq}${color}m"
|
|
42 printf "${color}%s${r}" "${text}"
|
|
43 }
|
|
44
|