From 10a2d4bba1cc65254e621dd7a0e2785e323e96e0 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Tue, 20 Apr 2021 09:26:50 +0200 Subject: [PATCH] --disable option to turn bultin matchers off --- src/main.rs | 11 ++++++++++- src/state.rs | 14 ++++++++++++++ src/swapper.rs | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e2374c6..da88db7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,6 +103,13 @@ fn app_args<'a>() -> clap::ArgMatches<'a> { .default_value("left") .short("p"), ) + .arg( + Arg::with_name("disable") + .help("Disable a default pattern") + .long("disable") + .takes_value(true) + .multiple(true) + ) .arg( Arg::with_name("regexp") .help("Use this regexp as extra pattern to match") @@ -142,6 +149,8 @@ fn main() { } else { [].to_vec() }; + let disable = args.values_of("disable") + .map(|x| x.collect::>()).unwrap_or_default(); let foreground_color = colors::get_color(args.value_of("foreground_color").unwrap()); let background_color = colors::get_color(args.value_of("background_color").unwrap()); @@ -165,7 +174,7 @@ fn main() { } ).collect(); - let mut state = state::State::new(&lines, &unescaped, alphabet, ®exp); + let mut state = state::State::new_with_exclude(&lines, &unescaped, alphabet, ®exp, &disable); let selected = { let mut viewbox = view::View::new( diff --git a/src/state.rs b/src/state.rs index 02986fd..4cb8826 100644 --- a/src/state.rs +++ b/src/state.rs @@ -55,6 +55,7 @@ pub struct State<'a> { pub unescaped: &'a Vec, alphabet: &'a str, regexp: &'a Vec<&'a str>, + exclude_default: &'a [&'a str] } impl<'a> State<'a> { @@ -64,6 +65,17 @@ impl<'a> State<'a> { unescaped, alphabet, regexp, + exclude_default: &[] + } + } + + pub fn new_with_exclude(lines: &'a Vec<&'a str>, unescaped: &'a Vec, alphabet: &'a str, regexp: &'a Vec<&'a str>, exclude_default: &'a [&'a str]) -> State<'a> { + State { + lines, + unescaped, + alphabet, + regexp, + exclude_default, } } @@ -72,6 +84,7 @@ impl<'a> State<'a> { let exclude_patterns = EXCLUDE_PATTERNS .iter() + .filter(|(name, _)| !self.exclude_default.contains(name)) .map(|tuple| (tuple.0, Regex::new(tuple.1).unwrap())) .collect::>(); @@ -83,6 +96,7 @@ impl<'a> State<'a> { let patterns = PATTERNS .iter() + .filter(|(name, _)| !self.exclude_default.contains(name)) .map(|tuple| (tuple.0, Regex::new(tuple.1).unwrap())) .collect::>(); diff --git a/src/swapper.rs b/src/swapper.rs index 3368929..1f92b89 100644 --- a/src/swapper.rs +++ b/src/swapper.rs @@ -186,6 +186,10 @@ impl<'a> Swapper<'a> { return vec!["--regexp".to_string(), format!("'{}'", value.replace("\\\\", "\\"))]; } + if name.starts_with("disable") { + return vec!["--disable".to_owned(), value.to_owned()]; + } + vec![] } else { vec![]