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

Whitespace-only tweaks.

This commit is contained in:
Vadim Zhukov
2018-08-27 17:01:29 +03:00
parent e796a08e68
commit 9e1e12b222
+147 -147
View File
@@ -222,64 +222,64 @@ else
fi
cmd_cd() {
if [ $# -eq 0 ]; then
if [ $# -eq 0 ]; then
section=""
else
new_section=$(echo "$input" | sed 's/cd *//; s@/*$@@; s@^/*@@')
if [ -z "$new_section" ] || [ ".." = "$new_section" ]; then
section=""
else
new_section=$(echo "$input" | sed 's/cd *//; s@/*$@@; s@^/*@@')
if [ -z "$new_section" ] || [ ".." = "$new_section" ]; then
section=""
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
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
section="$new_section"
fi
fi
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
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
}
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
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
}
cmd_exit() {
exit 0
exit 0
}
cmd_help() {
cat <<EOF
cat <<EOF
help - show this help
hush - do not show the 'help' string at start anymore
cd LANG - change the language context
@@ -299,54 +299,54 @@ EOF
}
cmd_hush() {
mkdir -p $HOME/.cht.sh/ && touch $HOME/.cht.sh/.hushlogin && echo "Initial 'use help' message was disabled"
mkdir -p $HOME/.cht.sh/ && touch $HOME/.cht.sh/.hushlogin && echo "Initial 'use help' message was disabled"
}
cmd_id() {
id_file="$HOME/.cht.sh/id"
id_file="$HOME/.cht.sh/id"
if [ id = "$input" ]; then
new_id=""
else
new_id=$(echo "$input" | sed 's/id *//; s/ *$//; s/ /+/g')
fi
if [ "$new_id" = remove ]; then
if [ -e "$id_file" ]; then
rm -f -- "$id_file" && echo "id is removed"
else
echo "id was not set, so you can't remove it"
fi
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"
return
fi
if [ -z "$new_id" ]; then
# if new_id is not specified check if we have some id already
# if yes, just show it
# if not, generate a new id
if [ -e "$id_file" ]; then
echo $(awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1)
return
else
new_id=reset
fi
fi
if [ "$new_id" = reset ]; then
new_id=$(gen_random_str 12)
else
echo WARNING: if someone gueses your id, he can read your cht.sh search history
fi
if [ -e "$id_file" ] && grep -q '\tid\t[^\t][^\t]*$' "$id_file" 2> /dev/null; then
sed -i 's/\tid\t[^\t][^\t]*$/ id '"$new_id"'/' "$id_file"
else
if ! [ -e "$id_file" ]; then
printf '#\n\n' > "$id_file"
fi
printf ".cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id\n" >> "$id_file"
fi
echo "$new_id"
if [ id = "$input" ]; then
new_id=""
else
new_id=$(echo "$input" | sed 's/id *//; s/ *$//; s/ /+/g')
fi
if [ "$new_id" = remove ]; then
if [ -e "$id_file" ]; then
rm -f -- "$id_file" && echo "id is removed"
else
echo "id was not set, so you can't remove it"
fi
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"
return
fi
if [ -z "$new_id" ]; then
# if new_id is not specified check if we have some id already
# if yes, just show it
# if not, generate a new id
if [ -e "$id_file" ]; then
echo $(awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1)
return
else
new_id=reset
fi
fi
if [ "$new_id" = reset ]; then
new_id=$(gen_random_str 12)
else
echo WARNING: if someone gueses your id, he can read your cht.sh search history
fi
if [ -e "$id_file" ] && grep -q '\tid\t[^\t][^\t]*$' "$id_file" 2> /dev/null; then
sed -i 's/\tid\t[^\t][^\t]*$/ id '"$new_id"'/' "$id_file"
else
if ! [ -e "$id_file" ]; then
printf '#\n\n' > "$id_file"
fi
printf ".cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id\n" >> "$id_file"
fi
echo "$new_id"
}
cmd_query() {
@@ -355,74 +355,74 @@ cmd_query() {
}
cmd_stealth() {
if [ "$input" != stealth ]; then
arguments=$(echo "$input" | sed 's/stealth //; s/ /\&/')
fi
trap break INT
if [ "$is_macos" = yes ]; then
past=$(pbpaste)
else
past=$(xsel -o)
fi
printf "\033[0;31mstealth:\033[0m you are in the stealth mode; select any text in any window for a query\n"
printf "\033[0;31mstealth:\033[0m selections longer than $STEALTH_MAX_SELECTION_LENGTH words are ignored\n"
if [ -n "$arguments" ]; then
printf "\033[0;31mstealth:\033[0m query arguments: ?$arguments\n"
fi
printf "\033[0;31mstealth:\033[0m use ^C to leave this mode\n"
while true; do
if [ "$is_macos" = yes ]; then
current=$(pbpaste)
else
current=$(xsel -o)
fi
if [ "$past" != "$current" ]; then
past=$current
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"
return
else
printf "\n\033[0;31mstealth: \033[7m $current_text\033[0m\n"
query=$(prepare_query "$section" "$current_text" "$arguments")
do_query "$query"
fi
fi
sleep 1;
done
trap - INT
if [ "$input" != stealth ]; then
arguments=$(echo "$input" | sed 's/stealth //; s/ /\&/')
fi
trap break INT
if [ "$is_macos" = yes ]; then
past=$(pbpaste)
else
past=$(xsel -o)
fi
printf "\033[0;31mstealth:\033[0m you are in the stealth mode; select any text in any window for a query\n"
printf "\033[0;31mstealth:\033[0m selections longer than $STEALTH_MAX_SELECTION_LENGTH words are ignored\n"
if [ -n "$arguments" ]; then
printf "\033[0;31mstealth:\033[0m query arguments: ?$arguments\n"
fi
printf "\033[0;31mstealth:\033[0m use ^C to leave this mode\n"
while true; do
if [ "$is_macos" = yes ]; then
current=$(pbpaste)
else
current=$(xsel -o)
fi
if [ "$past" != "$current" ]; then
past=$current
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"
return
else
printf "\n\033[0;31mstealth: \033[7m $current_text\033[0m\n"
query=$(prepare_query "$section" "$current_text" "$arguments")
do_query "$query"
fi
fi
sleep 1;
done
trap - INT
}
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
if grep -q ^__CHTSH_VERSION= "$TMP2"; then
# section was vaildated by us already
args="--shell $section"
cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" $args
else
echo "Something went wrong. Please update manually"
fi
else
echo "cht.sh is up to date. No update needed"
fi
rm -f "$TMP2" > /dev/null 2>&1
[ -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
if grep -q ^__CHTSH_VERSION= "$TMP2"; then
# section was vaildated by us already
args="--shell $section"
cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" $args
else
echo "Something went wrong. Please update manually"
fi
else
echo "cht.sh is up to date. No update needed"
fi
rm -f "$TMP2" > /dev/null 2>&1
}
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)
if curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"; then
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
echo "Update needed (type 'update' for that)".
else
echo "Up to date. No update needed"
fi
fi
rm -f "$TMP2" > /dev/null 2>&1
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)
if curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"; then
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
echo "Update needed (type 'update' for that)".
else
echo "Up to date. No update needed"
fi
fi
rm -f "$TMP2" > /dev/null 2>&1
}
TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)