annotate bin/termcolors.sh @ 145:c3ca45204c4e

Documentation.
author Edho Arief <edho@myconan.net>
date Sun, 02 Oct 2011 04:28:49 +0700
parents 1783152a1fe6
children 73e90355bbd7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
144
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1 #!/bin/sh
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
2
145
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
3 # Usage:
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
4 # _c [bold_|]<color> <text>
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
5 #
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
6 # <color> can be one of:
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
7 # black, blue, green, cyan, red, purple, brown, gray
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
8 # Prefix the color with bold_ to use bold style/light color.
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
9 #
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
10 # Example:
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
11 # To print word "Hello" in bold blue:
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
12 # _c bold_blue "Hello"
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
13 #
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
14 # To print text with only one word colored:
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
15 # echo "Hello, $(c green world)."
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
16 #
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
17 # To do:
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
18 # Support for background color, underline, etc.
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
19 #
c3ca45204c4e Documentation.
Edho Arief <edho@myconan.net>
parents: 144
diff changeset
20
144
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
21 _c() {
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
22 color_name="${1}"
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23 shift
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
24 text="${*}"
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
25 esq="\033["
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
26 r="${esq}0m" # r for reset
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
27 case "${color_name}" in
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28 bold_*) color="1;";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29 *) color="0;";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
30 esac
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31 case "${color_name}" in
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
32 *black) color="${color}30";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33 *blue) color="${color}34";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
34 *green) color="${color}32";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
35 *cyan) color="${color}36";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
36 *red) color="${color}31";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
37 *purple) color="${color}35";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
38 *brown) color="${color}33";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
39 *gray) color="${color}37";;
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
40 esac
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
41 color="${esq}${color}m"
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
42 printf "${color}%s${r}" "${text}"
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
43 }
1783152a1fe6 Color template for bash.
Edho Arief <edho@myconan.net>
parents:
diff changeset
44