From 16011a267dc025f2bc275fb5dde7ef86078405d1 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 20 Jan 2023 20:19:03 +0100 Subject: [PATCH] Display colon-delimited emoji in nicks --- CHANGELOG.md | 1 + src/euph/small_message.rs | 4 ++-- src/euph/util.rs | 15 ++++++++++++--- src/ui/euph/nick.rs | 4 ++-- src/ui/euph/nick_list.rs | 9 ++++++--- src/ui/euph/room.rs | 4 ++-- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fae8b58..c1be0e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Procedure when bumping the version number: ### Changed - Respect colon-delimited emoji when calculating nick hue +- Display colon-delimited emoji in nicks properly ## v0.5.2 - 2023-01-14 diff --git a/src/euph/small_message.rs b/src/euph/small_message.rs index f2e8901..4784c60 100644 --- a/src/euph/small_message.rs +++ b/src/euph/small_message.rs @@ -107,13 +107,13 @@ fn style_me() -> ContentStyle { fn styled_nick(nick: &str) -> Styled { Styled::new_plain("[") - .then(nick, util::nick_style(nick)) + .and_then(util::style_nick(nick, ContentStyle::default())) .then_plain("]") } fn styled_nick_me(nick: &str) -> Styled { let style = style_me(); - Styled::new("*", style).then(nick, util::nick_style(nick).italic()) + Styled::new("*", style).and_then(util::style_nick(nick, style)) } fn styled_content(content: &str) -> Styled { diff --git a/src/euph/util.rs b/src/euph/util.rs index 3285a07..cd2a443 100644 --- a/src/euph/util.rs +++ b/src/euph/util.rs @@ -1,8 +1,9 @@ use crossterm::style::{Color, ContentStyle, Stylize}; use euphoxide::emoji::Emoji; use once_cell::sync::Lazy; +use toss::styled::Styled; -static EMOJI: Lazy = Lazy::new(Emoji::load); +pub static EMOJI: Lazy = Lazy::new(Emoji::load); /// Convert HSL to RGB following [this approach from wikipedia][1]. /// @@ -41,7 +42,15 @@ pub fn nick_color(nick: &str) -> (u8, u8, u8) { hsl_to_rgb(hue, 1.0, 0.72) } -pub fn nick_style(nick: &str) -> ContentStyle { +pub fn nick_style(nick: &str, base: ContentStyle) -> ContentStyle { let (r, g, b) = nick_color(nick); - ContentStyle::default().bold().with(Color::Rgb { r, g, b }) + base.bold().with(Color::Rgb { r, g, b }) +} + +pub fn style_nick(nick: &str, base: ContentStyle) -> Styled { + Styled::new(EMOJI.replace(nick), nick_style(nick, base)) +} + +pub fn style_nick_exact(nick: &str, base: ContentStyle) -> Styled { + Styled::new(nick, nick_style(nick, base)) } diff --git a/src/ui/euph/nick.rs b/src/ui/euph/nick.rs index 9237c57..e520fef 100644 --- a/src/ui/euph/nick.rs +++ b/src/ui/euph/nick.rs @@ -1,5 +1,5 @@ +use crossterm::style::ContentStyle; use euphoxide::conn::Joined; -use toss::styled::Styled; use toss::terminal::Terminal; use crate::euph::{self, Room}; @@ -17,7 +17,7 @@ pub fn new(joined: Joined) -> EditorState { pub fn widget(editor: &EditorState) -> BoxedWidget { let editor = editor .widget() - .highlight(|s| Styled::new(s, euph::nick_style(s))); + .highlight(|s| euph::style_nick_exact(s, ContentStyle::default())); Popup::new(Padding::new(editor).left(1)) .title("Choose nick") .inner_padding(false) diff --git a/src/ui/euph/nick_list.rs b/src/ui/euph/nick_list.rs index 431edb3..926ca68 100644 --- a/src/ui/euph/nick_list.rs +++ b/src/ui/euph/nick_list.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::iter; use crossterm::style::{Color, ContentStyle, Stylize}; @@ -118,7 +119,7 @@ fn render_row(list: &mut List, session: &HalfSession, own_session: &S let name = "lurk"; let style = ContentStyle::default().grey(); let style_inv = ContentStyle::default().black().on_grey(); - (name, style, style_inv, style_inv) + (Cow::Borrowed(name), style, style_inv, style_inv) } else { let name = &session.name as &str; let (r, g, b) = euph::nick_color(name); @@ -126,7 +127,7 @@ fn render_row(list: &mut List, session: &HalfSession, own_session: &S let style = ContentStyle::default().bold().with(color); let style_inv = ContentStyle::default().bold().black().on(color); let perms_style_inv = ContentStyle::default().black().on(color); - (name, style, style_inv, perms_style_inv) + (euph::EMOJI.replace(name), style, style_inv, perms_style_inv) }; let perms = if session.is_staff { @@ -145,7 +146,9 @@ fn render_row(list: &mut List, session: &HalfSession, own_session: &S " " }; - let normal = Styled::new_plain(owner).then(name, style).then_plain(perms); + let normal = Styled::new_plain(owner) + .then(&name, style) + .then_plain(perms); let selected = Styled::new_plain(owner) .then(name, style_inv) .then(perms, perms_style_inv); diff --git a/src/ui/euph/room.rs b/src/ui/euph/room.rs index 882fcd1..8fefda0 100644 --- a/src/ui/euph/room.rs +++ b/src/ui/euph/room.rs @@ -309,8 +309,8 @@ impl EuphRoom { if nick.is_empty() { info.then_plain(", present without nick") } else { - let nick_style = euph::nick_style(nick); - info.then_plain(", present as ").then(nick, nick_style) + info.then_plain(", present as ") + .and_then(euph::style_nick(nick, ContentStyle::default())) } } };