From 6ea1ea4d6cd3e8d04f0b87f7364c73a30c4c6601 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Wed, 29 Nov 2023 01:11:42 +0100 Subject: [PATCH] Extract contains_word function --- src/main.rs | 1 + src/message_log.rs | 10 +++++----- src/util.rs | 7 +++++++ 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 src/util.rs diff --git a/src/main.rs b/src/main.rs index 3ed61ac..7d21a38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod irc_handler; mod irc_message; mod message_log; mod ui_message; +mod util; use color_eyre::eyre::Result; use iced::{Application, Settings}; diff --git a/src/message_log.rs b/src/message_log.rs index 707cfe7..112f4c7 100644 --- a/src/message_log.rs +++ b/src/message_log.rs @@ -1,4 +1,7 @@ -use crate::irc_message::{IrcMessage, MessageDetail}; +use crate::{ + irc_message::{IrcMessage, MessageDetail}, + util, +}; use chrono::{DateTime, Local}; use iced::{ @@ -7,7 +10,6 @@ use iced::{ Background, Color, Length, }; use irc::proto::BatchSubCommand; -use regex::Regex; use std::collections::{HashMap, HashSet}; pub struct Channel { @@ -321,9 +323,7 @@ impl<'a> MessageLog { } if !is_active { - let highlight_regex = - Regex::new(&format!(r"\b{}\b", regex::escape(current_nickname))).unwrap(); - if highlight_regex.is_match(message) { + if util::contains_word(current_nickname, message) { channel.unread_highlights += 1; } else { channel.unread_messages += 1; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..0c393bf --- /dev/null +++ b/src/util.rs @@ -0,0 +1,7 @@ +use regex::Regex; + +pub fn contains_word(needle: &str, haystack: &str) -> bool { + let pattern = format!(r"\b{}\b", regex::escape(needle)); + let regex = Regex::new(&pattern).unwrap(); + regex.is_match(haystack) +}