Skip to content

Commit e796a08

Browse files
committed
Convert huge case inside main loop in calls to functions.
Minimum code and whitestpace changes in this commit, to make reviewing easier. Cleanup will come in next commits.
1 parent 63a8b8b commit e796a08

File tree

1 file changed

+99
-91
lines changed

1 file changed

+99
-91
lines changed

share/cht.sh.txt

Lines changed: 99 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -221,55 +221,10 @@ else
221221
defpager=cat
222222
fi
223223

224-
TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
225-
trap 'rm -f $TMP1 $TMP2' EXIT
226-
trap 'true' INT
227-
228-
if ! [ -e "$HOME/.cht.sh/.hushlogin" ] && [ -z "$this_query" ]; then
229-
echo "type 'help' for the cht.sh shell help"
230-
fi
231-
232-
while true; do
233-
if [ "$section" != "" ]; then
234-
full_prompt="$prompt/$section> "
235-
else
236-
full_prompt="$prompt> "
237-
fi
238-
239-
input=$(
240-
rlwrap -H "$HOME/.cht.sh/history" -pgreen -C cht.sh -S "$full_prompt" sh "$0" --read | sed 's/ *#.*//'
241-
)
242-
243-
case "$input" in
244-
'?'|h|help)
245-
cat <<EOF
246-
help - show this help
247-
hush - do not show the 'help' string at start anymore
248-
cd LANG - change the language context
249-
copy - copy the last answer in the clipboard (aliases: yank, y, c)
250-
ccopy - copy the last answer w/o comments (cut comments; aliases: cc, Y, C)
251-
exit - exit the cheat shell (aliases: quit, ^D)
252-
id [ID] - set/show an unique session id ("reset" to reset, "remove" to remove)
253-
stealth - stealth mode (automatic queries for selected text)
254-
update - self update (only if the scriptfile is writeable)
255-
version - show current cht.sh version
256-
/:help - service help
257-
QUERY - space ceparated query staring (examples are below)
258-
cht.sh> python zip list
259-
cht.sh/python> zip list
260-
cht.sh/go> /python zip list
261-
EOF
262-
continue
263-
;;
264-
hush)
265-
mkdir -p $HOME/.cht.sh/ && touch $HOME/.cht.sh/.hushlogin && echo "Initial 'use help' message was disabled"
266-
continue
267-
;;
268-
cd)
224+
cmd_cd() {
225+
if [ $# -eq 0 ]; then
269226
section=""
270-
continue
271-
;;
272-
"cd "*)
227+
else
273228
new_section=$(echo "$input" | sed 's/cd *//; s@/*$@@; s@^/*@@')
274229
if [ -z "$new_section" ] || [ ".." = "$new_section" ]; then
275230
section=""
@@ -280,24 +235,18 @@ EOF
280235
echo "Invalid section: $new_section"
281236
echo "Valid sections:"
282237
echo $valid_sections | xargs printf "%-10s\n" | tr ' ' . | xargs -n 10 | sed 's/\./ /g; s/^/ /'
283-
continue
284238
else
285239
section="$new_section"
286240
fi
287241
fi
288-
continue
289-
;;
290-
exit|quit)
291-
break
292-
;;
293-
copy|yank|c|y)
242+
fi
243+
}
244+
245+
cmd_copy() {
294246
if [ -z "$DISPLAY" ]; then
295247
echo copy: supported only in the Desktop version
296-
continue
297-
fi
298-
if [ -z "$input" ]; then
248+
elif [ -z "$input" ]; then
299249
echo copy: Make at least one query first.
300-
continue
301250
else
302251
curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?T)" > "$TMP1"
303252
if [ "$is_macos" != yes ]; then
@@ -306,17 +255,14 @@ EOF
306255
cat "$TMP1" | pbcopy
307256
fi
308257
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
309-
continue
310258
fi
311-
;;
312-
ccopy|cc|C|Y)
259+
}
260+
261+
cmd_ccopy() {
313262
if [ -z "$DISPLAY" ]; then
314263
echo copy: supported only in the Desktop version
315-
continue
316-
fi
317-
if [ -z "$input" ]; then
264+
elif [ -z "$input" ]; then
318265
echo copy: Make at least one query first.
319-
continue
320266
else
321267
curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?TQ)" > "$TMP1"
322268
if [ "$is_macos" != yes ]; then
@@ -325,10 +271,38 @@ EOF
325271
cat "$TMP1" | pbcopy
326272
fi
327273
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
328-
continue
329274
fi
330-
;;
331-
id|"id "*)
275+
}
276+
277+
cmd_exit() {
278+
exit 0
279+
}
280+
281+
cmd_help() {
282+
cat <<EOF
283+
help - show this help
284+
hush - do not show the 'help' string at start anymore
285+
cd LANG - change the language context
286+
copy - copy the last answer in the clipboard (aliases: yank, y, c)
287+
ccopy - copy the last answer w/o comments (cut comments; aliases: cc, Y, C)
288+
exit - exit the cheat shell (aliases: quit, ^D)
289+
id [ID] - set/show an unique session id ("reset" to reset, "remove" to remove)
290+
stealth - stealth mode (automatic queries for selected text)
291+
update - self update (only if the scriptfile is writeable)
292+
version - show current cht.sh version
293+
/:help - service help
294+
QUERY - space ceparated query staring (examples are below)
295+
cht.sh> python zip list
296+
cht.sh/python> zip list
297+
cht.sh/go> /python zip list
298+
EOF
299+
}
300+
301+
cmd_hush() {
302+
mkdir -p $HOME/.cht.sh/ && touch $HOME/.cht.sh/.hushlogin && echo "Initial 'use help' message was disabled"
303+
}
304+
305+
cmd_id() {
332306
id_file="$HOME/.cht.sh/id"
333307

334308
if [ id = "$input" ]; then
@@ -342,19 +316,19 @@ EOF
342316
else
343317
echo "id was not set, so you can't remove it"
344318
fi
345-
continue
319+
return
346320
fi
347321
if [ -n "$new_id" ] && [ reset != "$new_id" ] && [ $(/bin/echo -n "$new_id" | wc -c) -lt 16 ]; then
348322
echo "ERROR: $new_id: Too short id. Minimal id length is 16. Use 'id reset' for a random id"
349-
continue
323+
return
350324
fi
351325
if [ -z "$new_id" ]; then
352326
# if new_id is not specified check if we have some id already
353327
# if yes, just show it
354328
# if not, generate a new id
355329
if [ -e "$id_file" ]; then
356330
echo $(awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1)
357-
continue
331+
return
358332
else
359333
new_id=reset
360334
fi
@@ -373,9 +347,14 @@ EOF
373347
printf ".cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id\n" >> "$id_file"
374348
fi
375349
echo "$new_id"
376-
continue
377-
;;
378-
stealth|"stealth "*)
350+
}
351+
352+
cmd_query() {
353+
query=$(prepare_query "$section" "$input")
354+
do_query "$query"
355+
}
356+
357+
cmd_stealth() {
379358
if [ "$input" != stealth ]; then
380359
arguments=$(echo "$input" | sed 's/stealth //; s/ /\&/')
381360
fi
@@ -402,7 +381,7 @@ EOF
402381
current_text="$(echo $current | tr -c '[a-zA-Z0-9]' ' ')"
403382
if [ $(echo $current_text | wc -w) -gt "$STEALTH_MAX_SELECTION_LENGTH" ]; then
404383
echo "\033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
405-
continue
384+
return
406385
else
407386
printf "\n\033[0;31mstealth: \033[7m $current_text\033[0m\n"
408387
query=$(prepare_query "$section" "$current_text" "$arguments")
@@ -412,10 +391,10 @@ EOF
412391
sleep 1;
413392
done
414393
trap - INT
415-
continue
416-
;;
417-
update)
418-
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s "${CHTSH_URL}"/:bash | sudo tee $0"; continue; }
394+
}
395+
396+
cmd_update() {
397+
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s "${CHTSH_URL}"/:bash | sudo tee $0"; return; }
419398
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
420399
curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"
421400
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
@@ -430,9 +409,9 @@ EOF
430409
echo "cht.sh is up to date. No update needed"
431410
fi
432411
rm -f "$TMP2" > /dev/null 2>&1
433-
continue
434-
;;
435-
version)
412+
}
413+
414+
cmd_version() {
436415
insttime=$(ls -l -- "$0" | sed 's/ */ /g' | cut -d ' ' -f 6-8)
437416
echo "cht.sh version $__CHTSH_VERSION of $__CHTSH_DATETIME; installed at: $insttime"
438417
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
@@ -444,13 +423,42 @@ EOF
444423
fi
445424
fi
446425
rm -f "$TMP2" > /dev/null 2>&1
447-
continue
448-
;;
449-
"")
450-
continue
451-
;;
452-
esac
426+
}
453427

