Add option to invert match command selection (#3114)

* 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 <jakub.zadnik@tuni.fi>
This commit is contained in:
Jakub Žádník 2021-03-01 19:48:22 +02:00 committed by GitHub
parent f83ff0e47d
commit 28a6a5ea57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View File

@ -3,6 +3,7 @@ use regex::Regex;
pub struct Match { pub struct Match {
pub column: String, pub column: String,
pub regex: Regex, pub regex: Regex,
pub invert: bool,
} }
impl Match { impl Match {
@ -11,6 +12,7 @@ impl Match {
Ok(Match { Ok(Match {
column: String::new(), column: String::new(),
regex: Regex::new("")?, regex: Regex::new("")?,
invert: false,
}) })
} }
} }

View File

@ -24,6 +24,7 @@ impl Plugin for Match {
"dotall mode: allow a dot . to match newline character \\n", "dotall mode: allow a dot . to match newline character \\n",
Some('s'), Some('s'),
) )
.switch("invert", "invert the match", Some('v'))
.filter()) .filter())
} }
@ -31,6 +32,7 @@ impl Plugin for Match {
let insensitive = call_info.args.has("insensitive"); let insensitive = call_info.args.has("insensitive");
let multiline = call_info.args.has("multiline"); let multiline = call_info.args.has("multiline");
let dotall = call_info.args.has("dotall"); let dotall = call_info.args.has("dotall");
self.invert = call_info.args.has("invert");
if let Some(args) = call_info.args.positional { if let Some(args) = call_info.args.positional {
match &args[0] { match &args[0] {
Value { Value {
@ -113,7 +115,7 @@ impl Plugin for Match {
return Err(ShellError::labeled_error("Expected row", "value", tag)); return Err(ShellError::labeled_error("Expected row", "value", tag));
} }
} }
if flag { if flag ^ self.invert {
Ok(vec![Ok(ReturnSuccess::Value(input))]) Ok(vec![Ok(ReturnSuccess::Value(input))])
} else { } else {
Ok(vec![]) Ok(vec![])