Commit 515fb5852d

515fb5852dba255f7e97d3aa63650708cec9ac4b

parent: 8ad4307763

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-09-11 22:45 UTC
committer: <strk@kbt.io>

look: add configurable Matrix nick format

Layout: unified · split

README.md +8 −1
@@ -297,8 +297,15 @@ Nick prefix colors for Matrix power levels can be changed with:
297297 /set matrix-rust.color.nick_prefix_moderator lightmagenta
298298 /set matrix-rust.color.nick_prefix_power yellow
299299
300Existing nicklist entries may need a member update or plugin restart before the new colors are visible.
300Existing nicklist entries may need a member update or plugin restart before the new colors or nick format are visible.
301301
302Matrix member nicknames default to display names. To include MXID parts in chat,
303the nicklist, `/names`, and nick completion, set `matrix-rust.look.nick_format`:
304
305 /set matrix-rust.look.nick_format "%u (%d)"
306
307Supported placeholders are `%d` for display name, `%m` for full MXID, `%u` for
308MXID localpart, and `%h` for homeserver.
302309
303310# Helpful Commands
304311
build.rs +4 −1
@@ -43,7 +43,10 @@ fn main() {
4343 let describe =
4444 String::from_utf8_lossy(&output.stdout).trim().to_string();
4545 println!("cargo::rustc-env=GIT_DESCRIBE={}", describe);
46 println!("cargo::warning=weechat-matrix git describe: {}", describe);
46 println!(
47 "cargo::warning=weechat-matrix git describe: {}",
48 describe
49 );
4750 } else {
4851 println!("cargo::warning=weechat-matrix: git describe failed");
4952 }
src/config.rs +9
@@ -147,6 +147,15 @@ config!(
147147 // Range: quote-preserving default to a large scrollback window.
148148 0..100000,
149149 },
150
151 nick_format: String {
152 // Description.
153 "Format for Matrix member nicks. Supported placeholders: %d for \
154 display name, %m for full MXID, %u for MXID localpart, and %h \
155 for MXID homeserver",
156 // Default value.
157 "%d",
158 },
150159 },
151160
152161 Section color {
src/room/members.rs +86 −6
@@ -99,6 +99,7 @@ pub struct WeechatRoomMember {
9999 color: Rc<String>,
100100 prefix_color: Rc<String>,
101101 ambiguous_nick: Rc<bool>,
102 nick: Rc<String>,
102103}
103104
104105impl PartialEq for WeechatRoomMember {
@@ -242,6 +243,12 @@ impl Members {
242243 .map(|a| *a)
243244 .unwrap_or(false),
244245 ),
246 nick: Rc::new(format_member_nick(
247 &self.config.borrow().look().nick_format(),
248 member.user_id(),
249 member.name(),
250 member.name_ambiguous(),
251 )),
245252 inner: member,
246253 }
247254 }
@@ -632,6 +639,41 @@ mod tests {
632639 None,
633640 );
634641 }
642
643 #[test]
644 fn nick_format_defaults_to_display_name_with_ambiguity_suffix() {
645 let user_id = UserId::parse("@ada:example.org").unwrap();
646
647 assert_eq!(format_member_nick("%d", &user_id, "Ada", false), "Ada");
648 assert_eq!(
649 format_member_nick("%d", &user_id, "Ada", true),
650 "Ada (@ada:example.org)"
651 );
652 }
653
654 #[test]
655 fn nick_format_exposes_mxid_parts() {
656 let user_id = UserId::parse("@ada:example.org").unwrap();
657
658 assert_eq!(
659 format_member_nick("%u (%d) @ %h", &user_id, "Ada", true),
660 "ada (Ada) @ example.org"
661 );
662 assert_eq!(
663 format_member_nick("%m", &user_id, "Ada", true),
664 "@ada:example.org"
665 );
666 }
667
668 #[test]
669 fn nick_format_keeps_unknown_placeholders_literal() {
670 let user_id = UserId::parse("@ada:example.org").unwrap();
671
672 assert_eq!(
673 format_member_nick("%d %% %x %", &user_id, "Ada", false),
674 "Ada % %x %"
675 );
676 }
635677}
636678
637679impl WeechatRoomMember {
@@ -710,7 +752,10 @@ impl WeechatRoomMember {
710752 }
711753
712754 pub fn nick_colored(&self) -> String {
713 if *self.ambiguous_nick {
755 let default_ambiguous_nick =
756 format!("{} ({})", self.nick_raw(), self.user_id());
757 if *self.ambiguous_nick && self.nick.as_ref() == &default_ambiguous_nick
758 {
714759 // TODO: this should color the parenthesis differently.
715760 format!(
716761 "{}{}{} ({})",
@@ -725,17 +770,52 @@ impl WeechatRoomMember {
725770 Weechat::color(self.prefix_color()),
726771 self.prefix(),
727772 Weechat::color(self.color()),
728 self.nick_raw(),
773 self.nick(),
729774 Weechat::color("reset")
730775 )
731776 }
732777 }
733778
734779 pub fn nick(&self) -> String {
735 if *self.ambiguous_nick {
736 format!("{} ({})", self.nick_raw(), self.user_id())
737 } else {
738 self.nick_raw().to_string()
780 self.nick.as_ref().to_owned()
781 }
782}
783
784fn format_member_nick(
785 format: &str,
786 user_id: &UserId,
787 display_name: &str,
788 ambiguous_display_name: bool,
789) -> String {
790 let localpart = user_id.localpart();
791 let homeserver = user_id.server_name().as_str();
792
793 let mut formatted =
794 String::with_capacity(format.len() + display_name.len());
795 let mut chars = format.chars().peekable();
796 while let Some(ch) = chars.next() {
797 if ch != '%' {
798 formatted.push(ch);
799 continue;
800 }
801
802 match chars.next() {
803 Some('d') => formatted.push_str(display_name),
804 Some('m') => formatted.push_str(user_id.as_str()),
805 Some('u') => formatted.push_str(localpart),
806 Some('h') => formatted.push_str(homeserver),
807 Some('%') => formatted.push('%'),
808 Some(other) => {
809 formatted.push('%');
810 formatted.push(other);
811 }
812 None => formatted.push('%'),
739813 }
740814 }
815
816 if ambiguous_display_name && format == "%d" {
817 format!("{} ({})", formatted, user_id)
818 } else {
819 formatted
820 }
741821}