aboutsummaryrefslogtreecommitdiff
path: root/bin/color
blob: 05972780a291bc55784016e03afbddd5dcefed41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  color -c COLOR TEXT
		  color -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -c COLOR
		  -h, --help    show this message


		The available colors are:
	EOF
	list_colors | sed 's/^/    /'
}


END="\033[0m"

black() {
	BLACK="\033[0;30m"
	printf "${BLACK}${1}${END}"
}

blackb() {
	BLACK_B="\033[1;30m"
	printf "${BLACK_B}${1}${END}"
}

blacki() {
	BLACK_I="\033[0;90m"
	printf "${BLACK_I}${1}${END}"
}

white() {
	WHITE="\033[0;37m"
	printf "${WHITE}${1}${END}"
}

whiteb() {
	WHITE_B="\033[1;37m"
	printf "${WHITE_B}${1}${END}"
}

red() {
	RED="\033[0;31m"
	printf "${RED}${1}${END}"
}

redb() {
	RED_B="\033[1;31m"
	printf "${RED_B}${1}${END}"
}

green() {
	GREEN="\033[0;32m"
	printf "${GREEN}${1}${END}"
}

greenb() {
	GREEN_B="\033[1;32m"
	printf "${GREEN_B}${1}${END}"
}

yellow() {
	YELLOW="\033[0;33m"
	printf "${YELLOW}${1}${END}"
}

yellowb() {
	YELLOW_B="\033[1;33m"
	printf "${YELLOW_B}${1}${END}"
}

blue() {
	BLUE="\033[0;34m"
	printf "${BLUE}${1}${END}"
}

blueb() {
	BLUE_B="\033[1;34m"
	printf "${BLUE_B}${1}${END}"
}

bluei() {
	BLUE_I="\033[0;94m"
	printf "${BLUE_I}${1}${END}"
}

purple() {
	PURPLE="\033[0;35m"
	printf "${PURPLE}${1}${END}"
}


purpleb() {
	PURPLE_B="\033[1;35m"
	printf "${PURPLE_B}${1}${END}"
}

lightblue() {
	LIGHT_BLUE="\033[0;36m"
	printf "${LIGHT_BLUE}${1}${END}"
}

lightblueb() {
	LIGHT_BLUE_B="\033[1;36m"
	printf "${LIGHT_BLUE_B}${1}${END}"
}

COLOR_LIST='
black
blackb
white
whiteb
red
redb
green
greenb
yellow
yellowb
blue
blueb
purple
purpleb
lightblue
lightblueb
blacki
bluei
'
list_colors() {
	for c in $COLOR_LIST; do
		printf '%s\n' "$("$c" "$c")"
	done
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

while getopts 'c:h' flag; do
	case "$flag" in
		c)
			EXISTS=false
			for c in $COLOR_LIST; do
				if [ "$OPTARG" = "$c" ]; then
					EXISTS=true
					break
				fi
			done
			if [ "$EXISTS" = false ]; then
				printf 'Invalid color: %s\n' "$OPTARG" >&2
				exit 2
			fi
			COLOR_FN="$OPTARG"
			;;
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

assert_arg() {
	if [ -z "$1" ]; then
		printf 'Missing %s\n\n' "$2" >&2
		usage >&2
		exit 2
	fi
}

TEXT="${1:-}"
assert_arg "$COLOR_FN" '-c COLOR'
assert_arg "$TEXT"  'TEXT'


"$COLOR_FN" "$TEXT"