Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit 9b3a673

Browse files
display expected output on mismatch
check for invalid argument and terminal's color capability, use command to override aliases/functions
1 parent 02df0b1 commit 9b3a673

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

exercises/GNU_grep/solve

+26-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ ref_file="../.ref_solutions/$dir_name.txt"
33
sol_file="../$dir_name.txt"
44
tmp_file='../.tmp.txt'
55

6-
# color output - need to check if Terminal supports!
7-
red=$(tput setaf 1)
8-
green=$(tput setaf 2)
9-
blue=$(tput setaf 4)
10-
clr_color=$(tput sgr0)
6+
# color output
7+
tcolors=$(tput colors)
8+
if [[ -n $tcolors && $tcolors -ge 8 ]]; then
9+
red=$(tput setaf 1)
10+
green=$(tput setaf 2)
11+
blue=$(tput setaf 4)
12+
clr_color=$(tput sgr0)
13+
else
14+
red=''
15+
green=''
16+
blue=''
17+
clr_color=''
18+
fi
1119

1220
sub_sol=0
1321
if [[ $1 == -s ]]; then
@@ -18,10 +26,14 @@ elif [[ $1 == -q ]]; then
1826
# or show only the (unanswered)? question to be solved next
1927
cat "$sol_file"
2028
return
29+
elif [[ -n $1 ]]; then
30+
echo -e 'Unknown option...Exiting script'
31+
return
2132
fi
2233

2334
count=0
2435
sol_count=0
36+
err_count=0
2537
while IFS= read -u3 -r ref_line && read -u4 -r sol_line; do
2638
if [[ "${ref_line:0:9}" == Solution: ]]; then
2739
(( count++ ))
@@ -31,7 +43,7 @@ while IFS= read -u3 -r ref_line && read -u4 -r sol_line; do
3143
sub_sol=0
3244
fi
3345

34-
if [[ $(eval "${ref_line:10}") == $(eval "$sol_line") ]]; then
46+
if [[ $(eval "command ${ref_line:10}") == $(eval "command $sol_line") ]]; then
3547
(( sol_count++ ))
3648
# use color if terminal supports
3749
echo '---------------------------------------------'
@@ -40,6 +52,14 @@ while IFS= read -u3 -r ref_line && read -u4 -r sol_line; do
4052
echo "${green}Reference solution:${clr_color} ${ref_line:10}"
4153
echo '---------------------------------------------'
4254
else
55+
(( err_count++ ))
56+
if [[ $err_count == 1 && -n $sol_line ]]; then
57+
echo '---------------------------------------------'
58+
echo "Mismatch for question $count:"
59+
echo "$(tput bold)${red}Expected output is:${clr_color}$(tput rmso)"
60+
eval "command ${ref_line:10}"
61+
echo '---------------------------------------------'
62+
fi
4363
sol_line=''
4464
fi
4565
fi

0 commit comments

Comments
 (0)