-
Notifications
You must be signed in to change notification settings - Fork 0
/
help_alice.sh
executable file
·189 lines (166 loc) · 3.55 KB
/
help_alice.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
#!/usr/bin/env bash
export NEWT_COLORS='
root=black,magenta
border=white,black
title=white,black
button=white,black
entry=black,cyan
'
width=50
height=15
# CTRL+C trap
trap ctrl_c INT
function ctrl_c() {
tput cnorm
exit 0
}
function dependencies() {
# Verify if whiptail is installed
if ! [ -x "$(command -v whiptail)" ]; then
echo "Error: whiptail is not installed." >&2
echo "Please, install if you want to use the TUI"
echo "Don't know how to install it? Let me help you:"
distribution=$(lsb_release -d 2>/dev/null || echo "ups Linux" | awk '{print $2}')
echo -e "\t https://lmddgtfy.net/?q=How%20to%20install%20whiptail%20on%20$distribution"
cli=true
fi
}
function help_panel() {
echo -e "help_alice.sh"
echo -e "Usage: help_alice.sh [options]"
echo -e "Options:"
echo -e "\t -t <text> Text to convert"
echo -e "\t -c Use CLI mode"
echo -e "\t -f Fast mode, no progress bar"
echo -e "\t -h Show this help panel"
exit 0
}
function false_wait() {
# Just a false wait so seams that the program is calculating something
{
for ((i = 0; i <= 100; i += 5)); do
sleep 0.02
echo $i
done
} | whiptail --gauge "\nPerforming operations" $height $width 0
}
function tui_mode() {
title="Help Alice"
tput civis
if [ -z "$args" ]; then
args=$(whiptail --inputbox "\nPlease, provide a text to convert" $height $width Jabberwocky --title "$title" 3>&1 1>&2 2>&3)
fi
# Check for any errors from whiptail
if [ $? -ne 0 ]; then
return
fi
string_to_numbers "${args}"
while true; do
perform_operations
whiptail --title "$title" --yes-button "Change text" --no-button "Exit program" --yesno "$menu" $height $width
if [ "$?" = 1 ]; then
whiptail --title "$title" --msgbox "Goodluck!" $height $width
tput cnorm
exit 0
fi
args=$(whiptail --title "Change Text" --inputbox "\n$menu\nProvide a new text to convert:" $height $width 3>&1 1>&2 2>&3)
string_to_numbers "$args"
done
}
function cli_mode() {
if [ -z "$args" ]; then
echo "Please, provide a text to convert"
read -rp "Text: " args
fi
string_to_numbers "$args"
perform_operations
echo -e "$menu\n\nChoose an operation:"
PS3="Choose a number: "
while true; do
select operation in ChangeText Exit; do
case $operation in
ChangeText)
echo "Provide a new text to convert"
read -rp "Text: " args
string_to_numbers "$args"
;;
Exit)
echo "Goodluck!"
exit 0
;;
*)
echo "Invalid operation"
;;
esac
break
done
done
}
function string_to_numbers() {
args=$*
args=${args^^}
args=${args// /}
# Eliminate all non-alphabetic characters
args=${args//[^A-Z]/}
numbers=()
echo "Captured argument: $args"
echo "Convertion to numbers:"
for i in $(seq 0 $((${#args} - 1))); do
char=${args:$i:1}
num=$(($(printf "%d" "'$char") - 64))
echo -e "\t$char = $num"
numbers+=("$num")
done
}
function perform_operations() {
sum=$(
IFS=+
echo "$((${numbers[*]}))"
)
substract=$(
IFS=-
echo "$((${numbers[*]}))"
)
multiply=$(
IFS=*
echo "$((${numbers[*]}))"
)
divide=$(
IFS=/
echo "$((${numbers[*]}))"
)
menu="Text: $args\nNumbers: ${numbers[*]}\nOperation results:\n Sum: $sum\n Substract: $substract\n Multiply: $multiply\n Divide: $divide"
if [ "$fast" = false ]; then
false_wait
fi
}
args=""
cli=false
fast=true
dependencies
while getopts "t:chf" arg; do
case $arg in
t)
args=$OPTARG
;;
c)
cli=true
;;
f)
fast=true
;;
h)
help_panel
;;
*)
echo "Invalid option $arg"
;;
esac
done
# Main
if [ "$cli" = true ]; then
cli_mode
else
tui_mode
fi
tput cnorm