-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconform-whitespace
executable file
·25 lines (19 loc) · 1.87 KB
/
conform-whitespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# Default behavior is to ignore hidden files
hidden_filter="-not -path '*/.*'"
hides_files_notice="(ignores hidden files (dotfiles))"
if [ "$1" = "--include-hidden" ]; then
hidden_filter=""
hides_files_notice="(you have elected to have this command NOT ignore hidden files (dotfiles), and so they shall be processed as well)"
fi
echo Trimming trailing whitespace... $hides_files_notice
echo '* Note: this script apparently leaves CRLF line endings alone, which is more tolerant than I thought perl would be.'
find . -size +0 -not \( -iname .svn -prune -o -iname .git -prune -o -iname '*.pdf' \) $hidden_filter -type f -print0 | # Thanks to https://stackoverflow.com/questions/149057/how-to-remove-trailing-whitespace-of-all-files-recursively#comment78251151_149081, which I modified.
perl -0 -nle 'print if -T' | # thanks to https://superuser.com/a/328483/ ; finds only text files (non-"binary" files).
xargs -0 -P 16 perl -pi -e 's/[^\S\v]+$//' # [^\S\n] is \s but without \n (the actual newline). Thanks to https://stackoverflow.com/a/3469155 for this. #actually it occurred to me that I would like to allow for vertical tab as well, so I use \v, which covers all vertical whitespace (incl newline).
echo
echo Enforcing the existence of exactly a single trailing newline... $hides_files_notice
echo "* This program is very slow, because it reads all the files into memory, even though it just has to read the last bytes and see if there are any \ns there. Well, that said, I'm tired of writing the program now, so whatever..."
find . -size +0 -not \( -iname .svn -prune -o -iname .git -prune -o -iname '*.pdf' \) $hidden_filter -type f -print0 |
perl -0 -nle 'print if -T' | # thanks to https://superuser.com/a/328483/ ; finds only text files (non-"binary" files).
xargs -0 -P 16 perl -i -0777 -wpe 's/\n*$/\n/' # Thanks to https://stackoverflow.com/a/39540892 for this general idea.