aegrep is a small Rust command-line tool inspired by grep and the Rust Book's
minigrep project. It searches for a pattern in one or more files and prints matching
lines with colored, nicely formatted output.
This project is mainly a learning playground for:
- Error handling with custom enums and
Result - Module organization with
main.rs,lib.rs, and multiple submodules - Basic CLI argument parsing
- Simple text searching and line highlighting
- Search for a pattern across one or more files
- Show matches as
file:line:col line_content - Highlight the matched pattern using terminal colors (via
owo-colors) - Custom error type (
MyErrors) for argument and I/O errors - Config struct with a simple builder-style method (
with_quiet)
From the project root:
cargo run -- <pattern> <file> [more_files...]Examples:
# Search for "fox" in poem.txt
cargo run -- fox poem.txt
# Search in multiple files
cargo run -- error src/main.rs src/lib.rsIf required arguments are missing, the program exits with a descriptive panic message.
src/
main.rs # entry point, calls CLI + search
lib.rs # library: search logic, single_search, etc.
cli.rs # parse_args(): builds Config, returns Result<Config, MyErrors>
types.rs # MyErrors enum + Config struct
pretty.rs # print_found(): colored, formatted output
main.rs- Calls
cli::parse_args()to get aConfig - On success, calls
aegrep::search(...)
- Calls
lib.rs- Exposes
search(...)and wires togethersingle_search,pretty, and errors
- Exposes
types.rsMyErrors:FileReadError(String),MissingArgsError, etc.Config:{ pattern, files, quiet }withConfig::newandwith_quiet
pretty.rs- Uses
owo-colorsto highlight the pattern and format the output line
- Uses
# Build
cargo build
# Run with arguments
cargo run -- pattern file1 [file2 ...]Run with --release for an optimized binary:
cargo build --release- Add a
--quietor-qflag that hooks intoConfig::with_quiet - Add case-insensitive search (e.g.
--ignore-case) - Add
--line-number/--columntoggles - Add tests for
parse_args,search, and matching logic
This project is primarily for learning Rust patterns around CLI tools, error handling, and module organization.