Skip to content

Commit d358a74

Browse files
author
Daniel Tobon
committed
clang formatter script file
1 parent c992f55 commit d358a74

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,4 @@ docker/
4242
.vscode/
4343

4444
# dev
45-
clang-format-all.sh
4645
display_pcl.sh

clang-format-all.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)