fix highlighted in ignorecase

This commit is contained in:
mindfreq 2026-04-22 19:56:10 +02:00
parent 9041110788
commit 4a40ae0457

View file

@ -47,9 +47,10 @@ pub fn search(config: Config) -> Result<Vec<String>, &'static str> {
let query = config.query; 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(); 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<String> = Vec::new(); let mut str_result: Vec<String> = Vec::new();
@ -59,8 +60,12 @@ pub fn search(config: Config) -> Result<Vec<String>, &'static str> {
let line_lower = line.to_lowercase(); let line_lower = line.to_lowercase();
if line_lower.contains(&search_lower) { if line_lower.contains(&search_lower) {
let custom_line = line.replace(&query, &query.red()); let highlighted = line_lower
str_result.push(custom_line); .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 { } else {