Skip to content

Commit 1151b30

Browse files
committed
Only write files when the formatter actually modified them
We already checked if the files were formatted, but wrote them unconditionally, so now this is handled. Close #214
1 parent fbd2457 commit 1151b30

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/main.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
264264

265265
// If true, all input files were already formatted (used for check mode)
266266
let mut all_formatted = true;
267+
let mut modified_file_count = 0;
267268
for output in sorted_outputs {
268269
match output {
269270
Ok(output) => {
@@ -282,14 +283,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
282283
println!("#--file:{}", output.file_path.display());
283284
}
284285
print!("{}", output.formatted_content);
285-
} else {
286-
fs::write(&output.file_path, output.formatted_content).map_err(|e| {
286+
} else if !output.is_formatted {
287+
fs::write(&output.file_path, output.formatted_content).map_err(|error| {
287288
format!(
288289
"Failed to write to file {}: {}",
289290
output.file_path.display(),
290-
e
291+
error
291292
)
292293
})?;
294+
modified_file_count += 1;
293295
}
294296
}
295297
Err(error_msg) => {
@@ -310,9 +312,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
310312
} else if !args.stdout {
311313
terminal_clear_line();
312314
if total_files == 1 {
313-
eprintln!("\rFormatted {}", input_gdscript_files[0].display());
315+
if modified_file_count > 0 {
316+
eprintln!("\rFormatted {}", input_gdscript_files[0].display());
317+
} else {
318+
eprintln!("\rAlready formatted: {}", input_gdscript_files[0].display());
319+
}
314320
} else {
315-
eprintln!("\rFormatted {} files", total_files);
321+
let already_formatted_count = total_files - modified_file_count;
322+
if modified_file_count > 0 && already_formatted_count > 0 {
323+
eprintln!(
324+
"\rFormatted {} files, {} already formatted",
325+
modified_file_count, already_formatted_count
326+
);
327+
} else if modified_file_count > 0 {
328+
eprintln!("\rFormatted {} files", modified_file_count);
329+
} else {
330+
eprintln!("\rAll {} files already formatted", total_files);
331+
}
316332
}
317333
}
318334

0 commit comments

Comments
 (0)