From b4c4a89625b50a3c723146435b4c253980e8476f Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 23 Feb 2025 22:03:42 +0100 Subject: [PATCH] Fix mention color of non-ascii nicks The old code included the @ in mention color computations. If the nick consisted only of weird unicode characters, this resulted in an incorrect color being computed. --- cove/src/euph/highlight.rs | 4 ++-- cove/src/euph/util.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cove/src/euph/highlight.rs b/cove/src/euph/highlight.rs index e69fd10..0e7fb56 100644 --- a/cove/src/euph/highlight.rs +++ b/cove/src/euph/highlight.rs @@ -138,8 +138,8 @@ pub fn apply_spans( let text = &content[range.start..range.end]; result = match span { - SpanType::Mention if exact => result.and_then(util::style_nick_exact(text, base)), - SpanType::Mention => result.and_then(util::style_nick(text, base)), + SpanType::Mention if exact => result.and_then(util::style_mention_exact(text, base)), + SpanType::Mention => result.and_then(util::style_mention(text, base)), SpanType::Room => result.then(text, base.blue().bold()), SpanType::Emoji if exact => result.then(text, base.magenta()), SpanType::Emoji => { diff --git a/cove/src/euph/util.rs b/cove/src/euph/util.rs index aff2192..c2928ab 100644 --- a/cove/src/euph/util.rs +++ b/cove/src/euph/util.rs @@ -55,3 +55,17 @@ pub fn style_nick(nick: &str, base: Style) -> Styled { pub fn style_nick_exact(nick: &str, base: Style) -> Styled { Styled::new(nick, nick_style(nick, base)) } + +pub fn style_mention(mention: &str, base: Style) -> Styled { + let nick = mention + .strip_prefix('@') + .expect("mention must start with @"); + Styled::new(EMOJI.replace(mention), nick_style(nick, base)) +} + +pub fn style_mention_exact(mention: &str, base: Style) -> Styled { + let nick = mention + .strip_prefix('@') + .expect("mention must start with @"); + Styled::new(mention, nick_style(nick, base)) +}