-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-code.py
More file actions
executable file
·66 lines (50 loc) · 2.17 KB
/
format-code.py
File metadata and controls
executable file
·66 lines (50 loc) · 2.17 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import subprocess
import argparse
count = 0
def find_source_files(root_dir, ignore_dirs):
source_files = []
for dirpath, dirnames, filenames in os.walk(root_dir):
# Remove ignored directories from dirnames to prevent walking into them
dirnames[:] = [d for d in dirnames if d not in ignore_dirs]
for filename in filenames:
if filename.endswith((".c", ".cpp", ".h", ".hpp", ".cc", ".cxx", ".hh")):
source_files.append(os.path.join(dirpath, filename))
return source_files
def format_files(files, clang_format, style):
global count
for file in files:
try:
cmd = [clang_format, "-i", "--style", style, file]
subprocess.run(cmd, check=True)
print(f"\033[32mFormatted:\033[0m {file}")
count += 1
except subprocess.CalledProcessError as e:
print(f"\033[31mError formatting {file}:\033[0m {e}")
def main():
parser = argparse.ArgumentParser(
description="Recursively format C/C++ files with clang-format"
)
parser.add_argument("root_dir", help="Root directory to search for source files")
parser.add_argument("--ignore", nargs="+", default=[], help="Directories to ignore")
parser.add_argument(
"--clang-format", default="clang-format", help="Path to clang-format executable"
)
parser.add_argument(
"--style", default="file", help="Formatting style (file/Google/LLVM/etc.)"
)
args = parser.parse_args()
print("\033[36m=== C/C++ Source Formatter ===\033[0m")
print(f"\033[33mRoot directory:\033[0m {args.root_dir}")
print(f"\033[33mIgnored directories:\033[0m {args.ignore or 'None'}")
print(f"\033[33mStyle:\033[0m {args.style}")
print("\033[36m" + "=" * 30 + "\033[0m")
source_files = find_source_files(args.root_dir, args.ignore)
if not source_files:
print("\033[33mNo C/C++ files found to format.\033[0m")
return
print(f"\033[33mFound {len(source_files)} files to format:\033[0m")
format_files(source_files, args.clang_format, args.style)
print(f"\033[32mFormatting complete ({count} files)!\033[0m")
if __name__ == "__main__":
main()