Skip to content

Instantly share code, notes, and snippets.

@thingsiplay
Last active June 3, 2023 13:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thingsiplay/69aada6a57f829d9580d7b54f8a207a0 to your computer and use it in GitHub Desktop.
Save thingsiplay/69aada6a57f829d9580d7b54f8a207a0 to your computer and use it in GitHub Desktop.
cheat.sh - The only cheat sheet you need
#!/bin/env bash
cheat='curl -s cheat.sh'
menu='fzf --reverse'
pager='less -R -c'
cachefile_max_age_hours=6
# Path to temporary cache file. If your Linux system does not support /dev/shm
# or if you are on MacOS, then change the path to your liking:
cachefile='/dev/shm/cheatlist' # GNU+LINUX
# cachefile="${TMPDIR}/cheatlist" # MacOS/Darwin
# Download list file and cache it.
listing () {
if [ -f "${cachefile}" ]
then
local filedate=$(stat -c %Y -- "${cachefile}")
local now=$(date +%s)
local age_hours=$(( (now - filedate) / 60 / 60 ))
if [[ "${age_hours}" > "${cachefile_max_age_hours}" ]]
then
${cheat}/:list > "${cachefile}"
fi
else
${cheat}/:list > "${cachefile}"
fi
cat -- "${cachefile}"
}
case "${1}" in
'')
if selection=$(listing | ${menu})
then
${cheat}/"${selection}" | ${pager}
fi
;;
'-h')
${cheat}/:help | ${pager}
;;
'-l')
listing
;;
*)
${cheat}/${@} | ${pager}
;;
esac
@thingsiplay
Copy link
Author

I want to give a special thanks to the r/bash community. This little script is shaped strongly by the suggestions made at https://www.reddit.com/r/bash/comments/13xz1kc/the_only_cheat_sheet_you_need/ .

@nidan841g
Copy link

This case statement would allow the script to work on MacOS as well as Linux.

case "$(uname -o)" in
    GNU/Linux)  cachefile='/dev/shm/cheatlist'  ;;
    Darwin)     cachefile="$TMPDIR/cheatlist" ;;
    *)          echo "Unsupported OS"; exit 1 ;;
esac

@thingsiplay
Copy link
Author

Thank you for the suggestion. I actually would like to avoid additional checks that are always true for the current user. The variables are intended to be changed for the environment and user needs, like an option. So I will add a comment and a line ready to uncomment instead. I prefer this approach. I hope this is a good enough compromise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment