From b6114b607caffc404f049e83c5224bdde8772ddc Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Thu, 7 Jul 2022 00:28:25 -0400 Subject: [PATCH] Use utils get_key --- src/set.rs | 57 +++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/set.rs b/src/set.rs index 6a74de4..67169e2 100644 --- a/src/set.rs +++ b/src/set.rs @@ -18,15 +18,14 @@ pub struct Options { )] args: Vec, - #[clap( - short = 'T', - help = "Do not trim the selections before doing set operations" - )] - no_trim: bool, + #[clap(short, long, help = "Trim each selection before doing set operations")] + skip_whitespace: bool, // #[clap(short, long)] - // regex: Option, + #[clap(skip)] + regex: Option, // #[clap(short, long)] - // ignore_case: bool, + #[clap(skip)] + ignore_case: bool, // #[clap(short = 'S', long)] // no_skip_whitespace: bool, } @@ -160,7 +159,13 @@ fn reduce_selections( // Does not matter if the operation was - or & // Since key_set_operation_result contains elements that should be in the set, // we can just use contains here - let key = into_key(options, swd.content)?; + let key = crate::utils::get_key( + &swd.content, + options.skip_whitespace, + options.regex.as_ref(), + options.ignore_case, + ); + if key_set_operation_result.contains(&key) { Some(swd.desc) } else { @@ -264,34 +269,24 @@ fn to_ordered_counts(options: &Options, sels: Vec) -> LinkedHashMap { - let entry: &mut usize = ret.entry(key).or_insert(0); - *entry = entry.saturating_add(1); - } - None => { - // We don't want to even pretend to look at empty keys - } + let key = crate::utils::get_key( + &i, + options.skip_whitespace, + options.regex.as_ref(), + options.ignore_case, + ); + + if key.is_empty() { + // We don't want to even pretend to look at empty keys + continue; + } else { + let entry: &mut usize = ret.entry(key).or_insert(0); + *entry = entry.saturating_add(1); } } ret } -fn into_key(options: &Options, sel: Selection) -> Option { - let key = if options.no_trim { - sel - } else { - sel.trim().to_string() - }; - - if key.is_empty() { - // Never treat an empty string as a key - None - } else { - Some(key) - } -} - fn key_set_operation<'a>( operation: &Operation, left_keys: &LinkedHashSet<&'a Selection>,