diff --git a/src/main.rs b/src/main.rs index f9d15a9..2efcb61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,9 @@ struct Cli { #[arg(value_name = "FILES", num_args = 1..)] file_paths: Vec, + + #[arg(short, long)] + ignore_case: bool, } fn main() -> ExitCode { @@ -33,7 +36,7 @@ fn run() -> anyhow::Result { // println!("{:?} {}", cli.file_paths, cli.pattern); - let matches = process_files(&cli.file_paths, &cli.pattern)?; + let matches = process_files(&cli.file_paths, &cli.pattern, &cli)?; Ok(matches) } diff --git a/src/parser.rs b/src/parser.rs index 6bbd9b3..cab8874 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,19 +5,23 @@ use std::{ }; use anyhow::Context; -use regex::Regex; +use regex::{Regex, RegexBuilder}; -use crate::matches::{Match, Matches}; +use crate::{ + matches::{Match, Matches}, + Cli, +}; pub fn process_files( file_paths: &Vec, pattern: &str, + cli: &Cli, ) -> anyhow::Result { let mut matches = Matches { items: vec![], pattern: String::from(pattern) }; for file_path in file_paths { let lines_iter = read_lines(file_path)?; - let mut _matches = match_pattern(lines_iter, pattern, file_path)?; + let mut _matches = match_pattern(lines_iter, pattern, file_path, cli)?; matches.append(&mut _matches); } @@ -56,8 +60,13 @@ pub fn match_pattern( iter: impl Iterator>, pattern: &str, file_path: &Path, + cli: &Cli, ) -> anyhow::Result { - let reg = Regex::new(pattern).with_context(|| format!("invalid regex: {:?}", pattern))?; + let reg = RegexBuilder::new(pattern) + .case_insensitive(cli.ignore_case) + .build() + .with_context(|| format!("invalid regex: {:?}", pattern))?; + let mut matches = Vec::new(); for (idx, line) in iter.enumerate() {