Add a search function
This commit is contained in:
parent
be1efce6e8
commit
9041110788
5 changed files with 121 additions and 13 deletions
27
Cargo.lock
generated
27
Cargo.lock
generated
|
|
@ -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",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
colored = "3"
|
||||
9
poem.txt
Normal file
9
poem.txt
Normal 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!
|
||||
68
src/lib.rs
68
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<String>) -> () {
|
||||
pub fn build(mut args: Vec<String>) -> Result<Self, &'static str> {
|
||||
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!");
|
||||
}
|
||||
|
||||
fn is_ignore_case(args: Vec<String>) {
|
||||
let (query, file_path) = (args.remove(0), args.remove(0));
|
||||
Ok(Self {
|
||||
query,
|
||||
file_path,
|
||||
ignor_case,
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
21
src/main.rs
21
src/main.rs
|
|
@ -1,5 +1,24 @@
|
|||
use std::process;
|
||||
|
||||
use colored::Colorize;
|
||||
|
||||
use m_grep::{Config, search};
|
||||
|
||||
fn main() {
|
||||
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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue