grrs-bj 0.1.0

A tool to search files
Documentation
use clap::Parser;
use std::io::BufRead;
use anyhow::{Context, Result};
use std::io::{self};
use indicatif::ProgressBar;

#[derive(Parser)]
struct Cli {
    pattern: String,
    path: std::path::PathBuf
}

// dyn means dynamic dispatch.
// dyn Error : “some type that implements the Error trait.” and “I don’t care which concrete error it is, just treat it like an Error.”
// Box<dyn Error> : “Box is a heap pointer to some type that implements Error, but we don’t know its size or exact type at compile time.
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Cli::parse();
    let file = std::fs::File::open(&args.path)
        .with_context(|| format!("could not read file `{}`", &args.path.display()))?;
    let reader = std::io::BufReader::new(file);

    let stdout = io::stdout(); // get the global stdout entity
    let handle = stdout.lock(); // lock implement the Write trait
    let mut writer = io::BufWriter::new(handle); // BufWriter accept anything with Write trait
    let pb = ProgressBar::new(100);

    grrs_bj::find_matches(reader, &pb, &args.pattern, &mut writer)?;

    pb.finish_with_message("done");

    Ok(())
}