mirror of
https://github.com/nushell/nushell
synced 2024-11-10 07:04:13 +00:00
add regex match plugin
This commit is contained in:
parent
91e6d31dc6
commit
9fb9adb6b4
3 changed files with 106 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1561,6 +1561,7 @@ dependencies = [
|
|||
"prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ptree 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rawkey 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"roxmltree 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rusqlite 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustyline 5.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -75,6 +75,7 @@ bigdecimal = { version = "0.1.0", features = ["serde"] }
|
|||
natural = "0.3.0"
|
||||
serde_urlencoded = "0.6.1"
|
||||
sublime_fuzzy = "0.5"
|
||||
regex = "1"
|
||||
|
||||
neso = { version = "0.5.0", optional = true }
|
||||
crossterm = { version = "0.10.2", optional = true }
|
||||
|
@ -134,6 +135,10 @@ path = "src/plugins/str.rs"
|
|||
name = "nu_plugin_skip"
|
||||
path = "src/plugins/skip.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "nu_plugin_match"
|
||||
path = "src/plugins/match.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "nu_plugin_sys"
|
||||
path = "src/plugins/sys.rs"
|
||||
|
|
100
src/plugins/match.rs
Normal file
100
src/plugins/match.rs
Normal file
|
@ -0,0 +1,100 @@
|
|||
use nu::{
|
||||
serve_plugin, CallInfo, CoerceInto, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError,
|
||||
Signature, SyntaxShape, Tagged, TaggedItem, Value, EvaluatedArgs,
|
||||
};
|
||||
use indexmap::IndexMap;
|
||||
use regex::Regex;
|
||||
|
||||
struct Match {
|
||||
column: String,
|
||||
regex: Regex,
|
||||
}
|
||||
|
||||
impl Match {
|
||||
fn new() -> Self {
|
||||
Match {
|
||||
column: String::new(),
|
||||
regex: Regex::new("").unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Match {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("match")
|
||||
.desc("filter rows by regex")
|
||||
.required("member", SyntaxShape::Member)
|
||||
.required("regex", SyntaxShape::String)
|
||||
.filter())
|
||||
}
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
match &args[0] {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.column = s.clone();
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"Unrecognized type in params: {:?}", args[0])));
|
||||
}
|
||||
}
|
||||
match &args[1] {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.regex = Regex::new(s).unwrap();
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"Unrecognized type in params: {:?}", args[0])));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let flag: bool;
|
||||
match &input {
|
||||
Tagged {
|
||||
item: Value::Row(dict),
|
||||
..
|
||||
} => {
|
||||
if let Some(val) = dict.entries.get(&self.column) {
|
||||
match val {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
flag = self.regex.is_match(s);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"value is not a string! {:?}", &val)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::string(format!(
|
||||
"column not in row! {:?} {:?}", &self.column, dict)));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"Not a row! {:?}", &input)));
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
Ok(vec![Ok(ReturnSuccess::Value(input))])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Match::new());
|
||||
}
|
Loading…
Reference in a new issue