feat: add regex option for username_display

This commit is contained in:
Youwen Wu 2025-06-16 10:36:27 -07:00
parent 33d3407694
commit 7f752da89d
Signed by: youwen
GPG key ID: 865658ED1FE61EC3
2 changed files with 22 additions and 0 deletions

View file

@ -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<String>,
pub message_user_color: bool,
pub default_room: Option<String>,
pub open_command: Option<Vec<String>>,
@ -592,6 +596,7 @@ pub struct Tunables {
pub typing_notice_display: Option<bool>,
pub users: Option<UserOverrides>,
pub username_display: Option<UserDisplayStyle>,
pub username_display_regex: Option<String>,
pub message_user_color: Option<bool>,
pub default_room: Option<String>,
pub open_command: Option<Vec<String>>,
@ -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)