|
8 | 8 | RED = "\033[31m" |
9 | 9 | GREEN = "\033[32m" |
10 | 10 | YELLOW = "\033[33m" |
11 | | -NC = "\033[0m" # No Color |
| 11 | +NC = "\033[0m" # No Color |
12 | 12 | BOLD = "\033[1m" # No Color |
13 | 13 |
|
14 | 14 | # File Extension filter. You can add new extension |
|
22 | 22 |
|
23 | 23 |
|
24 | 24 | def print_usage(): |
25 | | - """Print the usage instructions.""" |
26 | | - print(f"{YELLOW}Usage: convert_tabs(file_path, tab_size, conversion_type){NC}") |
27 | | - print("<conversion_type>: 'spaces' or 'tabs'") |
| 25 | + """Print the usage instructions.""" |
| 26 | + print(f"{YELLOW}Usage: convert_tabs(file_path, tab_size, conversion_type){NC}") |
| 27 | + print("<conversion_type>: 'spaces' or 'tabs'") |
28 | 28 |
|
29 | 29 |
|
30 | 30 | def print_error(message): |
31 | | - """Print error messages.""" |
32 | | - print(f"{RED}Error: {message}{NC}") |
| 31 | + """Print error messages.""" |
| 32 | + print(f"{RED}Error: {message}{NC}") |
33 | 33 |
|
34 | 34 |
|
35 | 35 | def validate_positive_integer(value): |
36 | | - """Validate if the value is a positive integer.""" |
37 | | - try: |
38 | | - int_value = int(value) |
39 | | - if int_value < 1: |
40 | | - raise ValueError |
41 | | - return int_value |
42 | | - except ValueError: |
43 | | - print_error("Tab size must be a positive integer.") |
44 | | - return None |
| 36 | + """Validate if the value is a positive integer.""" |
| 37 | + try: |
| 38 | + int_value = int(value) |
| 39 | + if int_value < 1: |
| 40 | + raise ValueError |
| 41 | + return int_value |
| 42 | + except ValueError: |
| 43 | + print_error("Tab size must be a positive integer.") |
| 44 | + return None |
45 | 45 |
|
46 | 46 |
|
47 | 47 | def file_exists(file_path): |
48 | | - """Check if the file exists.""" |
49 | | - if not os.path.isfile(file_path): |
50 | | - print_error(f"File not found: {file_path}") |
51 | | - return False |
52 | | - return True |
| 48 | + """Check if the file exists.""" |
| 49 | + if not os.path.isfile(file_path): |
| 50 | + print_error(f"File not found: {file_path}") |
| 51 | + return False |
| 52 | + return True |
53 | 53 |
|
54 | 54 |
|
55 | 55 | def convert_tabs(file_path, tab_size, conversion_type): |
56 | | - """Convert tabs to spaces or spaces to tabs based on conversion type.""" |
57 | | - try: |
58 | | - if conversion_type == "spaces": |
59 | | - print(f"{BOLD}Converting tabs to spaces...{NC}") |
60 | | - subprocess.run( |
61 | | - ["expand", "-t", str(tab_size), file_path], |
62 | | - stdout=open(f"{file_path}.tmp", "w"), |
63 | | - ) |
64 | | - elif conversion_type == "tabs": |
65 | | - print(f"{BOLD}Converting spaces to tabs...{NC}") |
66 | | - subprocess.run( |
67 | | - ["unexpand", "-t", str(tab_size), file_path], |
68 | | - stdout=open(f"{file_path}.tmp", "w"), |
69 | | - ) |
70 | | - else: |
71 | | - print_error( |
72 | | - f"Invalid conversion type: {conversion_type}. Use 'spaces' or 'tabs'." |
73 | | - ) |
74 | | - return |
75 | | - |
76 | | - os.replace(f"{file_path}.tmp", file_path) |
77 | | - print(f"{GREEN}Conversion completed successfully: {file_path}{NC}") |
78 | | - except Exception as e: |
79 | | - print_error(f"Conversion failed: {str(e)}") |
| 56 | + """Convert tabs to spaces or spaces to tabs based on conversion type.""" |
| 57 | + try: |
| 58 | + if conversion_type == "spaces": |
| 59 | + print(f"{BOLD}Converting tabs to spaces...{NC}") |
| 60 | + subprocess.run( |
| 61 | + ["expand", "-t", str(tab_size), file_path], |
| 62 | + stdout=open(f"{file_path}.tmp", "w"), |
| 63 | + ) |
| 64 | + elif conversion_type == "tabs": |
| 65 | + print(f"{BOLD}Converting spaces to tabs...{NC}") |
| 66 | + subprocess.run( |
| 67 | + ["unexpand", "-t", str(tab_size), file_path], |
| 68 | + stdout=open(f"{file_path}.tmp", "w"), |
| 69 | + ) |
| 70 | + else: |
| 71 | + print_error( |
| 72 | + f"Invalid conversion type: {conversion_type}. Use 'spaces' or 'tabs'." |
| 73 | + ) |
| 74 | + return |
| 75 | + |
| 76 | + os.replace(f"{file_path}.tmp", file_path) |
| 77 | + print(f"{GREEN}Conversion completed successfully: {file_path}{NC}") |
| 78 | + except Exception as e: |
| 79 | + print_error(f"Conversion failed: {str(e)}") |
80 | 80 |
|
81 | 81 |
|
82 | 82 | def convert_file(file_path, tab_size, conversion_type): |
83 | | - """Main function to manage conversion.""" |
84 | | - tab_size = validate_positive_integer(tab_size) |
85 | | - if tab_size is None: |
86 | | - return |
| 83 | + """Main function to manage conversion.""" |
| 84 | + tab_size = validate_positive_integer(tab_size) |
| 85 | + if tab_size is None: |
| 86 | + return |
87 | 87 |
|
88 | | - if not file_exists(file_path): |
89 | | - return |
| 88 | + if not file_exists(file_path): |
| 89 | + return |
90 | 90 |
|
91 | | - convert_tabs(file_path, tab_size, conversion_type) |
| 91 | + convert_tabs(file_path, tab_size, conversion_type) |
92 | 92 |
|
93 | 93 |
|
94 | 94 | def main(): |
95 | | - # Set the current working directory for scanning c/c++ sources (including |
96 | | - # header files) and apply the clang formatting |
97 | | - # Please note "-style" is for standard style options |
98 | | - # and "-i" is in-place editing |
99 | | - |
100 | | - if len(sys.argv) > 1: |
101 | | - print(f"{BOLD}Format {sys.argv[1]}{NC}") |
102 | | - os.system(f"{RUFF} check {sys.argv[1]} --fix") |
103 | | - os.system(f"{RUFF} format {sys.argv[1]}") |
104 | | - print(f"{GREEN}Formatting completed successfully: {sys.argv[1]}{NC}") |
105 | | - convert_file(f"{sys.argv[1]}", "4", "tabs") |
106 | | - return |
107 | | - |
108 | | - for root, dirs, files in os.walk(os.getcwd()): |
109 | | - if len(set(root.split("/")).intersection(IGNORED_DIRS)) > 0: |
110 | | - continue |
111 | | - for file in files: |
112 | | - if file.endswith(py_extensions): |
113 | | - print(f"{BOLD}Format {file}: {root}/{file}{NC}") |
114 | | - os.system(f"{RUFF} check {root}/{file} --fix") |
115 | | - os.system(f"{RUFF} format {root}/{file}") |
116 | | - print(f"{GREEN}Formatting completed successfully: {root}/{file}{NC}") |
117 | | - convert_file(f"{root}/{file}", "4", "tabs") |
| 95 | + # Set the current working directory for scanning c/c++ sources (including |
| 96 | + # header files) and apply the clang formatting |
| 97 | + # Please note "-style" is for standard style options |
| 98 | + # and "-i" is in-place editing |
| 99 | + |
| 100 | + if len(sys.argv) > 1: |
| 101 | + print(f"{BOLD}Format {sys.argv[1]}{NC}") |
| 102 | + os.system(f"{RUFF} check {sys.argv[1]} --fix") |
| 103 | + os.system(f"{RUFF} format {sys.argv[1]}") |
| 104 | + print(f"{GREEN}Formatting completed successfully: {sys.argv[1]}{NC}") |
| 105 | + convert_file(f"{sys.argv[1]}", "4", "tabs") |
| 106 | + return |
| 107 | + |
| 108 | + for root, dirs, files in os.walk(os.getcwd()): |
| 109 | + if len(set(root.split("/")).intersection(IGNORED_DIRS)) > 0: |
| 110 | + continue |
| 111 | + for file in files: |
| 112 | + if file.endswith(py_extensions): |
| 113 | + print(f"{BOLD}Format {file}: {root}/{file}{NC}") |
| 114 | + os.system(f"{RUFF} check {root}/{file} --fix") |
| 115 | + os.system(f"{RUFF} format {root}/{file}") |
| 116 | + print(f"{GREEN}Formatting completed successfully: {root}/{file}{NC}") |
| 117 | + convert_file(f"{root}/{file}", "4", "tabs") |
118 | 118 |
|
119 | 119 |
|
120 | 120 | if __name__ == "__main__": |
121 | | - main() |
| 121 | + main() |
0 commit comments