From 84861d8574dc4ca2ea784e668873e68292d5a38a Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 9 Mar 2022 22:10:25 -0500 Subject: [PATCH] Add optional regex to uniq --- src/uniq.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/uniq.rs b/src/uniq.rs index ed2fa36..d9c7f98 100644 --- a/src/uniq.rs +++ b/src/uniq.rs @@ -1,12 +1,15 @@ use crate::{ get_selections_desc, set_selections, set_selections_desc, KakMessage, SelectionWithDesc, }; +use regex::Regex; use std::{ collections::{hash_map::DefaultHasher, BTreeSet}, hash::{Hash, Hasher}, }; #[derive(clap::StructOpt, Debug)] pub struct Options { + #[clap(index = 1)] + regex: Option, #[clap(short, long)] ignore_case: bool, // TODO: Can we invert a boolean? This name is terrible @@ -23,12 +26,24 @@ pub fn uniq(options: &Options) -> Result { .into_iter() // Create a BTreeSet of hashes of lines. This way, string content is not stored, but uniqueness can be determined .scan(BTreeSet::new(), |state, s| { - let key = if options.no_skip_whitespace { + // Strip whitespace if requested + let mut key = if options.no_skip_whitespace { s.content.as_str() } else { s.content.trim() }; + if let Some(regex_match) = (|| { + let captures = options.regex.as_ref()?.captures(key)?; + captures + .get(1) + .or_else(|| captures.get(0)) + .map(|m| m.as_str()) + })() { + key = regex_match; + } + + // Ignore case if requested let key = if options.ignore_case { key.to_lowercase() } else {