Skip to content

Instantly share code, notes, and snippets.

@lenaschimmel
Last active March 8, 2023 15:39
Show Gist options
  • Save lenaschimmel/bac310ac8269e40a18492c7c9cf7f7b4 to your computer and use it in GitHub Desktop.
Save lenaschimmel/bac310ac8269e40a18492c7c9cf7f7b4 to your computer and use it in GitHub Desktop.
Analyzes indentation in all files matching the find pattern in the given directory.
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY PATTERN" >&2
echo "Analyzes indentation in all files matching the find pattern in the given directory. Remember to quote your pattern."
exit 1
fi
TARGETDIR=$(realpath $1)
TARGETDIR_NAME=$(basename "$TARGETDIR")
PATTERN=$2
TMPFILE=${TMPDIR:-/tmp}/tmp_analyze_indent_$TARGETDIR_NAME
echo ""
echo "Analyzing indentation in project $TARGETDIR_NAME for files named '$PATTERN'..."
echo ""
find $TARGETDIR -name "$PATTERN" -exec cat {} > $TMPFILE \;
printf '%5s lines total\n' `cat $TMPFILE | wc -l`
echo ""
printf '%5s empty lines (not even whitespace)\n' `grep --count --no-filename -E "^$" $TMPFILE`
printf '%5s lines with only whitespace (no code)\n' `grep --count --no-filename -E "^\s+$" $TMPFILE`
printf '%5s lines with some whitespace before code\n' `grep --count --no-filename -E "^\s+\S" $TMPFILE`
printf '%5s lines with tabs before code\n' `grep --count --no-filename -E "^\t+\S" $TMPFILE`
printf '%5s lines with spaces before code\n' `grep --count --no-filename -E "^ +\S" $TMPFILE`
printf '%5s lines with tabs AND spaces before code\n' `grep --count --no-filename -E "^(\t+ +)|( +\t+)\S" $TMPFILE`
echo ""
printf '%5s lines with whitespace AFTER code\n' `grep --count --no-filename -E "\S( |\t)+$" $TMPFILE`
echo ""
for NUM in {1..20}
do
printf '%5s lines with %2s spaces before code\n' `grep --count --no-filename -E "^ {$NUM}\S" $TMPFILE` $NUM
done
echo ""
rm $TMPFILE
@lenaschimmel
Copy link
Author

Output will look like this:

15113 lines total

  738 empty lines (not even whitespace)
 1561 lines with only whitespace (no code)
10958 lines with some whitespace before code
 6642 lines with tabs before code
 3839 lines with spaces before code
  338 lines with tabs AND spaces before code

 5489 lines with whitespace AFTER code

  148 lines with  1 spaces before code
   52 lines with  2 spaces before code
   29 lines with  3 spaces before code
  765 lines with  4 spaces before code
    4 lines with  5 spaces before code
  765 lines with  6 spaces before code
    0 lines with  7 spaces before code
  920 lines with  8 spaces before code
    0 lines with  9 spaces before code
    1 lines with 10 spaces before code
    0 lines with 11 spaces before code
  969 lines with 12 spaces before code
    0 lines with 13 spaces before code
    0 lines with 14 spaces before code
    0 lines with 15 spaces before code
  170 lines with 16 spaces before code
    0 lines with 17 spaces before code
    0 lines with 18 spaces before code
    1 lines with 19 spaces before code
   15 lines with 20 spaces before code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment