From 73028972e05a9b6eef1703aaa0dd9094720ff4dc Mon Sep 17 00:00:00 2001 From: DastInDark <2350416+hitenkoku@users.noreply.github.com> Date: Thu, 11 Aug 2022 20:10:27 +0900 Subject: [PATCH] added output percentage of detections in result summary #658 --- src/afterfact.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/afterfact.rs b/src/afterfact.rs index 7be9dec8..f16c35b7 100644 --- a/src/afterfact.rs +++ b/src/afterfact.rs @@ -437,6 +437,10 @@ fn _print_unique_results( // the order in which are registered and the order of levels to be displayed are reversed counts_by_level.reverse(); + + let total_count = counts_by_level + .iter() + .sum::(); // output total results write_color_buffer( &BufferWriter::stdout(ColorChoice::Always), @@ -445,10 +449,7 @@ fn _print_unique_results( "{} {}: {}", head_word, tail_word, - counts_by_level - .iter() - .sum::() - .to_formatted_string(&Locale::en), + total_count.to_formatted_string(&Locale::en), ), true, ) @@ -458,12 +459,18 @@ fn _print_unique_results( if "undefined" == *level_name { continue; } + let percent = if total_count == 0 { + 0 as f64 + } else { + (counts_by_level[i] as f64) / (total_count as f64) * 100.0 + }; let output_raw_str = format!( - "{} {} {}: {}", + "{} {} {}: {} ({:.2}%)", head_word, level_name, tail_word, - counts_by_level[i].to_formatted_string(&Locale::en) + counts_by_level[i].to_formatted_string(&Locale::en), + percent ); write_color_buffer( &BufferWriter::stdout(ColorChoice::Always),