Skip to content

Commit 879f91d

Browse files
committed
say module updated
1 parent e368d78 commit 879f91d

File tree

1 file changed

+89
-63
lines changed

1 file changed

+89
-63
lines changed

src/say.sh

Lines changed: 89 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,114 +3,140 @@
33
Clear="\033[0m";
44
# Suppressed Variables
55
if (( 1<2 )); then
6-
# Colors
7-
Gora="\033[1;97m"
6+
# ----------
7+
# Colors.
8+
# Gora="\033[0;97m"
9+
# ----------
810
# BG Colors.
9-
BGRed="\033[1;41m";
10-
BGGreen="\033[1;42m";
11-
BGYelo="\033[1;43m";
11+
# BGRed="\033[1;41m";
12+
# BGGreen="\033[1;42m";
13+
# BGYelo="\033[1;43m";
14+
# BGBold="\033[1m";
15+
# ----------
1216
# Status Colors.
13-
StatusRed="${BGRed}${Gora}";
14-
StatusGreen="${BGGreen}${Gora}";
15-
StatusYelo="${BGYelo}${Gora}";
17+
# StatusRed="${BGRed}${Gora}";
18+
# StatusGreen="${BGGreen}${Gora}";
19+
# StatusYelo="${BGYelo}${Gora}";
20+
# ----------
1621
# Global variables.
17-
Source="'([^']*)'";
22+
Pattern="'([^']*)'";
23+
# Color plate.
24+
COLOR_STRIP=(
25+
"\033[0;31m" # Red
26+
"\033[0;32m" # Green
27+
"\033[0;33m" # Yelo
28+
"\033[0;34m" # Blue
29+
"\033[0;35m" # Genta
30+
"\033[0;36m" # Cyan
31+
);
1832
fi
1933

20-
# Colors
21-
Red="\033[1;31m";
22-
Green="\033[1;32m";
23-
Yelo="\033[1;33m";
24-
2534
# say.gencolor()
2635
# Gives you a random color code.
2736
say.gencolor(){
28-
local STRIP=(
29-
"\033[1;31m"
30-
"\033[1;32m"
31-
"\033[1;33m"
32-
"\033[1;34m"
33-
"\033[1;35m"
34-
"\033[1;36m"
35-
);
36-
echo "${STRIP[$(( RANDOM % ${#STRIP[@]} ))]}";
37-
return 0;
37+
echo "${COLOR_STRIP[$(( RANDOM % ${#COLOR_STRIP[@]} ))]}";
3838
}
3939

40-
# say(str) -> str
41-
# Say strings with syntax highlighting feature.
42-
say(){
43-
local String="${1}";
44-
local STRIP=(${String});
45-
local String='';
46-
for Uri in "${STRIP[@]}"
40+
# _say.colorizeString(str,@--gencolor,@--color <color>) ~ str
41+
# Create and returns you raw colorized strings.
42+
# Args:
43+
# str (str): takes string as arg.
44+
# --gencolor (obj,optional): adds colors randomly to syntax.
45+
# --color <color> (str,optional): adds given color to syntax.
46+
_say.colorizeString(){
47+
local ARGString="${1}";
48+
local isGenColorEnabled=1;
49+
local Sentence=(${ARGString});
50+
local String;
51+
shift;
52+
case "${1}" in
53+
--gencolor)
54+
local isGenColorEnabled=0;
55+
;;
56+
--color)
57+
local Color="${2}";
58+
;;
59+
esac
60+
for Shabd in "${Sentence[@]}"
4761
do
48-
if [[ "${Uri}" =~ ${Source} ]]; then
62+
if [[ "${Shabd}" =~ ${Pattern} ]]; then
63+
[[ "${isGenColorEnabled}" == 1 ]] || \
4964
local Color="$(say.gencolor)";
50-
local String+="${Color}${Uri}${Clear} ";
51-
else local String+="${Uri} ";
65+
local String+="${Color}${Shabd}${Clear} ";
66+
else
67+
local String+="${Shabd} ";
5268
fi
5369
done
70+
echo "${String}";
71+
}
72+
73+
# say(str) ~ str
74+
# Say strings with syntax highlighting feature.
75+
say(){
76+
local String="$(_say.colorizeString "${1}" --gencolor)";
5477
echo -e "${String}";
55-
return 0;
5678
}
5779

5880
_status(){
5981
case "${1}" in
6082
-error)
61-
local Color="\033[1;31m";
62-
local StatusColor="${StatusRed}";
63-
;;
64-
-success)
65-
local Color="\033[1;32m";
66-
local StatusColor="${StatusGreen}";
67-
;;
83+
local Color="\033[0;31m";
84+
local Header="ERR";
85+
;;
6886
-warn)
69-
local Color="\033[1;33m";
70-
local StatusColor="${StatusYelo}";
71-
;;
87+
local Color="\033[0;33m";
88+
local Header="WARN";
89+
;;
90+
-success)
91+
local Color="\033[0;32m";
92+
local Header="INFO";
93+
;;
7294
esac
73-
local String="${2}";
74-
local xSTRIP=(${String});
75-
local String='';
76-
for Uri in "${xSTRIP[@]}"
77-
do
78-
if [[ "${Uri}" =~ ${Source} ]]; then
79-
local String+="${Color}${Uri}${Clear} ";
80-
else local String+="${Uri} ";
81-
fi
82-
done
83-
echo -e "${StatusColor} INFO ${Clear}${String}";
84-
return 0;
95+
local String="$(_say.colorizeString "${2}" --color "${Color}")";
96+
echo -ne "[${Color} ${Header} ${Clear}]: ";
97+
echo -e "${String}";
8598
}
8699

87-
# say.error(str) -> str
100+
# say.error(str) ~ str
88101
# Says error to terminal.
89102
say.error(){
90103
_status -error "${@}";
91104
}
92105

93-
# say.warn(str) -> str
106+
# say.warn(str) ~ str
94107
# Says warning to terminal.
95108
say.warn(){
96109
_status -warn "${@}";
97110
}
98111

99-
# say.success(str) -> str
112+
# say.success(str) ~ str
100113
# Says success to terminal.
101114
say.success(){
102115
_status -success "${@}";
103116
}
104117

105-
# say.checkStatus(status) -> str
118+
# log(str) ~ str
119+
# Say log to the user.
120+
log(){
121+
case "${?}" in
122+
0)
123+
_status -success "${@}";
124+
;;
125+
*)
126+
_status -error "${@}";
127+
;;
128+
esac
129+
}
130+
131+
# say.checkStatus(status) ~ str
106132
# This prints success or failure according to exit code.
107133
# Args:
108134
# status (int) > takes exit code.
109135
say.checkStatus(){
110136
if [[ "${1}" == 0 ]]; then
111-
echo -e " ${StatusGreen} SUCCESS ${Clear}";
137+
echo -e " ~ [${COLOR_STRIP[1]} SUCCESS ${Clear}]";
112138
else
113-
echo -e " ${StatusRed} FAILED ${Clear}";
139+
echo -e " ~ [${COLOR_STRIP[0]} FAILED ${Clear}]";
114140
exit 1;
115141
fi
116142
}

0 commit comments

Comments
 (0)