use anyhow::Result; use colored::*; use regex::Regex; use walkdir::WalkDir; pub fn ejecutar(patron: &str, ignorar_mayusculas: bool, archivo: &str) -> Result<()> { let re = if ignorar_mayusculas { Regex::new(&format!("(?i){}", patron))? } else { Regex::new(patron)? }; let path = std::path::Path::new(archivo); if path.is_dir() { for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) { if entry.file_type().is_file() { buscar_en_archivo(&entry.path().to_string_lossy(), &re)?; } } } else { buscar_en_archivo(archivo, &re)?; } Ok(()) } fn buscar_en_archivo(ruta: &str, re: &Regex) -> Result<()> { let contenido = match std::fs::read_to_string(ruta) { Ok(c) => c, Err(_) => return Ok(()), }; for (num, linea) in contenido.lines().enumerate() { if re.is_match(linea) { println!("{}:{}: {}", ruta.cyan().bold(), (num + 1).to_string().yellow(), linea); } } Ok(()) }