Skip to content

Commit

Permalink
Merge pull request #597 from squidowl/fix/highlight-user
Browse files Browse the repository at this point in the history
Only highlight if user nick isn't part of another word
  • Loading branch information
casperstorm authored Sep 27, 2024
2 parents 1b334aa + 225716d commit 9f90cb8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Added:
Fixed:

- Inverted scrolling direction.
- Only highlight if user nick isn't part of another word.

# 2024.12 (2024-09-17)

Expand Down
2 changes: 1 addition & 1 deletion data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ impl Client {
}

// Highlight notification
if message::reference_user_text(user.nickname(), self.nickname(), text)
if message::references_user_text(user.nickname(), self.nickname(), text)
&& self.highlight_blackout.allow_highlights()
{
return Some(vec![Event::Notification(
Expand Down
55 changes: 30 additions & 25 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,30 +383,37 @@ fn parse_url_fragments(text: String) -> Vec<Fragment> {
fragments
}

/// Checks if a given `text` contains or matches a user's nickname.
fn text_references_nickname(text: &str, nickname: NickRef) -> Option<bool> {
// TODO: Consider server case-mapping settings vs just ascii lowercase
let nick = nickname.as_ref();
let nick_lower = nick.to_ascii_lowercase();
let lower = text.to_ascii_lowercase();
let trimmed = text.trim_matches(|c: char| c.is_ascii_punctuation());
let lower_trimmed = trimmed.to_ascii_lowercase();

if nick == text || nick_lower == lower {
// Contains the user's nickname without trimming.
Some(false)
} else if nick == trimmed || nick_lower == lower_trimmed {
// Contains the user's nickname with trimming.
Some(true)
} else {
// Doesn't contain the user's nickname.
None
}
}

fn parse_user_and_channel_fragments(text: &str, channel_users: &[User]) -> Vec<Fragment> {
text.chars()
.group_by(|c| c.is_whitespace())
.into_iter()
.flat_map(|(is_whitespace, chars)| {
let text = chars.collect::<String>();
let trimmed = text.trim_matches(|c: char| c.is_ascii_punctuation());
let lower = text.to_ascii_lowercase();
let lower_trimmed = trimmed.to_ascii_lowercase();

if !is_whitespace {
if let Some((is_trimmed, user)) = channel_users.iter().find_map(|user| {
let nickname = user.nickname();
let nick = nickname.as_ref();
// TODO: Consider server case-mapping settings vs just ascii lowercase
let nick_lower = nick.to_ascii_lowercase();

if nick == text || nick_lower == lower {
Some((false, user.clone()))
} else if nick == trimmed || nick_lower == lower_trimmed {
Some((true, user.clone()))
} else {
None
}
text_references_nickname(text.as_str(), user.nickname())
.map(|is_trimmed| (is_trimmed, user.clone()))
}) {
if is_trimmed {
let prefix_end = text.find(|c: char| !c.is_ascii_punctuation());
Expand Down Expand Up @@ -1060,19 +1067,17 @@ fn monitored_targets_text(targets: Vec<String>) -> Option<String> {
}
}

pub fn reference_user(sender: NickRef, own_nick: NickRef, message: &Message) -> bool {
let has_nick = match &message.content {
Content::Plain(text) => text.contains(own_nick.as_ref()),
pub fn references_user(sender: NickRef, own_nick: NickRef, message: &Message) -> bool {
match &message.content {
Content::Plain(text) => references_user_text(sender, own_nick, text),
Content::Fragments(fragments) => fragments
.iter()
.any(|f| f.as_str().contains(own_nick.as_ref())),
};

sender != own_nick && has_nick
.any(|f| references_user_text(sender, own_nick, f.as_str())),
}
}

pub fn reference_user_text(sender: NickRef, own_nick: NickRef, text: &str) -> bool {
sender != own_nick && text.contains(own_nick.as_ref())
pub fn references_user_text(sender: NickRef, own_nick: NickRef, text: &str) -> bool {
sender != own_nick && text_references_nickname(text, own_nick).is_some()
}

#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn view<'a>(
))
.push(container(text).style(move |theme| match our_nick {
Some(nick)
if message::reference_user(
if message::references_user(
user.nickname(),
nick,
message,
Expand Down

0 comments on commit 9f90cb8

Please sign in to comment.