cheat.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. echo " -i interactive menu with dmenu"
  16. }
  17. # select the files using a dmenu/rofi interface and display em with md-viewer
  18. function interactive {
  19. cs=`fd .md$ $cheatsheets | dmenu -i -l 20`
  20. echo $cs > /tmp/lul
  21. #nohup md-viewer $cs >&2 2>/dev/null &
  22. $HOME/.scripts/tools/md-viewer "$cs"
  23. }
  24. # Set the path to the cheatncheck repo here
  25. if [ "$CHEAT" = "" ]; then
  26. cheatsheets="$HOME/documents/cheatsheets/"
  27. else
  28. cheatsheets="$CHEAT"
  29. fi
  30. # check if the cheatsheets dir. exists.
  31. [ ! -d "$cheatsheets" ] && echo "[-] Cheatsheet folder does not exist." && exit 1
  32. # if yes, switch to that dir.
  33. cd $cheatsheets
  34. if [ $# -eq 0 ] ; then
  35. #vim -R -c "Goyo 90%" "$(fzf)"
  36. chosen_cheatsheet=`fzf`
  37. md-viewer "$chosen_cheatsheet"
  38. exit 1
  39. fi
  40. # Option Parsin
  41. while getopts ":ghlws:pi" options ; do
  42. case "${options}" in
  43. h) usage ;; # print help/usage
  44. l) fd -t f \.md -x echo {/.} ;; # list all files
  45. p) echo $cheatsheets ;; # print cheatsheets path
  46. w) vim -c "Goyo 90%" "$(fzf)" ;; # writable
  47. g) vim -R -c "Goyo 90%" "$(fzf)" -g ;; # open in gvim
  48. s) rg "${OPTARG}" ;; # search for string in <arg> using ripgrep
  49. i) interactive ;; # interactive dmenu/rofi menu
  50. *) echo 'Invalid Argument' ;; # Argument not available
  51. esac
  52. done