454-
query=$(prepare_query "$section" "$input")
455-
do_query "$query"
428+
TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
429+
trap 'rm -f $TMP1 $TMP2' EXIT
430+
trap 'true' INT
431+
432+
if ! [ -e "$HOME/.cht.sh/.hushlogin" ] && [ -z "$this_query" ]; then
433+
echo "type 'help' for the cht.sh shell help"
434+
fi
435+
436+
while true; do
437+
if [ "$section" != "" ]; then
438+
full_prompt="$prompt/$section> "
439+
else
440+
full_prompt="$prompt> "
441+
fi
442+
443+
input=$(
444+
rlwrap -H "$HOME/.cht.sh/history" -pgreen -C cht.sh -S "$full_prompt" sh "$0" --read | sed 's/ *#.*//'
445+
)
446+
447+
cmd_name=${input%% *}
448+
cmd_args=${input#* }
449+
case $cmd_name in
450+
"") continue;; # skip empty input lines
451+
'?'|h|help) cmd_name=help;;
452+
hush) cmd_name=hush;;
453+
cd) cmd_name=cd;;
454+
exit|quit) cmd_name=exit;;
455+
copy|yank|c|y) cmd_name=copy;;
456+
ccopy|cc|C|Y) cmd_name=ccopy;;
457+
id) cmd_name=id;;
458+
stealth) cmd_name=stealth;;
459+
update) cmd_name=update;;
460+
version) cmd_name=version;;
461+
*) cmd_name="query $cmd_name";;
462+
esac
463+
eval cmd_$cmd_name \$cmdargs
456464
done

0 commit comments

Comments
 (0)