feat: add case insensitive mode
This commit is contained in:
parent
ad6dc24d7e
commit
8452eae0f4
2 changed files with 17 additions and 5 deletions
|
|
@ -12,6 +12,9 @@ struct Cli {
|
||||||
|
|
||||||
#[arg(value_name = "FILES", num_args = 1..)]
|
#[arg(value_name = "FILES", num_args = 1..)]
|
||||||
file_paths: Vec<PathBuf>,
|
file_paths: Vec<PathBuf>,
|
||||||
|
|
||||||
|
#[arg(short, long)]
|
||||||
|
ignore_case: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
|
|
@ -33,7 +36,7 @@ fn run() -> anyhow::Result<Matches> {
|
||||||
|
|
||||||
// println!("{:?} {}", cli.file_paths, cli.pattern);
|
// 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)
|
Ok(matches)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,23 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Context;
|
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(
|
pub fn process_files(
|
||||||
file_paths: &Vec<PathBuf>,
|
file_paths: &Vec<PathBuf>,
|
||||||
pattern: &str,
|
pattern: &str,
|
||||||
|
cli: &Cli,
|
||||||
) -> anyhow::Result<Matches, anyhow::Error> {
|
) -> anyhow::Result<Matches, anyhow::Error> {
|
||||||
let mut matches = Matches { items: vec![], pattern: String::from(pattern) };
|
let mut matches = Matches { items: vec![], pattern: String::from(pattern) };
|
||||||
|
|
||||||
for file_path in file_paths {
|
for file_path in file_paths {
|
||||||
let lines_iter = read_lines(file_path)?;
|
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);
|
matches.append(&mut _matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,8 +60,13 @@ pub fn match_pattern(
|
||||||
iter: impl Iterator<Item = std::io::Result<String>>,
|
iter: impl Iterator<Item = std::io::Result<String>>,
|
||||||
pattern: &str,
|
pattern: &str,
|
||||||
file_path: &Path,
|
file_path: &Path,
|
||||||
|
cli: &Cli,
|
||||||
) -> anyhow::Result<Matches, anyhow::Error> {
|
) -> anyhow::Result<Matches, anyhow::Error> {
|
||||||
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();
|
let mut matches = Vec::new();
|
||||||
|
|
||||||
for (idx, line) in iter.enumerate() {
|
for (idx, line) in iter.enumerate() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue