From 4a40ae04579d074a46d00dd9ed4f50bc679edf21 Mon Sep 17 00:00:00 2001 From: mindfreq <144544047+mindfreq@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:56:10 +0200 Subject: [PATCH] fix highlighted in ignorecase --- src/lib.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 182ac57..620f54a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,9 +47,10 @@ pub fn search(config: Config) -> Result, &'static str> { let query = config.query; - let mut file = File::open(file_path).unwrap(); + let mut file = File::open(file_path).map_err(|_| "Faild to open file")?; let mut file_content = String::new(); - file.read_to_string(&mut file_content).unwrap(); + file.read_to_string(&mut file_content) + .map_err(|_| "Faild to read file")?; let mut str_result: Vec = Vec::new(); @@ -59,8 +60,12 @@ pub fn search(config: Config) -> Result, &'static str> { 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); + let highlighted = line_lower + .find(&search_lower) + .map(|i| line[..i].to_string() + &line[i..i+query.len()].green().to_string() + &line[i+query.len()..]) + .unwrap_or(line.to_string()); + + str_result.push(highlighted); } } } else {