mirror of
https://github.com/chubin/cheat.sh.git
synced 2025-01-07 05:38:03 +03:00
Merge branch 'offline_usage' of https://github.com/chubin/cheat.sh into offline_usage
This commit is contained in:
commit
011a1c673d
@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
#
|
#
|
||||||
# [X] open section
|
# [X] open section
|
||||||
# [X] one shot mode
|
# [X] one shot mode
|
||||||
@ -23,19 +23,20 @@
|
|||||||
# count words in text counter
|
# count words in text counter
|
||||||
# group elements list
|
# group elements list
|
||||||
|
|
||||||
__CHTSH_VERSION=4
|
__CHTSH_VERSION=5
|
||||||
__CHTSH_DATETIME="2018-07-08 22:26:46 +0200"
|
__CHTSH_DATETIME="2019-05-11 07:29:46 +0200"
|
||||||
|
|
||||||
export LESSSECURE=1
|
export LESSSECURE=1
|
||||||
STEALTH_MAX_SELECTION_LENGTH=5
|
STEALTH_MAX_SELECTION_LENGTH=5
|
||||||
|
|
||||||
case `uname -s` in
|
case "$(uname -s)" in
|
||||||
Darwin) is_macos=yes ;;
|
Darwin) is_macos=yes ;;
|
||||||
*) is_macos=no ;;
|
*) is_macos=no ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# for KSH93
|
# for KSH93
|
||||||
if echo $KSH_VERSION | grep -q ' 93' && ! local foo 2>/dev/null; then
|
# shellcheck disable=SC2034,SC2039,SC2168
|
||||||
|
if echo "$KSH_VERSION" | grep -q ' 93' && ! local foo 2>/dev/null; then
|
||||||
alias local=typeset
|
alias local=typeset
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -61,7 +62,7 @@ do_query()
|
|||||||
b_opts="-b \"\$HOME/.cht.sh/id\""
|
b_opts="-b \"\$HOME/.cht.sh/id\""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
eval curl $b_opts -s $uri > "$TMP1"
|
eval curl "$b_opts" -s "$uri" > "$TMP1"
|
||||||
|
|
||||||
if [ -z "$lines" ] || [ "$(wc -l "$TMP1" | awk '{print $1}')" -lt "$lines" ]; then
|
if [ -z "$lines" ] || [ "$(wc -l "$TMP1" | awk '{print $1}')" -lt "$lines" ]; then
|
||||||
cat "$TMP1"
|
cat "$TMP1"
|
||||||
@ -96,23 +97,24 @@ gen_random_str()
|
|||||||
(
|
(
|
||||||
len=$1
|
len=$1
|
||||||
if command -v openssl >/dev/null; then
|
if command -v openssl >/dev/null; then
|
||||||
openssl rand -base64 $(($len*3/4)) | awk -v ORS='' //
|
openssl rand -base64 $((len*3/4)) | awk -v ORS='' //
|
||||||
else
|
else
|
||||||
rdev=/dev/urandom
|
rdev=/dev/urandom
|
||||||
for d in /dev/{srandom,random,arandom}; do
|
for d in /dev/{srandom,random,arandom}; do
|
||||||
test -r $d && rdev=$d
|
test -r "$d" && rdev=$d
|
||||||
done
|
done
|
||||||
if command -v hexdump >/dev/null; then
|
if command -v hexdump >/dev/null; then
|
||||||
hexdump -vn $(($len/2)) -e '1/1 "%02X" 1 ""' $rdev
|
hexdump -vn $((len/2)) -e '1/1 "%02X" 1 ""' "$rdev"
|
||||||
elif command -v xxd >/dev/null; then
|
elif command -v xxd >/dev/null; then
|
||||||
xxd -l $(($len/2)) -ps $dev | awk -v ORS='' //
|
xxd -l $((len/2)) -ps "$rdev" | awk -v ORS='' //
|
||||||
else
|
else
|
||||||
cd /tmp
|
cd /tmp || { echo Cannot cd into /tmp >&2; exit 1; }
|
||||||
s=
|
s=
|
||||||
while [ $(echo "$s" | wc -c) -lt $len ]; do
|
# shellcheck disable=SC2000
|
||||||
|
while [ "$(echo "$s" | wc -c)" -lt "$len" ]; do
|
||||||
s="$s$(mktemp -u XXXXXXXXXX)"
|
s="$s$(mktemp -u XXXXXXXXXX)"
|
||||||
done
|
done
|
||||||
printf %.${len}s "$s"
|
printf "%.${len}s" "$s"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
)
|
)
|
||||||
@ -129,7 +131,7 @@ fi
|
|||||||
[ -z "$CHTSH_URL" ] && CHTSH_URL=https://cht.sh
|
[ -z "$CHTSH_URL" ] && CHTSH_URL=https://cht.sh
|
||||||
|
|
||||||
# any better test not involving either OS matching or actual query?
|
# any better test not involving either OS matching or actual query?
|
||||||
if [ `uname -s` = OpenBSD ] && [ -x /usr/bin/ftp ]; then
|
if [ "$(uname -s)" = OpenBSD ] && [ -x /usr/bin/ftp ]; then
|
||||||
curl() {
|
curl() {
|
||||||
local opt args="-o -"
|
local opt args="-o -"
|
||||||
while getopts "b:s" opt; do
|
while getopts "b:s" opt; do
|
||||||
@ -139,22 +141,23 @@ if [ `uname -s` = OpenBSD ] && [ -x /usr/bin/ftp ]; then
|
|||||||
*) echo "internal error: unsupported cURL option '$opt'" >&2; exit 1;;
|
*) echo "internal error: unsupported cURL option '$opt'" >&2; exit 1;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $(($OPTIND - 1))
|
shift $((OPTIND - 1))
|
||||||
/usr/bin/ftp $args "$@"
|
/usr/bin/ftp "$args" "$@"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
command -v curl >/dev/null || { echo 'DEPENDENCY: install "curl" to use cht.sh' >&2; exit 1; }
|
command -v curl >/dev/null || { echo 'DEPENDENCY: install "curl" to use cht.sh' >&2; exit 1; }
|
||||||
_CURL=$(command -v curl)
|
_CURL=$(command -v curl)
|
||||||
if [ x"$CHTSH_CURL_OPTIONS" != x ]; then
|
if [ x"$CHTSH_CURL_OPTIONS" != x ]; then
|
||||||
curl() {
|
curl() {
|
||||||
$_CURL ${CHTSH_CURL_OPTIONS} "$@"
|
$_CURL "${CHTSH_CURL_OPTIONS}" "$@"
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
if [ "$1" = --read ]; then
|
if [ "$1" = --read ]; then
|
||||||
read -r a || a=exit
|
read -r a || a="exit"
|
||||||
|
# shellcheck disable=SC1117
|
||||||
printf "%s\n" "$a"
|
printf "%s\n" "$a"
|
||||||
exit 0
|
exit 0
|
||||||
elif [ x"$1" = x--help ] || [ -z "$1" ]; then
|
elif [ x"$1" = x--help ] || [ -z "$1" ]; then
|
||||||
@ -193,10 +196,13 @@ else
|
|||||||
|
|
||||||
if [ "$valid" = yes ]; then
|
if [ "$valid" = yes ]; then
|
||||||
section="$new_section"
|
section="$new_section"
|
||||||
|
# shellcheck disable=SC2001
|
||||||
this_query="$(echo "$input" | sed 's@ *[^ ]* *@@')"
|
this_query="$(echo "$input" | sed 's@ *[^ ]* *@@')"
|
||||||
|
# shellcheck disable=SC1117
|
||||||
this_prompt="\033[0;32mcht.sh/$section>\033[0m "
|
this_prompt="\033[0;32mcht.sh/$section>\033[0m "
|
||||||
else
|
else
|
||||||
this_query="$input"
|
this_query="$input"
|
||||||
|
# shellcheck disable=SC1117
|
||||||
this_prompt="\033[0;32mcht.sh>\033[0m "
|
this_prompt="\033[0;32mcht.sh>\033[0m "
|
||||||
fi
|
fi
|
||||||
if [ -n "$this_query" ] && [ -z "$CHEATSH_RESTART" ]; then
|
if [ -n "$this_query" ] && [ -z "$CHEATSH_RESTART" ]; then
|
||||||
@ -216,9 +222,9 @@ lines=$(tput lines)
|
|||||||
if command -v less >/dev/null; then
|
if command -v less >/dev/null; then
|
||||||
defpager="less -R"
|
defpager="less -R"
|
||||||
elif command -v more >/dev/null; then
|
elif command -v more >/dev/null; then
|
||||||
defpager=more
|
defpager="more"
|
||||||
else
|
else
|
||||||
defpager=cat
|
defpager="cat"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cmd_cd() {
|
cmd_cd() {
|
||||||
@ -234,7 +240,12 @@ cmd_cd() {
|
|||||||
if [ "$valid" = no ]; then
|
if [ "$valid" = no ]; then
|
||||||
echo "Invalid section: $new_section"
|
echo "Invalid section: $new_section"
|
||||||
echo "Valid sections:"
|
echo "Valid sections:"
|
||||||
echo $valid_sections | xargs printf "%-10s\n" | tr ' ' . | xargs -n 10 | sed 's/\./ /g; s/^/ /'
|
# shellcheck disable=SC1117
|
||||||
|
echo "$valid_sections" \
|
||||||
|
| xargs printf "%-10s\n" \
|
||||||
|
| tr ' ' . \
|
||||||
|
| xargs -n 10 \
|
||||||
|
| sed 's/\./ /g; s/^/ /'
|
||||||
else
|
else
|
||||||
section="$new_section"
|
section="$new_section"
|
||||||
fi
|
fi
|
||||||
@ -252,7 +263,7 @@ cmd_copy() {
|
|||||||
if [ "$is_macos" != yes ]; then
|
if [ "$is_macos" != yes ]; then
|
||||||
xsel -bi < "$TMP1"
|
xsel -bi < "$TMP1"
|
||||||
else
|
else
|
||||||
cat "$TMP1" | pbcopy
|
pbcopy < "$TMP1"
|
||||||
fi
|
fi
|
||||||
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
|
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
|
||||||
fi
|
fi
|
||||||
@ -268,7 +279,7 @@ cmd_ccopy() {
|
|||||||
if [ "$is_macos" != yes ]; then
|
if [ "$is_macos" != yes ]; then
|
||||||
xsel -bi < "$TMP1"
|
xsel -bi < "$TMP1"
|
||||||
else
|
else
|
||||||
cat "$TMP1" | pbcopy
|
pbcopy < "$TMP1"
|
||||||
fi
|
fi
|
||||||
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
|
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
|
||||||
fi
|
fi
|
||||||
@ -299,7 +310,7 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd_hush() {
|
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() {
|
cmd_id() {
|
||||||
@ -318,7 +329,7 @@ cmd_id() {
|
|||||||
fi
|
fi
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
if [ -n "$new_id" ] && [ reset != "$new_id" ] && [ $(/bin/echo -n "$new_id" | wc -c) -lt 16 ]; then
|
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"
|
echo "ERROR: $new_id: Too short id. Minimal id length is 16. Use 'id reset' for a random id"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@ -327,7 +338,7 @@ cmd_id() {
|
|||||||
# if yes, just show it
|
# if yes, just show it
|
||||||
# if not, generate a new id
|
# if not, generate a new id
|
||||||
if [ -e "$id_file" ]; then
|
if [ -e "$id_file" ]; then
|
||||||
echo $(awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1)
|
awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
new_id=reset
|
new_id=reset
|
||||||
@ -379,7 +390,7 @@ cmd_stealth() {
|
|||||||
if [ "$past" != "$current" ]; then
|
if [ "$past" != "$current" ]; then
|
||||||
past=$current
|
past=$current
|
||||||
current_text="$(echo $current | tr -c '[a-zA-Z0-9]' ' ')"
|
current_text="$(echo $current | tr -c '[a-zA-Z0-9]' ' ')"
|
||||||
if [ $(echo $current_text | wc -w) -gt "$STEALTH_MAX_SELECTION_LENGTH" ]; then
|
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"
|
echo "\033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
|
||||||
continue
|
continue
|
||||||
else
|
else
|
||||||
@ -394,14 +405,14 @@ cmd_stealth() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd_update() {
|
cmd_update() {
|
||||||
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s "${CHTSH_URL}"/:bash | sudo tee $0"; return; }
|
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s ${CHTSH_URL}/:cht.sh | sudo tee $0"; return; }
|
||||||
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
|
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
|
||||||
curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"
|
curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"
|
||||||
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
|
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
|
||||||
if grep -q ^__CHTSH_VERSION= "$TMP2"; then
|
if grep -q ^__CHTSH_VERSION= "$TMP2"; then
|
||||||
# section was vaildated by us already
|
# section was vaildated by us already
|
||||||
args="--shell $section"
|
args=(--shell "$section")
|
||||||
cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" $args
|
cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" "${args[@]}"
|
||||||
else
|
else
|
||||||
echo "Something went wrong. Please update manually"
|
echo "Something went wrong. Please update manually"
|
||||||
fi
|
fi
|
||||||
@ -450,8 +461,8 @@ while true; do
|
|||||||
"") continue;; # skip empty input lines
|
"") continue;; # skip empty input lines
|
||||||
'?'|h|help) cmd_name=help;;
|
'?'|h|help) cmd_name=help;;
|
||||||
hush) cmd_name=hush;;
|
hush) cmd_name=hush;;
|
||||||
cd) cmd_name=cd;;
|
cd) cmd_name="cd";;
|
||||||
exit|quit) cmd_name=exit;;
|
exit|quit) cmd_name="exit";;
|
||||||
copy|yank|c|y) cmd_name=copy;;
|
copy|yank|c|y) cmd_name=copy;;
|
||||||
ccopy|cc|C|Y) cmd_name=ccopy;;
|
ccopy|cc|C|Y) cmd_name=ccopy;;
|
||||||
id) cmd_name=id;;
|
id) cmd_name=id;;
|
||||||
@ -460,5 +471,5 @@ while true; do
|
|||||||
version) cmd_name=version;;
|
version) cmd_name=version;;
|
||||||
*) cmd_name="query $cmd_name";;
|
*) cmd_name="query $cmd_name";;
|
||||||
esac
|
esac
|
||||||
cmd_$cmd_name $cmd_args
|
"cmd_$cmd_name" $cmd_args
|
||||||
done
|
done
|
||||||
|
@ -48,7 +48,7 @@ while read -r number test_line; do
|
|||||||
|
|
||||||
if [ "$test_standalone" = YES ]; then
|
if [ "$test_standalone" = YES ]; then
|
||||||
test_line="${test_line//cht.sh /}"
|
test_line="${test_line//cht.sh /}"
|
||||||
python ../lib/standalone.py "$test_line" > "$TMP" 2> /dev/null
|
"${PYTHON}" ../lib/standalone.py "$test_line" > "$TMP" 2> /dev/null
|
||||||
elif [[ $test_line = "cht.sh "* ]]; then
|
elif [[ $test_line = "cht.sh "* ]]; then
|
||||||
test_line="${test_line//cht.sh /}"
|
test_line="${test_line//cht.sh /}"
|
||||||
eval "bash $CHTSH_SCRIPT $test_line" > "$TMP"
|
eval "bash $CHTSH_SCRIPT $test_line" > "$TMP"
|
||||||
@ -69,4 +69,8 @@ done < "$TMP3"
|
|||||||
|
|
||||||
echo TESTS/OK/FAILED "$i/$((i-failed))/$failed"
|
echo TESTS/OK/FAILED "$i/$((i-failed))/$failed"
|
||||||
|
|
||||||
|
if [ "$failed" != 0 ]; then
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user