From 9041110788225ad18331501ac662b56628cd702a Mon Sep 17 00:00:00 2001 From: mindfreq <144544047+mindfreq@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:34:27 +0200 Subject: [PATCH] Add a search function --- Cargo.lock | 27 +++++++++++++++++++ Cargo.toml | 1 + poem.txt | 9 +++++++ src/lib.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++--------- src/main.rs | 21 ++++++++++++++- 5 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 poem.txt diff --git a/Cargo.lock b/Cargo.lock index 5a23684..5c5903f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,33 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "colored" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" +dependencies = [ + "windows-sys", +] + [[package]] name = "m-grep" version = "0.1.0" +dependencies = [ + "colored", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml index 1a1523f..ecaae7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +colored = "3" \ No newline at end of file diff --git a/poem.txt b/poem.txt new file mode 100644 index 0000000..3a260bf --- /dev/null +++ b/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 0a73d86..182ac57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,23 +1,75 @@ +use std::fs::File; +use std::io::Read; +use std::path::Path; +use colored::Colorize; +#[derive(Debug)] pub struct Config { - search_text: String, + query: String, file_path: String, ignor_case: bool, } impl Config { - pub fn build(mut args: Vec) -> () { + pub fn build(mut args: Vec) -> Result { args.remove(0); // Remove program path - - println!("{:?}", args); - // Self { - - // } - () + let ignor_case = Self::ignore_case(&mut args); + + if args.len() < 2 { + return Err("Argument not enough!"); + } + + let (query, file_path) = (args.remove(0), args.remove(0)); + Ok(Self { + query, + file_path, + ignor_case, + }) } - - fn is_ignore_case(args: Vec) { - + + fn ignore_case(args: &mut Vec) -> bool { + for (i, arg) in args.iter().enumerate() { + if arg.contains("-i") { + args.remove(i); + return true; + } + } + false } -} \ No newline at end of file +} + +pub fn search(config: Config) -> Result, &'static str> { + let file_path = config.file_path; + if !Path::new(&file_path).exists() { + return Err("File not exists!"); + } + + let query = config.query; + + let mut file = File::open(file_path).unwrap(); + let mut file_content = String::new(); + file.read_to_string(&mut file_content).unwrap(); + + let mut str_result: Vec = Vec::new(); + + if config.ignor_case { + for line in file_content.lines() { + let search_lower = query.to_lowercase(); + let line_lower = line.to_lowercase(); + + if line_lower.contains(&search_lower) { + let custom_line = line.replace(&query, &query.red()); + str_result.push(custom_line); + } + } + } else { + for line in file_content.lines() { + if line.contains(&query) { + let highlighted = line.replace(&query, &query.green().to_string()); + str_result.push(highlighted); + } + } + } + Ok(str_result) +} diff --git a/src/main.rs b/src/main.rs index 153c6c1..49df74f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,24 @@ +use std::process; + +use colored::Colorize; + +use m_grep::{Config, search}; fn main() { let args: Vec = std::env::args().collect(); - m_grep::Config::build(args); + + if let Err(err) = run(args) { + eprintln!("{}: {}", "Error".red().bold(), err.to_string().red()); + process::exit(1); + } +} + +fn run(args: Vec) -> Result<(), Box> { + let config = Config::build(args)?; + let result = search(config)?; + + for line in result { + println!("{line}"); + } + Ok(()) }