Skip to content

Commit bdfe972

Browse files
committed
report progress when formatting cpp files
1 parent 03cd23a commit bdfe972

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

extras/formatting.sh

+36-14
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ require_bin() {
7070
local version
7171

7272
if ! command -v "$name" &>/dev/null; then
73-
echo "This script needs $name, but it isn't in \$PATH"
73+
echo "This script needs $name, but it isn't in \$PATH" >&2
7474
missing_bin=1
7575
return
7676
fi
@@ -79,14 +79,14 @@ require_bin() {
7979
version=$("$name" --version | grep -oP "\d+\.\d+\.?\d*" | head -n1)
8080

8181
if ! printf '%s\n%s\n' "$min_version" "$version" | sort -V -C; then
82-
echo "$name version $version is too old. Version $min_version or newer is required."
82+
echo "$name version $version is too old. Version $min_version or newer is required." >&2
8383
missing_bin=1
8484
return
8585
fi
8686

8787
if [ -n "$max_version" ]; then
8888
if ! printf '%s\n%s\n' "$version" "$max_version" | sort -V -C; then
89-
echo "$name version $version is too new. Version less than $max_version is required."
89+
echo "$name version $version is too new. Version less than $max_version is required." >&2
9090
missing_bin=1
9191
return
9292
fi
@@ -109,7 +109,7 @@ fi
109109
exit_code=0
110110

111111
cmake_formatting() {
112-
echo "Formatting CMake files..."
112+
echo "Formatting CMake files..." >&2
113113

114114
readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
115115

@@ -127,21 +127,42 @@ cmake_formatting() {
127127
fi
128128
}
129129

130+
track_progress() {
131+
# Don't output the progress bar if stderr isn't a terminal, just eat all the input
132+
[ -t 2 ] || {
133+
cat >/dev/null
134+
return
135+
}
136+
137+
local total=$1
138+
local current=0
139+
140+
while IFS= read -r _; do
141+
((current++)) || :
142+
percent=$((current * 100 / total))
143+
printf '\rProgress: [%-50s] %d%%' "$(printf '#%.0s' $(seq 1 $((percent / 2))))" "$percent" >&2
144+
done
145+
echo >&2
146+
}
147+
130148
cpp_formatting() {
131-
echo "Formatting cpp files..."
149+
echo "Formatting cpp files..." >&2
132150

133151
readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
134152

153+
# The progress reporting is a bit sneaky, we use `--verbose` with xargs which
154+
# prints a line to stderr for each command, and we simply count these...
155+
135156
if [ "$check_only" -eq 1 ]; then
136157
local tmpdir
137158
tmpdir=$(mktemp -d)
138159
trap 'rm -rf "$tmpdir"' EXIT
139160

140-
printf '%s\n' "${files[@]}" | xargs -P "$(nproc)" -I{} bash -c "
161+
printf '%s\n' "${files[@]}" | xargs --verbose -P "$(nproc)" -I{} bash -c "
141162
mkdir -p \"\$(dirname \"$tmpdir/{}\")\"
142-
diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
163+
diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
143164
:
144-
"
165+
" |& track_progress ${#files[@]}
145166

146167
for file in "${files[@]}"; do
147168
if [ -s "$tmpdir/$file" ]; then
@@ -150,12 +171,13 @@ cpp_formatting() {
150171
fi
151172
done
152173
else
153-
printf '%s\n' "${files[@]}" | xargs -n1 -P "$(nproc)" clang-format -i
174+
printf '%s\n' "${files[@]}" | xargs --verbose -n1 -P "$(nproc)" clang-format -i |&
175+
track_progress ${#files[@]}
154176
fi
155177
}
156178

157179
yaml_json_formatting() {
158-
echo "Formatting yaml and json files..."
180+
echo "Formatting yaml and json files..." >&2
159181

160182
readarray -t files < <(git ls-files "*.yaml" "*.yml" "*.json" ':!external/**')
161183

@@ -170,12 +192,12 @@ yaml_json_formatting() {
170192
fi
171193
done
172194
else
173-
prettier --write "${files[@]}" | grep -v '(unchanged)' || :
195+
prettier --write "${files[@]}" | grep -v '(unchanged)' >&2 || :
174196
fi
175197
}
176198

177199
sh_formatting() {
178-
echo "Formatting sh files..."
200+
echo "Formatting sh files..." >&2
179201

180202
readarray -t files < <(git ls-files "*.sh")
181203

@@ -191,9 +213,9 @@ sh_formatting() {
191213
fi
192214
}
193215

216+
((run_all || run_sh)) && sh_formatting
194217
((run_all || run_cmake)) && cmake_formatting
195-
((run_all || run_cpp)) && cpp_formatting
196218
((run_all || run_yaml)) && yaml_json_formatting
197-
((run_all || run_sh)) && sh_formatting
219+
((run_all || run_cpp)) && cpp_formatting
198220

199221
exit $exit_code

0 commit comments

Comments
 (0)