From d04842084a55dd68362cf3e646b4a573a99a520b Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Fri, 24 Dec 2021 04:30:58 +0100 Subject: [PATCH] Rewrite cheat.sh script --- home/.local/bin/scripts/cheat.sh | 43 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/home/.local/bin/scripts/cheat.sh b/home/.local/bin/scripts/cheat.sh index 104ab67..e41de68 100755 --- a/home/.local/bin/scripts/cheat.sh +++ b/home/.local/bin/scripts/cheat.sh @@ -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"