1+ #! /usr/bin/env bash
2+
3+ # By Gabriel Staples
4+ # For documentation on this file, see my answer here:
5+ # [my answer] How to call clang-format over a cpp project folder?:
6+ # https://stackoverflow.com/a/65988393/4561887
7+
8+ # See my ans: https://stackoverflow.com/a/60157372/4561887
9+ FULL_PATH_TO_SCRIPT=" $( realpath " $0 " ) "
10+ SCRIPT_DIRECTORY=" $( dirname " $FULL_PATH_TO_SCRIPT " ) "
11+
12+ RETURN_CODE_SUCCESS=0
13+ RETURN_CODE_ERROR=1
14+
15+ # Find all files in SCRIPT_DIRECTORY with one of these extensions
16+ FILE_LIST=" $( find " $SCRIPT_DIRECTORY " -not \( -path " */external/*" -prune \) -not \( -path " */build/*" -prune \) \
17+ | grep -E " .*\.(ino|cpp|c|h|hpp|hh)$" ) "
18+ # echo "\"$FILE_LIST\"" # debugging
19+ # split into an array; see my ans: https://stackoverflow.com/a/71575442/4561887
20+ # mapfile -t FILE_LIST_ARRAY <<< "$FILE_LIST"
21+ IFS=$' \n ' read -r -d ' ' -a FILE_LIST_ARRAY <<< " $FILE_LIST"
22+
23+ num_files=" ${# FILE_LIST_ARRAY[@]} "
24+ echo -e " $num_files files found to format:"
25+ if [ " $num_files " -eq 0 ]; then
26+ echo " Nothing to do."
27+ exit $RETURN_CODE_SUCCESS
28+ fi
29+
30+ # print the list of all files
31+ for i in " ${! FILE_LIST_ARRAY[@]} " ; do
32+ file=" ${FILE_LIST_ARRAY["$i"]} "
33+ printf " %2i: %s\n" $(( i + 1 )) " $file "
34+ done
35+ echo " "
36+
37+ format_files=" false"
38+ # See: https://stackoverflow.com/a/226724/4561887
39+ read -p " Do you wish to auto-format all of these files [y/N] " user_response
40+ case " $user_response " in
41+ [Yy]* ) format_files=" true"
42+ esac
43+
44+ if [ " $format_files " = " false" ]; then
45+ echo " Aborting."
46+ exit $RETURN_CODE_SUCCESS
47+ fi
48+
49+ # Format each file.
50+ clang-format --verbose -i --style=file " ${FILE_LIST_ARRAY[@]} "
0 commit comments