11import argparse
2- import subprocess
32import os
3+ import subprocess
44from pathlib import Path
5+
56from colorama import Fore , Style
67
78# Supported file types and their corresponding formatter categories
@@ -77,8 +78,9 @@ def format_file(file: Path, check: bool, formatter) -> bool:
7778 check = True ,
7879 )
7980 print (f"{ Fore .CYAN } Formatted: { file } { Style .RESET_ALL } " )
80-
81+ elif formatter == "ruff" :
8182 ruff_cmd = ["ruff" , "check" , file ]
83+ ruff_format_cmd = ["ruff" , "format" , file ]
8284 try :
8385 if check :
8486 process = subprocess .run (
@@ -93,6 +95,22 @@ def format_file(file: Path, check: bool, formatter) -> bool:
9395 f"Use { Fore .CYAN } ruff check --fix { file } { Style .RESET_ALL } to fix it."
9496 )
9597 formatted = False
98+
99+ ruff_format_cmd .insert (2 , "--check" )
100+ process = subprocess .run (
101+ ruff_format_cmd ,
102+ capture_output = True ,
103+ text = True ,
104+ check = False ,
105+ )
106+ if process .returncode != 0 :
107+ print (
108+ f"{ Fore .YELLOW } { file } is not ruff-formatted.{ Style .RESET_ALL } "
109+ )
110+ print (
111+ f"Use { Fore .CYAN } ruff format { file } { Style .RESET_ALL } to format it."
112+ )
113+ formatted = False
96114 else :
97115 ruff_cmd .insert (2 , "--fix" )
98116 process = subprocess .run (
@@ -110,6 +128,24 @@ def format_file(file: Path, check: bool, formatter) -> bool:
110128 formatted = False
111129 else :
112130 print (f"{ Fore .CYAN } Ruff fixed: { file } { Style .RESET_ALL } " )
131+
132+ process = subprocess .run (
133+ ruff_format_cmd ,
134+ capture_output = True ,
135+ text = True ,
136+ check = False ,
137+ )
138+ if process .returncode != 0 :
139+ print (
140+ f"{ Fore .RED } Ruff format failed for { file } .{ Style .RESET_ALL } "
141+ )
142+ if process .stdout :
143+ print (process .stdout , end = "" )
144+ if process .stderr :
145+ print (process .stderr , end = "" )
146+ formatted = False
147+ else :
148+ print (f"{ Fore .CYAN } Ruff formatted: { file } { Style .RESET_ALL } " )
113149 except FileNotFoundError :
114150 print (
115151 f"{ Fore .RED } Formatter ruff not found, { file } skipped.{ Style .RESET_ALL } "
@@ -216,9 +252,7 @@ def main():
216252 parser .add_argument (
217253 "--c" , default = "clang-format-16" , help = "C formatter (default: clang-format-16)"
218254 )
219- parser .add_argument (
220- "--py" , default = "black" , help = "Python formatter (default: black)"
221- )
255+ parser .add_argument ("--py" , default = "ruff" , help = "Python formatter (default: ruff)" )
222256 args = parser .parse_args ()
223257
224258 if args .ref is None and args .path is None :
0 commit comments