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 #!/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` # Select the cheatsheat
read -p "query: " query SELECTABLE="$(curl -s "cheat.sh/:list")"
query=`echo "$query" | tr ' ' '+'` if [ -n "$1" ]; then
if echo "$SELECTABLE" | grep -qe "\b$1\b"; then
selected="$1"
if printf "$languages" | grep -qs "$selected"; then else
url="cheat.sh/$selected/$query" echo "Invalid selection. For all selections, query cheat.sh/:list"
exit 1
fi
else else
url="cheat.sh/$selected~$query" selected=$(printf "$SELECTABLE" | fzf)
selected=$(printf "$selected" | tail -1)
fi 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"