# jsjs / tsts / cscs
# Save the three cheatsheet .txt files into one folder, then point this
# variable at that folder. Default is ~/cheatsheets.
#
# In ~/.bashrc or ~/.zshrc:
# export CHEATSHEET_DIR="$HOME/cheatsheets"
# source /path/to/cheatsheet-aliases.sh
#
# Usage:
# jsjs
# jsjs ARRAY
# tsts GENERICS
# cscs LIST
CHEATSHEET_DIR="${CHEATSHEET_DIR:-$HOME/cheatsheets}"
_cheatsheet() {
local file="$1"
shift
if [ $# -eq 0 ]; then
less "$file"
return
fi
awk -v IGNORECASE=1 -v pat="$*" '
BEGIN {
found=0
}
/^=+$/ {
getline title
getline underline
if (tolower(title) ~ tolower(pat)) {
found=1
print "===================================================================="
print title
print underline
while (getline) {
if ($0 ~ /^=+$/) {
getline nexttitle
getline nextunderline
print ""
exit
}
print
}
exit
}
}
END {
if (!found)
print "No section found."
}
' "$file" | less
}
jsjs() { _cheatsheet "$CHEATSHEET_DIR/javascript-cheatsheet.txt" "$@"; }
tsts() { _cheatsheet "$CHEATSHEET_DIR/typescript-cheatsheet.txt" "$@"; }
cscs() { _cheatsheet "$CHEATSHEET_DIR/csharp-cheatsheet.txt" "$@"; }