Only print folder name if there are non-compliant files in that folder

This commit is contained in:
Daan Boerlage 2022-09-01 01:47:43 +02:00
parent a834ab8bb9
commit 978bf187e0
Signed by: daan
GPG key ID: FCE070E1E4956606

View file

@ -36,6 +36,11 @@ impl Display for Stats {
} }
} }
struct Context {
current_folder: String,
has_printed_folder: bool,
}
fn main() -> Result<()> { fn main() -> Result<()> {
let params = Args::parse(); let params = Args::parse();
@ -48,11 +53,16 @@ fn main() -> Result<()> {
error: 0, error: 0,
}; };
let mut ctx = Context {
current_folder: "".to_string(),
has_printed_folder: false,
};
let regex = Regex::new(SERIES_REGEX).unwrap(); let regex = Regex::new(SERIES_REGEX).unwrap();
for entry in WalkDir::new(params.path) { for entry in WalkDir::new(params.path) {
if let Ok(file) = entry { if let Ok(file) = entry {
check_file(file, &mut stats, &regex, params.no_emoji, params.show_success, params.episode_name); check_file(file, &mut stats, &mut ctx, &regex, params.no_emoji, params.show_success, params.episode_name);
} }
} }
@ -61,7 +71,7 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
fn check_file(file: DirEntry, stats: &mut Stats, regex: &Regex, no_emoji: bool, show_success: bool, episode_name: bool) -> () { fn check_file(file: DirEntry, stats: &mut Stats, ctx: &mut Context, regex: &Regex, no_emoji: bool, show_success: bool, episode_name: bool) -> () {
let file_type = file.file_type(); let file_type = file.file_type();
if file_type.is_file() { if file_type.is_file() {
let filename = file.file_name().to_str().unwrap(); let filename = file.file_name().to_str().unwrap();
@ -69,10 +79,18 @@ fn check_file(file: DirEntry, stats: &mut Stats, regex: &Regex, no_emoji: bool,
ComplianceStatus::NotMatched => { ComplianceStatus::NotMatched => {
stats.files += 1; stats.files += 1;
stats.error += 1; stats.error += 1;
println!("{} {} --- (Failed to match)", get_status_icon(StatusIcon::Failure, no_emoji), filename) if !ctx.has_printed_folder {
println!("\n\n=== {} {}", get_status_icon(StatusIcon::Directory, no_emoji), ctx.current_folder);
ctx.has_printed_folder = true;
}
println!("{} {} --- (Failed to match)", get_status_icon(StatusIcon::Failure, no_emoji), filename);
} }
ComplianceStatus::NonCompliant(reason) => { ComplianceStatus::NonCompliant(reason) => {
stats.files += 1; stats.files += 1;
if !ctx.has_printed_folder {
println!("\n\n=== {} {}", get_status_icon(StatusIcon::Directory, no_emoji), ctx.current_folder);
ctx.has_printed_folder = true;
}
stats.warning += 1; stats.warning += 1;
println!("{} {} --- ({})", get_status_icon(StatusIcon::Warning, no_emoji), filename, reason); println!("{} {} --- ({})", get_status_icon(StatusIcon::Warning, no_emoji), filename, reason);
} }
@ -80,13 +98,14 @@ fn check_file(file: DirEntry, stats: &mut Stats, regex: &Regex, no_emoji: bool,
stats.files += 1; stats.files += 1;
stats.success += 1; stats.success += 1;
if show_success { if show_success {
println!("{} {}", get_status_icon(StatusIcon::Success, no_emoji), filename) println!("{} {}", get_status_icon(StatusIcon::Success, no_emoji), filename);
} }
} }
ComplianceStatus::Ignored => {} ComplianceStatus::Ignored => {}
} }
} else if file_type.is_dir() { } else if file_type.is_dir() {
println!("\n\n=== {} {}", get_status_icon(StatusIcon::Directory, no_emoji), file.path().display()) ctx.current_folder = file.path().display().to_string();
ctx.has_printed_folder = false;
} }
} }