-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-riddle-me.plugin.zsh
69 lines (62 loc) · 2.72 KB
/
zsh-riddle-me.plugin.zsh
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
#!/bin/zsh
##################################################################################
# Shellscript : zsh-riddle-me.plugin.zsh .--. #
# Author : Venkata Kolagotla <venkata.kolagotla@gmail.com> |ö_ö | #
# Created : 10-06-2020 |\ü/ | #
# Last Updated: 17-07-2021 // \ \ #
# Requires : zsh, oh-my-zsh, curl (| | ) #
# Category : zsh plugin /'\_ _/`\\#
# Version : v0.1.7 \___)=(___//#
##################################################################################
# Description : print random riddles
# Usage : riddle-me
##################################################################################
# custom zsh plugin for random riddles
function riddle-me {
TXT=$(curl -s --connect-timeout 2 "https://goodriddlesnow.com/riddles/random" | iconv -c -f ISO-8859-1 -t UTF-8 | grep "<p><strong>")
# formatted riddle
TXT_R=${TXT#*'<p><strong>Question: </strong>'}
R=${TXT_R%'><ul class="inline-list print-hide">'*}
R=$(echo "$R" | sed -e 's/<[^>]*>//g' | sed -e 's|?</div||g' | sed -e 's|</div||g')
# formatted answer
TXT_A=${TXT_R#*'<p><strong>Answer: </strong>'}
A=${TXT_A%'</p><div '*}
A=$(echo "$A" | sed -e 's/<[^>]*>//g')
# hint (number of words)
H=$(echo $A | wc -w)
print -P "Riddle: %F{1}${R}??%f"
echo -n "Enter 'A/h' to see the Answer/Hint or enter your Answer: "
read input
echo "You entered: $input"
case $input in
[Aa])
print -P "And the answer is: %F{2}${A}%f"
;;
[Hh])
if [[ $H -eq 2 ]] && [[ $A =~ ^"A"||"An"||"The" ]]; then
echo -n "\033[33mHint:\033[0m The answer has $H words in it and one of them is probably an article (A, An, The)...\n"
else
echo -n "\033[33mHint:\033[0m The answer has $H words in it... \n"
fi
echo -n "Do you want to see the answer now?(Y/n): "
read user_ans
case $user_ans in
[Yy])
print -P "And the answer is: %F{2}${A}%f"
;;
[Nn])
echo -n "Take your time and at the end type something to see the answer... "
read something
echo "You entered: $something"
print -P "And the answer is: %F{2}${A}%f"
;;
*)
print -P "And the answer is: %F{2}${A}%f"
;;
esac
;;
*)
print -P "The Answer is: %F{2}${A}%f"
;;
esac
}