Skip to content

Commit 7749aa0

Browse files
refactor generater
1 parent c673567 commit 7749aa0

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

β€Žgenerator.sh

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@
33
set -e
44
shopt -s expand_aliases
55

6-
distFolder=dist
7-
distPrefix=ColorEcho
6+
distFolder="dist"
7+
distPrefix="ColorEcho"
88
table="color table.txt"
99

10-
if [ ! -r "$distFolder/$distPrefix".bash ] || [ ! -s "$distFolder/$distPrefix".bash ]; then
11-
echo "$distFolder$distPrefix".bash is not readable, fallback to use origin echo
10+
if [ ! -r "${distFolder}/${distPrefix}.bash" ] || [ ! -s "${distFolder}/${distPrefix}.bash" ]; then
11+
echo "${distFolder}${distPrefix}.bash" is not readable, fallback to use origin echo
1212
alias echo.Red='echo'
1313
alias echo.Green='echo'
1414
alias echo.BoldYellow='echo'
1515
else
1616
# use ColorEcho
17-
. "$distFolder/$distPrefix".bash
17+
. "${distFolder}/${distPrefix}.bash"
18+
command -v echo.Red &> /dev/null || alias echo.Red='echo'
19+
command -v echo.Green &> /dev/null || alias echo.Green='echo'
20+
command -v echo.BoldYellow &> /dev/null || alias echo.BoldYellow='echo'
1821
fi
1922

2023
mkdir -p $distFolder
2124
if [ ! -w "$distFolder" ]; then
22-
echo.Red Dist folder - $distFolder is not writable, exit ...
25+
echo.Red "Dist folder - \"$distFolder\" is not writable, exit ..."
2326
exit 1;
2427
fi
2528

26-
echo.Green ColorEcho generator start!
29+
echo.Green "ColorEcho generator start!"
2730

2831
for shell in sh bash fish ksh zsh
2932
do
30-
echo.BoldYellow Generating ColorEcho for $shell shell ...
33+
echo.BoldYellow "Generating ColorEcho for $shell shell ..."
3134
#shell specify configs and tricks
32-
case $shell in
35+
case "$shell" in
3336
"bash" | "zsh")
3437
fn='function '
3538
dot='.'
@@ -71,15 +74,15 @@ do
7174
para='@'
7275
esac
7376

74-
newDist="$distFolder/$distPrefix.$shell"
75-
touch $newDist
77+
newDist="${distFolder}/${distPrefix}.${shell}"
78+
touch "$newDist"
7679
if [ ! -w "$newDist" ]; then
77-
echo.Red dist file - "$newDist" is not writable, exit ...
80+
echo.Red "dist file - \"$newDist\" is not writable, exit ..."
7881
exit 1
7982
fi
8083

81-
echo "#!/usr/bin/env $shell" > $newDist
82-
for color in `cat "$table" | awk '{print $1}'`
84+
echo "#!/usr/bin/env $shell" > "$newDist"
85+
for color in $(awk '{print $1}' "$table")
8386
do
8487
#light or not
8588
for light in "" "Light"
@@ -100,24 +103,26 @@ do
100103
#underline or not
101104
for underLine in "" "UL"
102105
do
103-
echo "" >> $newDist
104-
echo "$fn""echo$dot$light$bold$underLine$color$brackets" >> $newDist
105-
if [ "$underLine" = "" ]; then
106-
ulCode=
107-
else
108-
ulCode='4;'
109-
fi
110-
#write the code down
111-
echo "$startSym" >> $newDist
112-
echo " $echo"' -e "\e['"$ulCode$bCode$code"$(grep $color "$table" | awk '{print $2}')'m$'$para'\e[m"' >> $newDist
113-
echo "$endSym" >> $newDist
106+
{
107+
echo ""
108+
echo "${fn}echo${dot}${light}${bold}${underLine}${color}${brackets}"
109+
if [ "$underLine" = "" ]; then
110+
ulCode=
111+
else
112+
ulCode='4;'
113+
fi
114+
#write the code down
115+
echo "$startSym"
116+
echo " $echo"' -e "\e['"${ulCode}${bCode}${code}"$(grep $color "$table" | awk '{print $2}')'m$'$para'\e[m"'
117+
echo "$endSym"
118+
} >> "$newDist"
114119
done
115120
done
116121
done
117122
done
118123

119124
#rainbow output relys on lolcat
120-
fnName="$fn echo"$dot"Rainbow$brackets"
125+
fnName="${fn} echo${dot}Rainbow${brackets}"
121126
if [ "$shell" = "fish" ]; then
122127
ifCond="if type lolcat > /dev/null"
123128
else
@@ -136,7 +141,7 @@ $endSym
136141
LOLCAT
137142

138143
#echo.Reset to remove color code on output
139-
fnName="$fn echo"$dot"Reset$brackets"
144+
fnName="${fn} echo${dot}Reset${brackets}"
140145
cat << LOLCAT >> "$newDist"
141146
$fnName
142147
$startSym
@@ -146,4 +151,4 @@ LOLCAT
146151

147152
done
148153

149-
echo.Green ColorEcho generator end!
154+
echo.Green "ColorEcho generator end!"

0 commit comments

Comments
Β (0)