insanity.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env bash
  2. NOTICE="\e[1;34m" # bold light blue
  3. WARNING="\e[1;31m" # bold red
  4. RESET="\e[0m"
  5. notice() {
  6. echo -e "${NOTICE}$1${RESET}"
  7. }
  8. warning() {
  9. echo -e "${WARNING}$1${RESET}"
  10. }
  11. show_help() {
  12. echo -e "-h, -?\t\tShow this message."
  13. echo -e "-u\t\tTry to update the typo database."
  14. echo -e "-n\t\tDisable git grep's pager."
  15. }
  16. _software_notfound() {
  17. echo -e "${WARNING}ERROR:\t$1 must be installed in order to update the database${RESET}"
  18. echo -e "\tOn a debian based distro, run \e[38;5;244m\`sudo apt-get install $1'${RESET}"
  19. }
  20. update_typo() {
  21. if ! command -v xmllint>/dev/null; then
  22. _software_notfound "libxml2-utils"
  23. exit 1
  24. fi
  25. if ! command -v recode>/dev/null; then
  26. _software_notfound "recode"
  27. exit 1
  28. fi
  29. curl -s https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings/For_machines | xmllint --html --xpath '//pre/text()' - | recode html | awk -F"->" '{ print $1 }' | sed 's/\s$//' > ct.txt
  30. if cmp --silent common_typos.txt ct.txt; then
  31. rm ct.txt
  32. echo -e "No update necessary."
  33. else
  34. echo -e "The typo database has been updated. Differences:"
  35. diff -u common_typos.txt ct.txt
  36. mv ct.txt common_typos.txt
  37. fi
  38. exit 0
  39. }
  40. OPTIND=1
  41. no_pager=0
  42. while getopts "h?un" opt; do
  43. case "$opt" in
  44. h|\?)
  45. show_help
  46. exit 0
  47. ;;
  48. u)
  49. update_typo
  50. exit 0
  51. ;;
  52. n)
  53. no_pager=1
  54. ;;
  55. esac
  56. done
  57. #shift $((OPTIND-1))
  58. if [ "$no_pager" -gt 0 ]; then
  59. GREP="git --no-pager grep -n --color"
  60. else
  61. GREP="git grep -n --color"
  62. fi
  63. _build_negative_match() {
  64. output=""
  65. for arg in "$@"
  66. do
  67. output+=" --and --not -e \"${arg}\""
  68. done
  69. }
  70. notice 'Checking repeating words... (e.g. "voice voice")'
  71. _build_negative_match " ha ha " " no no " " sweet sweet " " low low "\
  72. " that that " " really really " " so so "\
  73. " her her "
  74. $GREP -e " \(\b[a-zA-Z][a-zA-Z]\+\) \1\b ""$output" -- locations/
  75. notice 'Checking bad articles... (e.g. "a automatic" or "an color")'
  76. $GREP -i -E "\Wa (a|e|i|o|u)." -- locations/ | grep -a -i -vE "\Wa (un|eu|us|ut|on|ur|in)."
  77. echo -en "\e[38;5;34m-";for i in {35..39} {39..34}; do echo -en "\e[38;5;${i}m*-*-"; done; echo -e "\e[0m"
  78. $GREP -i -E "\Wan (b|c|d|f|g|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z)\w+" -- locations/
  79. notice 'Checking syntax mistakes... (e.g. "<<\$var><")'
  80. $GREP -a -E '<<\$[^>]+><' -- locations/
  81. $GREP -a -E "^\W+\wact\W+'" -- locations/
  82. notice 'Checking common typos... (e.g. "recieve")'
  83. $GREP -i -a -E "\W(carress)" -- locations/
  84. echo -en "\n\e[0;31m";for i in {0..80}; do echo -en "#"; done; echo -e "${RESET}"
  85. warning 'The following will not be matching lines with variables ($variable['"''index''"']).'
  86. echo -en "\e[0;31m";for i in {0..80}; do echo -en "#"; done; echo -e "${RESET}\n"
  87. $GREP -i -a -E --not -e '^\W+(!|if|elseif)' --and --not -e '\$[a-zA-Z]+(\[|>)' --and --not -e 'teh' --and -e "\W($(cat common_typos.txt| tr '\n' '|'|sed 's/|$//'))\W" -- locations