Compare commits

...

2 commits

2 changed files with 29 additions and 0 deletions

View file

@ -399,6 +399,9 @@ pub enum UserDisplayStyle {
// it can wind up being the Matrix username if there are display name collisions in the room, // it can wind up being the Matrix username if there are display name collisions in the room,
// in order to avoid any confusion. // in order to avoid any confusion.
DisplayName, DisplayName,
// Acts like Username, except when the username matches given regex, then acts like DisplayName
Regex,
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
@ -568,6 +571,7 @@ pub struct TunableValues {
pub typing_notice_display: bool, pub typing_notice_display: bool,
pub users: UserOverrides, pub users: UserOverrides,
pub username_display: UserDisplayStyle, pub username_display: UserDisplayStyle,
pub username_display_regex: Option<String>,
pub message_user_color: bool, pub message_user_color: bool,
pub default_room: Option<String>, pub default_room: Option<String>,
pub open_command: Option<Vec<String>>, pub open_command: Option<Vec<String>>,
@ -595,6 +599,7 @@ pub struct Tunables {
pub typing_notice_display: Option<bool>, pub typing_notice_display: Option<bool>,
pub users: Option<UserOverrides>, pub users: Option<UserOverrides>,
pub username_display: Option<UserDisplayStyle>, pub username_display: Option<UserDisplayStyle>,
pub username_display_regex: Option<String>,
pub message_user_color: Option<bool>, pub message_user_color: Option<bool>,
pub default_room: Option<String>, pub default_room: Option<String>,
pub open_command: Option<Vec<String>>, pub open_command: Option<Vec<String>>,
@ -626,6 +631,7 @@ impl Tunables {
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
users: merge_maps(self.users, other.users), users: merge_maps(self.users, other.users),
username_display: self.username_display.or(other.username_display), 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), message_user_color: self.message_user_color.or(other.message_user_color),
default_room: self.default_room.or(other.default_room), default_room: self.default_room.or(other.default_room),
open_command: self.open_command.or(other.open_command), open_command: self.open_command.or(other.open_command),
@ -655,6 +661,7 @@ impl Tunables {
typing_notice_display: self.typing_notice_display.unwrap_or(true), typing_notice_display: self.typing_notice_display.unwrap_or(true),
users: self.users.unwrap_or_default(), users: self.users.unwrap_or_default(),
username_display: self.username_display.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), message_user_color: self.message_user_color.unwrap_or(false),
default_room: self.default_room, default_room: self.default_room,
open_command: self.open_command, open_command: self.open_command,
@ -1048,6 +1055,20 @@ impl ApplicationSettings {
Cow::Borrowed(user_id.as_str()) 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) Span::styled(name, style)
@ -1164,6 +1185,13 @@ mod tests {
let res: Tunables = let res: Tunables =
serde_json::from_str("{\"username_display\": \"displayname\"}").unwrap(); serde_json::from_str("{\"username_display\": \"displayname\"}").unwrap();
assert_eq!(res.username_display, Some(UserDisplayStyle::DisplayName)); assert_eq!(res.username_display, Some(UserDisplayStyle::DisplayName));
let res: Tunables = serde_json::from_str(
"{\"username_display\": \"regex\",\n\"username_display_regex\": \"foo\"}",
)
.unwrap();
assert_eq!(res.username_display, Some(UserDisplayStyle::Regex));
assert_eq!(res.username_display_regex.unwrap_or("FAILED".into()), "foo".to_string());
} }
#[test] #[test]

View file

@ -189,6 +189,7 @@ pub fn mock_tunables() -> TunableValues {
open_command: None, open_command: None,
external_edit_file_suffix: String::from(".md"), external_edit_file_suffix: String::from(".md"),
username_display: UserDisplayStyle::Username, username_display: UserDisplayStyle::Username,
username_display_regex: Some(String::from(".*")),
message_user_color: false, message_user_color: false,
mouse: Default::default(), mouse: Default::default(),
notifications: Notifications { notifications: Notifications {