From 28a6a5ea57c0a696538bb6de13a0ef27f1b5dbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Mon, 1 Mar 2021 19:48:22 +0200 Subject: [PATCH] Add option to invert match command selection (#3114) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add option to invert match command selection * Fix rustfmt error * Rename match --exclude to --invert To be more descriptive and conform to e.g. grep or ripgrep -v flag. Also simplified the --invert flag description. * Fix formatting when description got shorter Co-authored-by: Jakub Žádník --- crates/nu_plugin_match/src/match_.rs | 2 ++ crates/nu_plugin_match/src/nu/mod.rs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/nu_plugin_match/src/match_.rs b/crates/nu_plugin_match/src/match_.rs index 896e2188a..b90c5e2dc 100644 --- a/crates/nu_plugin_match/src/match_.rs +++ b/crates/nu_plugin_match/src/match_.rs @@ -3,6 +3,7 @@ use regex::Regex; pub struct Match { pub column: String, pub regex: Regex, + pub invert: bool, } impl Match { @@ -11,6 +12,7 @@ impl Match { Ok(Match { column: String::new(), regex: Regex::new("")?, + invert: false, }) } } diff --git a/crates/nu_plugin_match/src/nu/mod.rs b/crates/nu_plugin_match/src/nu/mod.rs index 10b0a5b46..887044f10 100644 --- a/crates/nu_plugin_match/src/nu/mod.rs +++ b/crates/nu_plugin_match/src/nu/mod.rs @@ -24,6 +24,7 @@ impl Plugin for Match { "dotall mode: allow a dot . to match newline character \\n", Some('s'), ) + .switch("invert", "invert the match", Some('v')) .filter()) } @@ -31,6 +32,7 @@ impl Plugin for Match { let insensitive = call_info.args.has("insensitive"); let multiline = call_info.args.has("multiline"); let dotall = call_info.args.has("dotall"); + self.invert = call_info.args.has("invert"); if let Some(args) = call_info.args.positional { match &args[0] { Value { @@ -113,7 +115,7 @@ impl Plugin for Match { return Err(ShellError::labeled_error("Expected row", "value", tag)); } } - if flag { + if flag ^ self.invert { Ok(vec![Ok(ReturnSuccess::Value(input))]) } else { Ok(vec![])