Add a search function

This commit is contained in:
mindfreq 2026-04-22 19:34:27 +02:00
parent be1efce6e8
commit 9041110788
5 changed files with 121 additions and 13 deletions

27
Cargo.lock generated
View file

@ -2,6 +2,33 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 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]] [[package]]
name = "m-grep" name = "m-grep"
version = "0.1.0" 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",
]

View file

@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
colored = "3"

9
poem.txt Normal file
View file

@ -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!

View file

@ -1,23 +1,75 @@
use std::fs::File;
use std::io::Read;
use std::path::Path;
use colored::Colorize;
#[derive(Debug)]
pub struct Config { pub struct Config {
search_text: String, query: String,
file_path: String, file_path: String,
ignor_case: bool, ignor_case: bool,
} }
impl Config { impl Config {
pub fn build(mut args: Vec<String>) -> () { pub fn build(mut args: Vec<String>) -> Result<Self, &'static str> {
args.remove(0); // Remove program path args.remove(0); // Remove program path
let ignor_case = Self::ignore_case(&mut args);
println!("{:?}", args);
// Self { 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<String>) { fn ignore_case(args: &mut Vec<String>) -> bool {
for (i, arg) in args.iter().enumerate() {
if arg.contains("-i") {
args.remove(i);
return true;
}
}
false
} }
} }
pub fn search(config: Config) -> Result<Vec<String>, &'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<String> = 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)
}

View file

@ -1,5 +1,24 @@
use std::process;
use colored::Colorize;
use m_grep::{Config, search};
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = 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<String>) -> Result<(), Box<dyn std::error::Error>> {
let config = Config::build(args)?;
let result = search(config)?;
for line in result {
println!("{line}");
}
Ok(())
} }