A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1 KiB

  1. #!/bin/bash
  2. # Bad Code Search, with syntax highlighting.
  3. #
  4. # Sample usage:
  5. # bcs.sh --file "Line" Vector3
  6. # bcs.sh -i new vector3
  7. # bcs.sh -C 10 new Vector3
  8. # bcs.sh -x py sprite
  9. # bcs.sh -i -x cs -f World spritebatch
  10. # bcs.sh --tree develop -x cs -f MonoGame.Framework SpriteBatch
  11. # https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
  12. POSITIONAL=()
  13. GREP_FLAGS=""
  14. CONTEXT=3
  15. TREE=master
  16. while [[ $# -gt 0 ]]
  17. do
  18. key="$1"
  19. case $key in
  20. -f|--file)
  21. FILE="$2"
  22. shift # past argument
  23. shift # past value
  24. ;;
  25. -x|--extension)
  26. EXTENSION="\.$2\$"
  27. shift # past argument
  28. shift # past value
  29. ;;
  30. -i|--ignore-case)
  31. GREP_FLAGS="-i"
  32. shift # past argument
  33. ;;
  34. -C|--context)
  35. CONTEXT="$2"
  36. shift # past argument
  37. shift # past value
  38. ;;
  39. -t|--tree)
  40. TREE="$2"
  41. shift # past argument
  42. shift # past value
  43. ;;
  44. *) # unknown option
  45. POSITIONAL+=("$1") # save it in an array for later
  46. shift # past argument
  47. ;;
  48. esac
  49. done
  50. set -- "${POSITIONAL[@]}" # restore positional parameters
  51. if [[ -z $@ ]]; then
  52. echo 'Usage: bcs.sh [-i] [-f/--file FILE_PATTERN] [-x/--extension EXTENSION] [-C/--context NUM_LINES] [-t/--tree TREE_NAME] QUERY'
  53. exit 1
  54. fi
  55. # highlight(1) sticks some ANSI cruft into the output.
  56. # We just turn spaces into "match anything", which kinda ignores the cruft.
  57. QUERY=`echo $@ | sed -e "s/ /.*/g"`
  58. if [[ -n $EXTENSION ]]; then
  59. FILE="${FILE}.*${EXTENSION}"
  60. fi
  61. if [[ -n $FILE ]]; then
  62. files=$(git ls-tree -r ${TREE} --name-only | grep "${FILE}")
  63. if [[ -z $files ]]; then
  64. echo "no files matched"
  65. exit 0
  66. fi
  67. for file in `grep -l ${GREP_FLAGS} "${QUERY}" $files`;
  68. do
  69. echo $file:
  70. highlight --force --line-numbers -O xterm256 --stdout $file | \
  71. grep -C ${CONTEXT} ${GREP_FLAGS} "${QUERY}" --label=$file | perl -pne s%^--\$%$file:%
  72. done
  73. else
  74. for file in `grep -l ${GREP_FLAGS} "${QUERY}" $(git ls-tree -r master --name-only)`
  75. do
  76. echo $file:
  77. highlight --force --line-numbers -O xterm256 --stdout $file | \
  78. grep -C ${CONTEXT} ${GREP_FLAGS} "${QUERY}" --label=$file | perl -pne s%^--\$%$file:%
  79. done
  80. fi