Extract contains_word function

This commit is contained in:
Sijmen 2023-11-29 01:11:42 +01:00
parent 5b94e67f0d
commit 6ea1ea4d6c
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
3 changed files with 13 additions and 5 deletions

View file

@ -4,6 +4,7 @@ mod irc_handler;
mod irc_message; mod irc_message;
mod message_log; mod message_log;
mod ui_message; mod ui_message;
mod util;
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use iced::{Application, Settings}; use iced::{Application, Settings};

View file

@ -1,4 +1,7 @@
use crate::irc_message::{IrcMessage, MessageDetail}; use crate::{
irc_message::{IrcMessage, MessageDetail},
util,
};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use iced::{ use iced::{
@ -7,7 +10,6 @@ use iced::{
Background, Color, Length, Background, Color, Length,
}; };
use irc::proto::BatchSubCommand; use irc::proto::BatchSubCommand;
use regex::Regex;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
pub struct Channel { pub struct Channel {
@ -321,9 +323,7 @@ impl<'a> MessageLog {
} }
if !is_active { if !is_active {
let highlight_regex = if util::contains_word(current_nickname, message) {
Regex::new(&format!(r"\b{}\b", regex::escape(current_nickname))).unwrap();
if highlight_regex.is_match(message) {
channel.unread_highlights += 1; channel.unread_highlights += 1;
} else { } else {
channel.unread_messages += 1; channel.unread_messages += 1;

7
src/util.rs Normal file
View file

@ -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)
}