1
0
mirror of https://github.com/chubin/cheat.sh.git synced 2026-06-20 13:16:44 +02:00

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.
This commit is contained in:
Vadim Zhukov
2018-08-27 16:56:46 +03:00
parent 63a8b8bff8
commit e796a08e68
+116 -108
View File
@@ -221,27 +221,64 @@ else
defpager=cat
fi
TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
trap 'rm -f $TMP1 $TMP2' EXIT
trap 'true' INT
cmd_cd() {
if [ $# -eq 0 ]; then
section=""
else
new_section=$(echo "$input" | sed 's/cd *//; s@/*$@@; s@^/*@@')
if [ -z "$new_section" ] || [ ".." = "$new_section" ]; then
section=""
else
valid_sections=$(get_list_of_sections)
valid=no; for q in $valid_sections; do [ "$q" = "$new_section/" ] && { valid=yes; break; }; done
if [ "$valid" = no ]; then
echo "Invalid section: $new_section"
echo "Valid sections:"
echo $valid_sections | xargs printf "%-10s\n" | tr ' ' . | xargs -n 10 | sed 's/\./ /g; s/^/ /'
else
section="$new_section"
fi
fi
fi
}
if ! [ -e "$HOME/.cht.sh/.hushlogin" ] && [ -z "$this_query" ]; then
echo "type 'help' for the cht.sh shell help"
fi
cmd_copy() {
if [ -z "$DISPLAY" ]; then
echo copy: supported only in the Desktop version
elif [ -z "$input" ]; then
echo copy: Make at least one query first.
else
curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?T)" > "$TMP1"
if [ "$is_macos" != yes ]; then
xsel -bi < "$TMP1"
else
cat "$TMP1" | pbcopy
fi
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
fi
}
while true; do
if [ "$section" != "" ]; then
full_prompt="$prompt/$section> "
else
full_prompt="$prompt> "
fi
cmd_ccopy() {
if [ -z "$DISPLAY" ]; then
echo copy: supported only in the Desktop version
elif [ -z "$input" ]; then
echo copy: Make at least one query first.
else
curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?TQ)" > "$TMP1"
if [ "$is_macos" != yes ]; then
xsel -bi < "$TMP1"
else
cat "$TMP1" | pbcopy
fi
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
fi
}
input=$(
rlwrap -H "$HOME/.cht.sh/history" -pgreen -C cht.sh -S "$full_prompt" sh "$0" --read | sed 's/ *#.*//'
)
cmd_exit() {
exit 0
}
case "$input" in
'?'|h|help)
cmd_help() {
cat <<EOF
help - show this help
hush - do not show the 'help' string at start anymore
@@ -259,76 +296,13 @@ QUERY - space ceparated query staring (examples are below)
cht.sh/python> zip list
cht.sh/go> /python zip list
EOF
continue
;;
hush)
}
cmd_hush() {
mkdir -p $HOME/.cht.sh/ && touch $HOME/.cht.sh/.hushlogin && echo "Initial 'use help' message was disabled"
continue
;;
cd)
section=""
continue
;;
"cd "*)
new_section=$(echo "$input" | sed 's/cd *//; s@/*$@@; s@^/*@@')
if [ -z "$new_section" ] || [ ".." = "$new_section" ]; then
section=""
else
valid_sections=$(get_list_of_sections)
valid=no; for q in $valid_sections; do [ "$q" = "$new_section/" ] && { valid=yes; break; }; done
if [ "$valid" = no ]; then
echo "Invalid section: $new_section"
echo "Valid sections:"
echo $valid_sections | xargs printf "%-10s\n" | tr ' ' . | xargs -n 10 | sed 's/\./ /g; s/^/ /'
continue
else
section="$new_section"
fi
fi
continue
;;
exit|quit)
break
;;
copy|yank|c|y)
if [ -z "$DISPLAY" ]; then
echo copy: supported only in the Desktop version
continue
fi
if [ -z "$input" ]; then
echo copy: Make at least one query first.
continue
else
curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?T)" > "$TMP1"
if [ "$is_macos" != yes ]; then
xsel -bi < "$TMP1"
else
cat "$TMP1" | pbcopy
fi
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
continue
fi
;;
ccopy|cc|C|Y)
if [ -z "$DISPLAY" ]; then
echo copy: supported only in the Desktop version
continue
fi
if [ -z "$input" ]; then
echo copy: Make at least one query first.
continue
else
curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?TQ)" > "$TMP1"
if [ "$is_macos" != yes ]; then
xsel -bi < "$TMP1"
else
cat "$TMP1" | pbcopy
fi
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
continue
fi
;;
id|"id "*)
}
cmd_id() {
id_file="$HOME/.cht.sh/id"
if [ id = "$input" ]; then
@@ -342,11 +316,11 @@ EOF
else
echo "id was not set, so you can't remove it"
fi
continue
return
fi
if [ -n "$new_id" ] && [ reset != "$new_id" ] && [ $(/bin/echo -n "$new_id" | wc -c) -lt 16 ]; then
echo "ERROR: $new_id: Too short id. Minimal id length is 16. Use 'id reset' for a random id"
continue
return
fi
if [ -z "$new_id" ]; then
# if new_id is not specified check if we have some id already
@@ -354,7 +328,7 @@ EOF
# if not, generate a new id
if [ -e "$id_file" ]; then
echo $(awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1)
continue
return
else
new_id=reset
fi
@@ -373,9 +347,14 @@ EOF
printf ".cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id\n" >> "$id_file"
fi
echo "$new_id"
continue
;;
stealth|"stealth "*)
}
cmd_query() {
query=$(prepare_query "$section" "$input")
do_query "$query"
}
cmd_stealth() {
if [ "$input" != stealth ]; then
arguments=$(echo "$input" | sed 's/stealth //; s/ /\&/')
fi
@@ -402,7 +381,7 @@ EOF
current_text="$(echo $current | tr -c '[a-zA-Z0-9]' ' ')"
if [ $(echo $current_text | wc -w) -gt "$STEALTH_MAX_SELECTION_LENGTH" ]; then
echo "\033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
continue
return
else
printf "\n\033[0;31mstealth: \033[7m $current_text\033[0m\n"
query=$(prepare_query "$section" "$current_text" "$arguments")
@@ -412,10 +391,10 @@ EOF
sleep 1;
done
trap - INT
continue
;;
update)
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s "${CHTSH_URL}"/:bash | sudo tee $0"; continue; }
}
cmd_update() {
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s "${CHTSH_URL}"/:bash | sudo tee $0"; return; }
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
@@ -430,9 +409,9 @@ EOF
echo "cht.sh is up to date. No update needed"
fi
rm -f "$TMP2" > /dev/null 2>&1
continue
;;
version)
}
cmd_version() {
insttime=$(ls -l -- "$0" | sed 's/ */ /g' | cut -d ' ' -f 6-8)
echo "cht.sh version $__CHTSH_VERSION of $__CHTSH_DATETIME; installed at: $insttime"
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
@@ -444,13 +423,42 @@ EOF
fi
fi
rm -f "$TMP2" > /dev/null 2>&1
continue
;;
"")
continue
;;
esac
}
query=$(prepare_query "$section" "$input")
do_query "$query"
TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
trap 'rm -f $TMP1 $TMP2' EXIT
trap 'true' INT
if ! [ -e "$HOME/.cht.sh/.hushlogin" ] && [ -z "$this_query" ]; then
echo "type 'help' for the cht.sh shell help"
fi
while true; do
if [ "$section" != "" ]; then
full_prompt="$prompt/$section> "
else
full_prompt="$prompt> "
fi
input=$(
rlwrap -H "$HOME/.cht.sh/history" -pgreen -C cht.sh -S "$full_prompt" sh "$0" --read | sed 's/ *#.*//'
)
cmd_name=${input%% *}
cmd_args=${input#* }
case $cmd_name in
"") continue;; # skip empty input lines
'?'|h|help) cmd_name=help;;
hush) cmd_name=hush;;
cd) cmd_name=cd;;
exit|quit) cmd_name=exit;;
copy|yank|c|y) cmd_name=copy;;
ccopy|cc|C|Y) cmd_name=ccopy;;
id) cmd_name=id;;
stealth) cmd_name=stealth;;
update) cmd_name=update;;
version) cmd_name=version;;
*) cmd_name="query $cmd_name";;
esac
eval cmd_$cmd_name \$cmdargs
done