-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.shell
220 lines (178 loc) · 6.88 KB
/
colors.shell
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
#!/bin/env bash
#
# Copyright (c) Roberto A. Foglietta, 2023
#
# Authors:
# Roberto A. Foglietta <roberto.foglietta@gmail.com>
#
# SPDX-License-Identifier: MIT
#
#set -x && resetx="set +x" # RAF: source, paired with set +x at the end of file
#set -Eeuo pipefail # RAF: this is only for test with every restriction possible
# bash -c "source colors.shell && source colors.shell"
set -u # RAF: only for devel, comment in main
###############################################################################
#
# Composition rule for printing colored text on the console
#
# echo -e "${mode}${textcolor}${text}${reset}"
#
# to add a background color this is the rule with basic codes
#
# echo -e "${mode}${background}${textcolor}${text}${reset}"
#
# or using the auto-generated codes like in these example
#
# echo -e "${BLRED} Hello world in light red! ${CRST}"
#
# auto-generated codes are also available in lower-case format
#
# echo -ne "${dgrnowht} Ciao in dark green over white. ${crst}\n"
#
###############################################################################
if [ ${GFRELOAD:-0} -eq 0 ]; then
echo "${BASH_SOURCE[0]##*/} not reloaded, try again with 'GFRELOAD=1 source'"
return 1
fi
type colors_codes_generation 2>&1 | grep -q "is a function" && return 0
trap 'echo -e "\n${ERROR:-ERROR}: in '${BASH_SOURCE[0]##*/}\
' at line ${LINENO} occured, try again with set -x\n" >&2' ERR
###############################################################################
# RAF: restricted shell cannot redirect to file but it is fine
# to redirect to a open file descriptor towards /dev/null
# So, also exec could fail but in gitshell &3 is just open
if [ ${GITSHLVL:-0} -lt 1 ]; then
exec 3>/dev/null || return $?
fi
echo "Loading ${BASH_SOURCE[0]##*/} into current bash enviroment"
###############################################################################
# The echo needs -e to print colored output
function echo() {
command echo -e "$@"
}
export -f echo
# The export of the basic variables statically defined
optx="x" # uncomment to export color codes
# To bound variable because set -u requires it
optx=${optx:-}
test "${optx}" != "x" && optx=""
###############################################################################
if true; then
# Displaying Modes
declare -r${optx} NRM="\033[0" || : # normal
declare -r${optx} BLD="\033[1" || : # bold
declare -r${optx} DRK="\033[2" || : # dark
declare -r${optx} ITL="\033[3" || : # italic
declare -r${optx} UDL="\033[4" || : # underline
declare -r${optx} FLS="\033[5" || : # blinking -> flashing
declare -r${optx} RVS="\033[7" || : # reverse
declare -r${optx} HDN="\033[8" || : # hidden
declare -r${optx} SRK="\033[9" || : # strikethrough
# Regular Colors
declare -r${optx} BLK=";30m" || : # black
declare -r${optx} RED=";31m" || : # red
declare -r${optx} GRN=";32m" || : # green
declare -r${optx} YLW=";33m" || : # yellow
declare -r${optx} BLU=";34m" || : # blue
declare -r${optx} PRL=";35m" || : # purple
declare -r${optx} CYN=";36m" || : # cyan
declare -r${optx} WHT=";37m" || : # white
# Backgound Colors
declare -r${optx} OBLK=";40" || : # black
declare -r${optx} ORED=";41" || : # red
declare -r${optx} OGRN=";42" || : # green
declare -r${optx} OYLW=";43" || : # yellow
declare -r${optx} OBLU=";44" || : # blue
declare -r${optx} OPRL=";45" || : # purple
declare -r${optx} OCYN=";46" || : # cyan
declare -r${optx} OWHT=";47" || : # white
# High Intensity Colors
declare -r${optx} LBLK=";90m" || : # light_black
declare -r${optx} LRED=";91m" || : # light_red
declare -r${optx} LGRN=";92m" || : # light_green
declare -r${optx} LYLW=";93m" || : # light_yellow
declare -r${optx} LBLU=";94m" || : # light_blue
declare -r${optx} LPRL=";95m" || : # light_purple
declare -r${optx} LCYN=";96m" || : # light_cyan
declare -r${optx} LWHT=";97m" || : # ligth_white
# Color Reset
declare -r${optx} CRST="\033[0;0m" || : # reset
fi 2>&3
###############################################################################
# List of Colors and Displaying Modes
COLORS="BLK RED GRN YLW BLU PRL CYN WHT"
DMODES="NRM BLD DRK ITL UDL FLS RVS SRK"
# This declares the basic variables in lower case and exports them optx=x
if true; then
for i in $COLORS $DMODES CRST; do
eval declare -r${optx} '${i,,}'=\"\$$i\" || :
done
for i in L O; do
for j in $COLORS; do
eval declare -r${optx} '${i,,}${j,,}'=\"\${$i$j}\" ||:
done
done
fi 2>&3
# This declares the basic variables as read-onnly and exports them if optx=x
if false; then
declare -r${optx} ${COLORS} ${DMODES} CRST crst || :
for j in ${COLORS}; do
declare -r${optx} O$j L$j || :
done
unset j
fi 2>&3
# Auto-generating function for colors codes
function colors_codes_generation() {
local i j l p s b g k
for i in ${DMODES}; do
for j in ${COLORS}; do
for b in '' ${COLORS}; do
for l in '' L; do
p="${i:0:1}$l"
k="O$b"
k=${k%O}
echo -n "declare -r${optx} $p$j$k='"
g=${k:-''}
g=${g/O/\$O}
eval "printf '%s%s%s' \$$i $g \$$l$j"
g="$k + "
s=${b:-\\t}
s=${s//[A-Z]/}
echo "' || : $s\t# $i + ${g# + }$l$j"
done
done
done
done
}
# Auto-generation of colors codes
if true; then
eval "$(colors_codes_generation)"
eval "$(colors_codes_generation | tr '[A-Z]' '[a-z]')"
else
tmp=$(mktemp -p "${TMPDIR:-}")
colors_codes_generation >$tmp
colors_codes_generation | tr '[A-Z]' '[a-z]' >>$tmp
source $tmp 2>&3
rm -f $tmp
fi 2>&3
###############################################################################
# Relevant keyword coloured
if true; then
declare -r${optx} WARNING="${BLYLW}WARNING${CRST}" || :
declare -r${optx} ERROR="${BLRED}ERROR${CRST}" || :
declare -r${optx} DONE="${BLGRN}DONE${CRST}" || :
declare -r${optx} OK="${BLGRN}OK${CRST}" || :
declare -r${optx} KO="${BLRED}KO${CRST}" || :
declare -r${optx} ON="${BLGRN}ON${CRST}" || :
declare -r${optx} OFF="${BLRED}OFF${CRST}" || :
declare -r${optx} ENTER="${BLWHT}ENTER${CRST}" || :
declare -r${optx} USAGE="${BLPRL}USAGE${CRST}" || :
declare -r${optx} NOTICE="${BLCYN}NOTICE${CRST}" || :
declare -r${optx} PLAY="${BLGRN}PLAY${CRST}" || :
declare -r${optx} PAUSE="${BLYLW}PAUSE${CRST}" || :
declare -r${optx} STOP="${BLRED}STOP${CRST}" || :
fi 2>&3
###############################################################################
${resetx:-}
trap -- ERR
true