dotfiles/home/.local/bin/scripts/cheat.sh

43 lines
1.1 KiB
Bash
Raw Normal View History

2021-12-09 18:05:19 +00:00
#!/bin/sh
2021-12-24 03:30:58 +00:00
# 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
selected=$(printf "$SELECTABLE" | fzf)
selected=$(printf "$selected" | tail -1)
fi
2021-12-09 18:05:19 +00:00
2021-12-24 03:30:58 +00:00
# If the cheatsheet doesn't already include a query, select a query
2021-12-24 03:46:48 +00:00
if echo "$selected" | grep -qe "[/:]"; then
2021-12-24 03:30:58 +00:00
query=""
elif [ -n "$2" ]; then
query="$2"
else
options=$(curl -s "cheat.sh/$selected/:list")
2021-12-24 03:46:48 +00:00
query="$(printf "\n$options" | \
fzf --bind=alt-enter:print-query \
--prompt="cheat.sh query>" \
--header="use alt-enter to enter non-listed query"\
)"
query=$(echo "$query" | tail -1)
2021-12-24 03:30:58 +00:00
fi
query="$(echo "$query" | tr ' ' '+')"
2021-12-09 18:05:19 +00:00
2021-12-24 03:30:58 +00:00
# cheat.sh doesn't play nicely with trailing / in URLs
if [ -n "$query" ]; then
2021-12-09 18:05:19 +00:00
url="cheat.sh/$selected/$query"
else
2021-12-24 03:30:58 +00:00
url="cheat.sh/$selected"
2021-12-09 18:05:19 +00:00
fi
2021-12-24 03:30:58 +00:00
# Print the URL and make the request
echo "$url"
curl -s "$url"