-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.sh
executable file
·322 lines (294 loc) · 9.08 KB
/
colors.sh
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bash
#-----------------------------------------------------------------------------------
# _
# __ ___| |___ _ _ ___
# / _/ _ \ / _ \ '_(_-<
# \__\___/_\___/_| /__/
#
#-----------------------------------------------------------------------------------
# VERSION="1.2.2"
#-----------------------------------------------------------------------------------
#
# Headings and colored text for shell scripts
#
#-----------------------------------------------------------------------------------
# Author: Rick Ellis
# URL: https://github.com/rickellis/Shell-Scripts/colors
# License: MIT
#-----------------------------------------------------------------------------------
#
# USAGE
#
# To give a text string a color use:
#
# echo -e "${green}This is green text${reset}
#
# echo -e "${bold}${magenta}This is bold magenta text${reset}
#
# Shorter syntax
#
# echo -e "${pur}This is purple text${r}
#
# echo -e "${b}${mag}This is bold magenta text${r}
#
# NOTE: Make sure to reset the color at the end of the string.
#
# AVAILABLE TEXT COLORS
#
# COLOR SHORTCUT
# white wht
# black blk
# grey gry
# red red
# green grn
# blue blu
# cyan cyn
# yellow yel
# orange org
# magenta mag
# purple pur
#
# bold b
# reset r
#
# To generate a heading with a colored background use:
#
# heading <color> "Heading Text"
#
# For a heading with bold text use:
#
# bheading <color> "Heading Text"
#
# For a heading with a randomly selected color use:
#
# heading random "Heading Text"
#
# AVAILABLE HEADING COLORS
#
# COLOR SHORTCUT
# random rnd
# grey gry
# charcoal chr
# red red
# green grn
# lime lim
# aquamarine aqm
# olive olv
# blue blu
# sky sky
# cyan cyn
# agua aqa
# goldenrod gdr
# yellow yel
# coral crl
# orange org
# pink pnk
# lavender lav
# magenta mag
# purple pur
#
# The above colors can be used to generate your own colored strings.
# For background colors just prefix "bg" to the color.
#
# echo -e "${bgred}${b}${wht}Red background with bold white text${r}"
#
#-----------------------------------------------------------------------------------
reset="\e[0m" # Reset all color values to default
bold="\e[1m" # Bold
# TEXT COLORS ----------------------------------------------------------------------
white="\e[97m" # White
black="\e[38;5;232m" # Black
grey="\e[37m" # Grey
red="\e[91m" # Red
green="\e[92m" # Green
blue="\e[94m" # Blue
cyan="\e[96m" # Cyan
yellow="\e[93m" # Yellow
orange="\e[38;5;208m" # Orange
magenta="\e[95m" # Magenta
purple="\e[38;5;53m" # Purple
# BACKGROUND COLORS ----------------------------------------------------------------
bg_black="\e[40m" # Black
bg_grey="\e[48;5;240m" # Grey
bg_charcoal="\e[48;5;237m" # Charcoal
bg_red="\e[48;5;1m" # Red
bg_green="\e[48;5;22m" # Green
bg_lime="\e[48;5;40m" # Lime
bg_aquamarine="\e[48;5;120m" # Aquamarine
bg_olive="\e[48;5;58m" # Olive
bg_blue="\e[44m" # Blue
bg_sky="\e[48;5;25m" # Sky
bg_cyan="\e[46m" # Cyan
bg_aqua="\e[48;5;87m" # Aqua
bg_goldenrod="\e[48;5;220m" # Goldenrod
bg_yellow="\e[42m" # Yellow
bg_coral="\e[48;5;208m" # Coral
bg_orange="\e[48;5;202m" # Orange
bg_pink="\e[48;5;207m" # Pink
bg_lavender="\e[48;5;141m" # Lavender
bg_magenta="\e[48;5;90m" # Magenta
bg_purple="\e[48;5;53m" # Purple
bg_white="\e[107m" # White
# COLOR SHORTCUTS ------------------------------------------------------------------
r=$reset
b=$bold
blk=$black
gry=$grey
grn=$green
yel=$yellow
org=$orange
mag=$magenta
pur=$purple
cyn=$cyan
wht=$white
bgblk=$bg_black
bggry=$bg_grey
bgchr=$bg_charcoal
bgred=$bg_red
bggrn=$bg_green
bglim=$bg_lime
bgaqm=$bg_aquamarine
bgolv=$bg_olive
bgblu=$bg_blue
bgsky=$bg_sky
bgcyn=$bg_cyan
bgaqa=$bg_aqua
bggdr=$bg_goldenrod
bgyel=$bg_yellow
bgcrl=$bg_coral
bgorg=$bg_orange
bgpnk=$bg_pink
bglav=$bg_lavender
bgmag=$bg_magenta
bgpur=$bg_purple
bgwht=$bg_white
# HEADING FUNCTION -----------------------------------------------------------------
heading() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo 'Usage: heading <color> "Heading Text"'
exit 1
fi
if [ -z "$3" ]; then
weight=""
else
weight=$bold
fi
local color=${1,,} # lowercase
local hding=${2} # capture heading
local hdlen=${#hding} # heading length
local twidt=$(tput cols) # terminal width
# Set the minimum width to match length of the heading
if [ ! $twidt -gt $hdlen ]; then
twidt=$hdlen
fi
# Calculate the padding necessary on either side of the heading
l=$(( twidt - hdlen ))
d=$(( l / 2 ))
local padding=""
for i in $(seq 1 ${d}); do
padding+=" "
done
# Thanks to Bash's auto-rounding, depending on the length of the
# terminal relative to the length of the heading we might end up
# one character off. To compensate we add one space if necessary
local padextra=""
local padlenth=${#padding}
local totlenth=$(( padlenth * 2 + hdlen ))
if [ $twidt -ne $totlenth ]; then
padextra=" ";
fi
# Random color generator
if [ "$color" == 'rnd' ] || [ "$color" == "rand" ] || [ "$color" == "random" ]; then
colors=(
"gry"
"chr"
"red"
"grn"
"lim"
"aqm"
"olv"
"blu"
"sky"
"cyn"
"aqa"
"gdr"
"yel"
"crl"
"org"
"pnk"
"lav"
"mag"
"pur"
)
color=${colors[$RANDOM % ${#colors[@]}]}
fi
# White text: \e[97m
# Black text: \e[38;5;232m
# Bold: \e[1m
case "$color" in
grey | gry) color="${bggry}${white}" ;;
charcoal | chr) color="${bgchr}${white}" ;;
red) color="${bgred}${white}" ;;
green | grn) color="${bggrn}${white}" ;;
lime | lim) color="${bglim}${black}" ;;
aquamarine | aqm) color="${bgaqm}${black}" ;;
olive | olv) color="${bgolv}${white}" ;;
blue | blu) color="${bgblu}${white}" ;;
sky) color="${bgsky}${white}" ;;
cyan | cyn) color="${bgcyn}${white}" ;;
aqua | aqa) color="${bgaqa}${black}" ;;
goldenrod | gdr) color="${bggdr}${black}" ;;
yellow | yel) color="${bgyel}${black}" ;;
coral| crl) color="${bgcrl}${white}" ;;
orange | org) color="${bgorg}${white}" ;;
pink | pnk) color="${bgpnk}${white}" ;;
lavender | lav) color="${bglav}${black}" ;;
magenta | mag) color="${bgmag}${white}" ;;
purple | pur) color="${bgpur}${white}" ;;
*) color="${bggrn}${white}" ;;
esac
echo
echo -e "${color}${padding}${weight}${hding}${padding}${padextra}${reset}"
echo
}
# BOLD HEADING FUNCTION ------------------------------------------------------------
function bheading() {
heading "$1" "$2" "bold"
}
# TESTS ----------------------------------------------------------------------------
# echo -e "${gry}Grey text${r}"
# echo -e "${b}${gry}Bold Grey text${r}"
# echo -e "${grn}Green text${r}"
# echo -e "${b}${grn}Bold Green text${r}"
# echo -e "${yel}Yellow text${r}"
# echo -e "${b}${yel}Bold Yellow text${r}"
# echo -e "${org}Orange text${r}"
# echo -e "${b}${org}Bold Orange text${r}"
# echo -e "${mag}Magenta text${r}"
# echo -e "${b}${mag}Bold Magenta text${r}"
# echo -e "${pur}Purple text${r}"
# echo -e "${b}${pur}Bold Purple text${r}"
# echo -e "${cyn}Cyan text${r}"
# echo -e "${b}${cyn}Bold Cyan text${r}"
# echo -e "${wht}White text${r}"
# echo -e "${b}${wht}Bold White text${r}"
# heading rnd "Random" # Random color
# heading gry "Grey"
# heading chr "Charcoal"
# heading red "Red"
# heading grn "Green"
# heading lim "Lime"
# heading aqm "Aquamarine"
# heading olv "Olive"
# heading blu "Blue"
# heading sky "Sky"
# heading cyn "Cyan"
# heading aqa "Aqua"
# heading gdr "Goldenrod"
# heading yel "Yellow"
# heading crl "Coral"
# heading org "Orange"
# heading pnk "Pink"
# heading lav "Lavender"
# heading mag "Magenta"
# heading pur "Purple"