use anyhow::Context;
use anyhow::Result;
use clap::Parser;
use clap_verbosity_flag::Verbosity;
use indicatif::ProgressBar;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
#[derive(Parser, Debug)]
pub struct Cli {
#[arg(short = 'p', long = "pattern")]
pub pattern: String,
#[arg(short = 'f', long = "file")]
pub file: std::path::PathBuf,
#[command(flatten)]
pub verbosity: Verbosity,
}
pub fn find_matches(pattern: &str, file: &PathBuf) -> Result<Vec<String>> {
let pb = ProgressBar::new(100);
let file = File::open(file).with_context(|| format!("Could not read file: {:?}", &file))?;
let mut reader = BufReader::new(file);
let mut line = String::new();
let mut results = Vec::new();
while let Ok(len) = reader.read_line(&mut line) {
if len == 0 {
break;
}
if line.contains(pattern) {
results.push(line.trim_end().to_string());
}
line.clear();
pb.println(format!("[+] finished reading line: {}", len));
pb.inc(1);
}
pb.finish_with_message("done");
Ok(results)
}