From fcb758a62d8925a1dda2feee52e88cf915eac7ac Mon Sep 17 00:00:00 2001 From: Cabbache Date: Wed, 23 Oct 2024 17:15:16 +0200 Subject: [PATCH] add --histogram flag definition --- crates/core/flags/defs.rs | 37 ++++++++++++++++++++++++++++++++++++ crates/core/flags/lowargs.rs | 1 + 2 files changed, 38 insertions(+) diff --git a/crates/core/flags/defs.rs b/crates/core/flags/defs.rs index 9a196c49..5476f6e1 100644 --- a/crates/core/flags/defs.rs +++ b/crates/core/flags/defs.rs @@ -59,6 +59,7 @@ pub(super) const FLAGS: &[&dyn Flag] = &[ &ContextSeparator, &Count, &CountMatches, + &Histogram, &Crlf, &Debug, &DfaSizeLimit, @@ -1322,6 +1323,42 @@ given. } } +/// --histogram +#[derive(Debug)] +struct Histogram; + +impl Flag for Histogram { + fn is_switch(&self) -> bool { + false + } + fn name_short(&self) -> Option { + None + } + fn name_long(&self) -> &'static str { + "histogram" + } + fn doc_variable(&self) -> Option<&'static str> { + Some("NUM") + } + fn doc_category(&self) -> Category { + Category::OutputModes + } + fn doc_short(&self) -> &'static str { + r"Print a histogram of the matches" + } + fn doc_long(&self) -> &'static str { + r" +--histogram NUM means that the bins of the histograms are NUM characters wide. In the output the numbers are the counts in these bins. + " + } + + fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> { + let binsize = convert::usize(&v.unwrap_value())?; + args.histogram = if binsize == 0 { None } else { Some(binsize) }; + Ok(()) + } +} + #[cfg(test)] #[test] fn test_count_matches() { diff --git a/crates/core/flags/lowargs.rs b/crates/core/flags/lowargs.rs index 184c96ae..7ca29793 100644 --- a/crates/core/flags/lowargs.rs +++ b/crates/core/flags/lowargs.rs @@ -59,6 +59,7 @@ pub(crate) struct LowArgs { pub(crate) globs: Vec, pub(crate) heading: Option, pub(crate) hidden: bool, + pub(crate) histogram: Option, pub(crate) hostname_bin: Option, pub(crate) hyperlink_format: HyperlinkFormat, pub(crate) iglobs: Vec,