diff options
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index c89614b..e18a109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,10 @@ use std::str::FromStr; use anyhow::{anyhow, Error, Result}; use chrono::{NaiveDateTime, Weekday}; use colored::Colorize; +use itertools::Itertools; use log::debug; use num_traits::cast::FromPrimitive; +use regex::Regex; use rustyline::Editor; use structopt::StructOpt; use walkdir::{DirEntry, WalkDir}; @@ -242,9 +244,7 @@ fn run() -> Result<()> { } if !opt.repl { - let expr_string = opt.expression.join(" "); - let filter = build_filter(&expr_string)?; - grep(&opt, &*filter)?; + single(&opt)?; } else { repl(&opt)?; } @@ -256,6 +256,41 @@ fn run() -> Result<()> { Ok(()) } +fn single(opt: &Opt) -> Result<()> { + // As a shortcut, we allow only the regular expression to be given, to retain the behaviour + // before the filter changes. + if opt.expression.len() == 1 { + let line = &opt.expression[0]; + let maybe_filter = build_filter(line); + if let Err(_) = maybe_filter { + let maybe_regex = Regex::new(line); + if let Ok(rgx) = maybe_regex { + let filter = filters::player::any( + filters::player::NameFilter::new(SearchField::Account, rgx.clone()) + | filters::player::NameFilter::new(SearchField::Character, rgx), + ); + return grep(opt, &*filter); + } + } + return grep(opt, &*maybe_filter?); + } + + let expr_string = opt + .expression + .iter() + .map(|part| { + if part.contains(' ') { + format!(r#""{}""#, part) + } else { + part.into() + } + }) + .join(" "); + let filter = build_filter(&expr_string)?; + grep(&opt, &*filter)?; + Ok(()) +} + fn repl(opt: &Opt) -> Result<()> { let mut rl = Editor::<()>::new(); loop { |