Use utils get_key

This commit is contained in:
Austen Adler 2022-07-07 00:28:25 -04:00
parent 9fd54770ab
commit b6114b607c

View File

@ -18,15 +18,14 @@ pub struct Options {
)] )]
args: Vec<String>, args: Vec<String>,
#[clap( #[clap(short, long, help = "Trim each selection before doing set operations")]
short = 'T', skip_whitespace: bool,
help = "Do not trim the selections before doing set operations"
)]
no_trim: bool,
// #[clap(short, long)] // #[clap(short, long)]
// regex: Option<Regex>, #[clap(skip)]
regex: Option<Regex>,
// #[clap(short, long)] // #[clap(short, long)]
// ignore_case: bool, #[clap(skip)]
ignore_case: bool,
// #[clap(short = 'S', long)] // #[clap(short = 'S', long)]
// no_skip_whitespace: bool, // no_skip_whitespace: bool,
} }
@ -160,7 +159,13 @@ fn reduce_selections(
// Does not matter if the operation was - or & // Does not matter if the operation was - or &
// Since key_set_operation_result contains elements that should be in the set, // Since key_set_operation_result contains elements that should be in the set,
// we can just use contains here // 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) { if key_set_operation_result.contains(&key) {
Some(swd.desc) Some(swd.desc)
} else { } else {
@ -264,34 +269,24 @@ fn to_ordered_counts(options: &Options, sels: Vec<Selection>) -> LinkedHashMap<S
let mut ret = LinkedHashMap::new(); let mut ret = LinkedHashMap::new();
for i in sels { for i in sels {
match into_key(options, i) { let key = crate::utils::get_key(
Some(key) => { &i,
let entry: &mut usize = ret.entry(key).or_insert(0); options.skip_whitespace,
*entry = entry.saturating_add(1); options.regex.as_ref(),
} options.ignore_case,
None => { );
// We don't want to even pretend to look at empty keys
} 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 ret
} }
fn into_key(options: &Options, sel: Selection) -> Option<Selection> {
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>( fn key_set_operation<'a>(
operation: &Operation, operation: &Operation,
left_keys: &LinkedHashSet<&'a Selection>, left_keys: &LinkedHashSet<&'a Selection>,