cheat.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/sh
  2. # Check dependencies
  3. [ -z "$(which fd 2>/dev/null)" ] && echo "[-] You need fd installed" && exit 1
  4. [ -z "$(which rg 2>/dev/null)" ] && echo "[-] You need ripgrep (rg) installed" && exit 1
  5. [ -z "$(which fzf 2>/dev/null)" ] && echo "[-] You need fzf installed" && exit 1
  6. function usage {
  7. echo "cheat.sh - cheatsheet reader"
  8. echo "Options:"
  9. echo " -h print help/usage"
  10. echo " -l list all files"
  11. echo " -p print cheatsheets path"
  12. echo " -w writable"
  13. echo " -g open in gvim"
  14. echo " -s search for string in <arg> using ripgrep"
  15. }
  16. # Set the path to the cheatncheck repo here
  17. if [ "$CHEAT" = "" ]; then
  18. cheatsheets="$HOME/documents/cheatsheets/"
  19. else
  20. cheatsheets="$CHEAT"
  21. fi
  22. # check if the cheatsheets dir. exists.
  23. [ ! -d "$cheatsheets" ] && echo "[-] Cheatsheet folder does not exist." && exit 1
  24. # if yes, switch to that dir.
  25. cd $cheatsheets
  26. if [ $# -eq 0 ] ; then
  27. vim -R -c "Goyo 90%" "$(fzf)"
  28. exit 1
  29. fi
  30. # Option Parsin
  31. while getopts ":ghlws:p" options ; do
  32. case "${options}" in
  33. h) usage ;; # print help/usage
  34. l) fd -t f \.md -x echo {/.} ;; # list all files
  35. p) echo $cheatsheets ;; # print cheatsheets path
  36. w) vim -c "Goyo 90%" "$(fzf)" ;; # writable
  37. g) vim -R -c "Goyo 90%" "$(fzf)" -g ;; # open in gvim
  38. s) rg "${OPTARG}" ;; # search for string in <arg> using ripgrep
  39. *) echo 'Invalid Argument' ;; # Argument not available
  40. esac
  41. done