Print progress when not in verbose mode

This commit is contained in:
Andrew 2026-01-14 00:49:39 +13:00
parent 6b882073a4
commit e155fdfecb

View file

@ -6,9 +6,10 @@ use std::num::NonZeroU64;
use std::{ use std::{
ffi::{OsStr, OsString}, ffi::{OsStr, OsString},
fs::DirBuilder, fs::DirBuilder,
io::Write, io::{IsTerminal, Write, stdout},
path::PathBuf, path::PathBuf,
process::ExitCode, process::ExitCode,
sync::atomic::{AtomicUsize, Ordering::AcqRel},
time::Duration, time::Duration,
}; };
@ -69,17 +70,28 @@ fn main() -> ExitCode {
) )
}; };
let parallel_files = matches.get_flag("parallel-files"); let is_verbose = matches.get_count("verbose") > 0;
let results: Vec<OptimizationResult> = if parallel_files { let print_summary = !matches.get_flag("quiet") && !matches!(out_file, OutFile::StdOut);
files let print_progress = print_summary && !is_verbose && stdout().is_terminal();
.into_par_iter() let total_files = files.len();
.map(|(input, output)| process_file(&input, &output, &opts)) let num_processed = AtomicUsize::new(0);
.collect() if print_progress {
print!("Files processed: 0/{}...", total_files);
stdout().flush().ok();
}
let process = |(input, output): (InFile, OutFile)| {
let result = process_file(&input, &output, &opts);
if print_progress && matches!(result, OptimizationResult::Ok(_, _)) {
let value = num_processed.fetch_add(1, AcqRel) + 1;
print!("\rFiles processed: {}/{}...", value, total_files);
stdout().flush().ok();
}
result
};
let results: Vec<OptimizationResult> = if matches.get_flag("parallel-files") {
files.into_par_iter().map(process).collect()
} else { } else {
files files.into_iter().map(process).collect()
.into_iter()
.map(|(input, output)| process_file(&input, &output, &opts))
.collect()
}; };
// Collect stats // Collect stats
@ -104,7 +116,7 @@ fn main() -> ExitCode {
} }
// Print summary // Print summary
if !matches.get_flag("quiet") && !matches!(out_file, OutFile::StdOut) { if print_summary {
let in_bytes = format_bytes(total_in, true); let in_bytes = format_bytes(total_in, true);
let out_bytes = format_bytes(total_out, true); let out_bytes = format_bytes(total_out, true);
let saved = total_in - total_out; let saved = total_in - total_out;
@ -114,7 +126,10 @@ fn main() -> ExitCode {
} else { } else {
0_f64 0_f64
}; };
println!("Files processed: {num_succeeded}"); if is_verbose {
println!("--------------------");
}
println!("\rFiles processed: {num_succeeded}/{total_files} ");
println!("Input size: {}", in_bytes); println!("Input size: {}", in_bytes);
println!("Output size: {}", out_bytes); println!("Output size: {}", out_bytes);
println!("Total saved: {} ({:.2}%)", saved_bytes, percent); println!("Total saved: {} ({:.2}%)", saved_bytes, percent);
@ -225,7 +240,8 @@ fn parse_opts_into_struct(
match record.level() { match record.level() {
Level::Error | Level::Warn => { Level::Error | Level::Warn => {
let style = buf.default_level_style(record.level()); let style = buf.default_level_style(record.level());
writeln!(buf, "{style}{}{style:#}", record.args()) // Prepend carriage return to clear progress line
writeln!(buf, "\r{style}{}{style:#}", record.args())
} }
// Leave info, debug and trace unstyled // Leave info, debug and trace unstyled
_ => writeln!(buf, "{}", record.args()), _ => writeln!(buf, "{}", record.args()),