-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
generator.sh
executable file
Β·208 lines (192 loc) Β· 5.75 KB
/
generator.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
#!/usr/bin/env bash
set -e
shopt -s expand_aliases
distFolder="dist"
distPrefix="ColorEcho"
table="color table.txt"
# use ColorEcho
bashDist="${distFolder}/${distPrefix}.bash"
# shellcheck source=dist/ColorEcho.bash
if [ ! -r "$bashDist" ] || [ ! -s "$bashDist" ] || ! . "$bashDist" &> /dev/null; then
echo "${bashDist}" is not usable, fallback to use origin echo
alias echo.BoldRed='echo'
alias echo.BoldGreen='echo'
alias echo.BoldYellow='echo'
else
command -v echo.BoldRed &> /dev/null || alias echo.BoldRed='echo'
command -v echo.BoldGreen &> /dev/null || alias echo.BoldGreen='echo'
command -v echo.BoldYellow &> /dev/null || alias echo.BoldYellow='echo'
fi
mkdir -p "${distFolder}"
if [ ! -w "${distFolder}" ]; then
echo.BoldRed "Dist folder - \"${distFolder}\" is not writable, exit ..."
exit 1
fi
echo.BoldGreen "ColorEcho generator start!"
for shell in sh bash fish ksh zsh; do
{
echo.BoldYellow "Generating ColorEcho for ${shell} shell ..."
# shell specify configs and tricks
case "${shell}" in
"bash" | "zsh")
fn='function '
dot='.'
echo='echo -e'
startSym=' {'
endSym='}'
endIf='fi'
brackets='()'
para='*'
;;
"ksh")
fn='function '
dot=
echo='echo -e'
startSym=' {'
endSym='}'
endIf='fi'
brackets=
para='*'
;;
"fish")
fn='function '
dot='.'
echo='echo -e'
startSym=
endSym='end'
endIf='end'
brackets=
para='argv'
;;
"sh")
fn=
dot=
# shellcheck disable=SC2016
echo='$ECHO'
startSym=' {'
endSym='}'
endIf='fi'
brackets='()'
para='*'
;;
esac
newDist="${distFolder}/${distPrefix}.${shell}"
touch "${newDist}"
if [ ! -w "${newDist}" ]; then
echo.BoldRed "dist file - \"${newDist}\" is not writable, exit ..."
exit 1
fi
tempDist="$(mktemp --suffix=ColorEcho)"
echo "#!/usr/bin/env ${shell}" > "${tempDist}"
cat << SH_ECHO >> "${tempDist}"
# ColorEchoForShell
# https://github.com/PeterDaveHello/ColorEchoForShell
# Copyright (C) 2015 ~ Peter Dave Hello
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
SH_ECHO
if [ "${shell}" = "sh" ]; then
cat << SH_ECHO >> "${tempDist}"
if [ "\$(uname)" = "FreeBSD" ]; then
ECHO="echo -e"
elif [ "\$(uname)" = "Darwin" ]; then
ECHO="echo"
else
ECHO="/bin/echo -e"
fi
SH_ECHO
fi
awk '{print $1}' "${table}" | while IFS= read -r color; do
# light or not
for light in "" "Light"; do
if [ "${light}" = "" ]; then
code=3
else
code=9
fi
# style like bold, italic, underline, blinking, strikethrough
for style in "" "Bold" "I" "UL" "BL" "ST"; do
case "${style}" in
"Bold") styleCode='1;' ;;
"I") styleCode='3;' ;;
"UL") styleCode='4;' ;;
"BL") styleCode='5;' ;;
"ST") styleCode='9;' ;;
"" | *) styleCode= ;;
esac
for style2 in "" "Bold" "I" "UL" "BL" "ST"; do
if [ "${style}" != "${style2}" ]; then
case "${style2}" in
"Bold") finalStyleCode="${styleCode}1;" ;;
"I") finalStyleCode="${styleCode}3;" ;;
"UL") finalStyleCode="${styleCode}4;" ;;
"BL") finalStyleCode="${styleCode}5;" ;;
"ST") finalStyleCode="${styleCode}9;" ;;
"" | *) finalStyleCode="${styleCode}" ;;
esac
else
style2=
finalStyleCode="${styleCode}"
fi
echoFunction="$(printf "%secho%s%s%s%s%s" "${fn}" "${dot}" "${light}" "${style}" "${style2}" "${color}")"
if ! grep -q "${echoFunction}" "${tempDist}"; then
{
echo ""
printf "%s%s" "${echoFunction}" "${brackets}"
# write the code down
echo "${startSym}"
echo " ${echo}"' "\\033['"${finalStyleCode}${code}""$(grep "${color}" "${table}" | awk '{print $2}')"'m$'"${para}"'\\033[m"'
echo "${endSym}"
} >> "${tempDist}"
fi
done
done
done
done
# rainbow output relys on lolcat
fnName="${fn}echo${dot}Rainbow${brackets}"
case "${shell}" in
"fish")
ifCond="if command -v lolcat > /dev/null"
;;
"ksh")
ifCond='if command -v lolcat 2> /dev/null >&2; then'
;;
*)
ifCond='if command -v lolcat > /dev/null 2>&1; then'
;;
esac
cat << LOLCAT >> "${tempDist}"
${fnName}${startSym}
${ifCond}
echo "\$${para}" | lolcat
else
echo "\$${para}"
${endIf}
${endSym}
LOLCAT
# echo.Reset to remove color code on output
fnName="${fn}echo${dot}Reset${brackets}"
cat << RESET >> "${tempDist}"
${fnName}${startSym}
echo "\$${para}" | tr -d '[:cntrl:]' | sed -E "s/\\\\[((;)?[0-9]{1,3}){0,3}m//g" | xargs
${endSym}
RESET
mv -f "${tempDist}" "${newDist}"
} &
done
wait
echo.BoldGreen "ColorEcho generator end!"