From 18e6865b68e12a8293d09033d587d7c57f3aeda2 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Mon, 16 Jun 2025 10:36:27 -0700 Subject: [PATCH] feat: add regex option for username_display --- src/config.rs | 21 +++++++++++++++++++++ src/tests.rs | 1 + 2 files changed, 22 insertions(+) diff --git a/src/config.rs b/src/config.rs index b712c73..26da1e3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -397,6 +397,9 @@ pub enum UserDisplayStyle { // it can wind up being the Matrix username if there are display name collisions in the room, // in order to avoid any confusion. DisplayName, + + // Acts like Username, except when the username matches given regex, then acts like DisplayName + Regex, } #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -566,6 +569,7 @@ pub struct TunableValues { pub typing_notice_display: bool, pub users: UserOverrides, pub username_display: UserDisplayStyle, + pub username_display_regex: Option, pub message_user_color: bool, pub default_room: Option, pub open_command: Option>, @@ -592,6 +596,7 @@ pub struct Tunables { pub typing_notice_display: Option, pub users: Option, pub username_display: Option, + pub username_display_regex: Option, pub message_user_color: Option, pub default_room: Option, pub open_command: Option>, @@ -622,6 +627,7 @@ impl Tunables { typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), users: merge_maps(self.users, other.users), username_display: self.username_display.or(other.username_display), + username_display_regex: self.username_display_regex.or(other.username_display_regex), message_user_color: self.message_user_color.or(other.message_user_color), default_room: self.default_room.or(other.default_room), open_command: self.open_command.or(other.open_command), @@ -650,6 +656,7 @@ impl Tunables { typing_notice_display: self.typing_notice_display.unwrap_or(true), users: self.users.unwrap_or_default(), username_display: self.username_display.unwrap_or_default(), + username_display_regex: self.username_display_regex, message_user_color: self.message_user_color.unwrap_or(false), default_room: self.default_room, open_command: self.open_command, @@ -1042,6 +1049,20 @@ impl ApplicationSettings { Cow::Borrowed(user_id.as_str()) } }, + (None, UserDisplayStyle::Regex) => { + let re = regex::Regex::new( + &self.tunables.username_display_regex.clone().unwrap_or("*".into()), + ) + .unwrap(); + + if !re.is_match(user_id.as_str()) { + Cow::Borrowed(user_id.as_str()) + } else if let Some(display) = info.display_names.get(user_id) { + Cow::Borrowed(display.as_str()) + } else { + Cow::Borrowed(user_id.as_str()) + } + }, }; Span::styled(name, style) diff --git a/src/tests.rs b/src/tests.rs index 52cb859..ef6f82c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -189,6 +189,7 @@ pub fn mock_tunables() -> TunableValues { open_command: None, external_edit_file_suffix: String::from(".md"), username_display: UserDisplayStyle::Username, + username_display_regex: Some(String::from(".*")), message_user_color: false, mouse: Default::default(), notifications: Notifications {