Rewrite cheat.sh script

This commit is contained in:
ItsDrike 2021-12-24 04:30:58 +01:00
parent 18ba4c4906
commit d04842084a
No known key found for this signature in database
GPG key ID: FB8CA11A2CF3A843

View file

@ -1,16 +1,37 @@
#!/bin/sh
languages=`echo "python rust golang lua cpp c typescript nodejs javascript js" | tr ' ' '\n'`
core_utils=`echo "xargs find sed awk" | tr ' ' '\n'`
selected=`printf "$languages\n$core_utils" | fzf`
read -p "query: " query
query=`echo "$query" | tr ' ' '+'`
if printf "$languages" | grep -qs "$selected"; then
url="cheat.sh/$selected/$query"
# Select the cheatsheat
SELECTABLE="$(curl -s "cheat.sh/:list")"
if [ -n "$1" ]; then
if echo "$SELECTABLE" | grep -qe "\b$1\b"; then
selected="$1"
else
echo "Invalid selection. For all selections, query cheat.sh/:list"
exit 1
fi
else
url="cheat.sh/$selected~$query"
selected=$(printf "$SELECTABLE" | fzf)
selected=$(printf "$selected" | tail -1)
fi
curl "$url"
# If the cheatsheet doesn't already include a query, select a query
if echo "$selected" | grep -q "/"; then
query=""
elif [ -n "$2" ]; then
query="$2"
else
options=$(curl -s "cheat.sh/$selected/:list")
query=$(printf "\n$options" | fzf --bind=enter:print-query --prompt="cheat.sh query (can be empty): ")
fi
query="$(echo "$query" | tr ' ' '+')"
# cheat.sh doesn't play nicely with trailing / in URLs
if [ -n "$query" ]; then
url="cheat.sh/$selected/$query"
else
url="cheat.sh/$selected"
fi
# Print the URL and make the request
echo "$url"
curl -s "$url"