From 1bcbb782afe1301fe4bcda825b3c7f1c28347590 Mon Sep 17 00:00:00 2001 From: Ben Sadeh <701096+bensadeh@users.noreply.github.com> Date: Sun, 14 Jan 2024 09:52:41 +0100 Subject: [PATCH] Clean up uuid highlighter --- src/highlight_utils.rs | 24 ---------- src/highlighters/uuid.rs | 98 +++++----------------------------------- 2 files changed, 12 insertions(+), 110 deletions(-) diff --git a/src/highlight_utils.rs b/src/highlight_utils.rs index 4eb3a37..27468f0 100644 --- a/src/highlight_utils.rs +++ b/src/highlight_utils.rs @@ -1,29 +1,5 @@ -use regex::{Captures, Regex}; - const MAX_ALLOCATION_SIZE: usize = 1024 * 1024; // 1 MiB -pub(crate) fn highlight_with_awareness(input: &str, regex: &Regex, highlight_fn: F) -> String -where - F: Fn(&Captures) -> String, -{ - let chunks = split_into_chunks(input); - let mut output = calculate_and_allocate_capacity(input); - - for chunk in chunks { - match chunk { - Chunk::NotHighlighted(text) => { - let highlighted = regex.replace_all(text, |caps: &Captures<'_>| highlight_fn(caps)); - output.push_str(&highlighted); - } - Chunk::AlreadyHighlighted(text) => { - output.push_str(text); - } - } - } - - output -} - pub(crate) fn apply_without_overwriting_existing_highlighting(input: &str, process_chunk: F) -> String where F: Fn(&str) -> String, diff --git a/src/highlighters/uuid.rs b/src/highlighters/uuid.rs index 1ba9b8c..528f9cf 100644 --- a/src/highlighters/uuid.rs +++ b/src/highlighters/uuid.rs @@ -1,4 +1,3 @@ -use crate::highlight_utils::highlight_with_awareness; use crate::line_info::LineInfo; use crate::types::Highlight; use nu_ansi_term::Style; @@ -43,91 +42,18 @@ impl Highlight for UuidHighlighter { } fn apply(&self, input: &str) -> String { - highlight_with_awareness(input, &UUID_REGEX, |caps: &Captures<'_>| { - let mut output = String::new(); - for i in 1..caps.len() { - if &caps[i] == "-" { - output.push_str(&format!("{}", self.separator.paint(&caps[i]))); - } else { - output.push_str(&format!("{}", self.segment.paint(&caps[i]))); + UUID_REGEX + .replace_all(input, |caps: &Captures<'_>| { + let mut output = String::new(); + for i in 1..caps.len() { + if &caps[i] == "-" { + output.push_str(&format!("{}", self.separator.paint(&caps[i]))); + } else { + output.push_str(&format!("{}", self.segment.paint(&caps[i]))); + } } - } - output - }) + output + }) + .to_string() } } - -// #[cfg(test)] -// mod tests { -// use super::*; -// use crate::color::Fg; -// use crate::theme::Style; -// -// #[test] -// fn test_highlight_uuids() { -// let uuid = "550e8400-e29b-41d4-a716-446655440000"; -// let segment = Style { -// fg: Fg::Red, -// ..Default::default() -// }; -// let separator = Style { -// fg: Fg::Green, -// ..Default::default() -// }; -// -// let highlighter = UuidHighlighter::new(&segment, &separator); -// let highlighted = highlighter.apply(uuid); -// -// let segment_color = "\x1b[31m"; // ANSI color code for red -// let separator_color = "\x1b[32m"; // ANSI color code for green -// let expected = format!( -// "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", -// segment_color, -// "550e8400", -// color::RESET, -// separator_color, -// "-", -// color::RESET, -// segment_color, -// "e29b", -// color::RESET, -// separator_color, -// "-", -// color::RESET, -// segment_color, -// "41d4", -// color::RESET, -// separator_color, -// "-", -// color::RESET, -// segment_color, -// "a716", -// color::RESET, -// separator_color, -// "-", -// color::RESET, -// segment_color, -// "446655440000", -// color::RESET -// ); -// assert_eq!(highlighted, expected); -// } -// -// #[test] -// fn test_highlight_uuids_no_uuid() { -// let text = "this is a test string with no uuid"; -// let segment = Style { -// fg: Fg::Red, -// ..Default::default() -// }; -// let separator = Style { -// fg: Fg::Green, -// ..Default::default() -// }; -// -// let highlighter = UuidHighlighter::new(&segment, &separator); -// let highlighted = highlighter.apply(text); -// -// assert_eq!(highlighted, text); -// } -// }