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:
297 /set matrix-rust.color.nick_prefix_moderator lightmagenta 297 /set matrix-rust.color.nick_prefix_moderator lightmagenta
298 /set matrix-rust.color.nick_prefix_power yellow 298 /set matrix-rust.color.nick_prefix_power yellow
299 299
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.
301 301
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.
302 309
303# Helpful Commands 310# Helpful Commands
304 311
build.rs +4 −1
@@ -43,7 +43,10 @@ fn main() {
43 let describe = 43 let describe =
44 String::from_utf8_lossy(&output.stdout).trim().to_string(); 44 String::from_utf8_lossy(&output.stdout).trim().to_string();
45 println!("cargo::rustc-env=GIT_DESCRIBE={}", describe); 45 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 );
47 } else { 50 } else {
48 println!("cargo::warning=weechat-matrix: git describe failed"); 51 println!("cargo::warning=weechat-matrix: git describe failed");
49 } 52 }
src/config.rs +9
@@ -147,6 +147,15 @@ config!(
147 // Range: quote-preserving default to a large scrollback window. 147 // Range: quote-preserving default to a large scrollback window.
148 0..100000, 148 0..100000,
149 }, 149 },
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 },
150 }, 159 },
151 160
152 Section color { 161 Section color {
src/room/members.rs +86 −6
@@ -99,6 +99,7 @@ pub struct WeechatRoomMember {
99 color: Rc<String>, 99 color: Rc<String>,
100 prefix_color: Rc<String>, 100 prefix_color: Rc<String>,
101 ambiguous_nick: Rc<bool>, 101 ambiguous_nick: Rc<bool>,
102 nick: Rc<String>,
102} 103}
103 104
104impl PartialEq for WeechatRoomMember { 105impl PartialEq for WeechatRoomMember {
@@ -242,6 +243,12 @@ impl Members {
242 .map(|a| *a) 243 .map(|a| *a)
243 .unwrap_or(false), 244 .unwrap_or(false),
244 ), 245 ),
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 )),
245 inner: member, 252 inner: member,
246 } 253 }
247 } 254 }
@@ -632,6 +639,41 @@ mod tests {
632 None, 639 None,
633 ); 640 );
634 } 641 }
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 }
635} 677}
636 678
637impl WeechatRoomMember { 679impl WeechatRoomMember {
@@ -710,7 +752,10 @@ impl WeechatRoomMember {
710 } 752 }
711 753
712 pub fn nick_colored(&self) -> String { 754 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 {
714 // TODO: this should color the parenthesis differently. 759 // TODO: this should color the parenthesis differently.
715 format!( 760 format!(
716 "{}{}{} ({})", 761 "{}{}{} ({})",
@@ -725,17 +770,52 @@ impl WeechatRoomMember {
725 Weechat::color(self.prefix_color()), 770 Weechat::color(self.prefix_color()),
726 self.prefix(), 771 self.prefix(),
727 Weechat::color(self.color()), 772 Weechat::color(self.color()),
728 self.nick_raw(), 773 self.nick(),
729 Weechat::color("reset") 774 Weechat::color("reset")
730 ) 775 )
731 } 776 }
732 } 777 }
733 778
734 pub fn nick(&self) -> String { 779 pub fn nick(&self) -> String {
735 if *self.ambiguous_nick { 780 self.nick.as_ref().to_owned()
736 format!("{} ({})", self.nick_raw(), self.user_id()) 781 }
737 } else { 782}
738 self.nick_raw().to_string() 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('%'),
739 } 813 }
740 } 814 }
815
816 if ambiguous_display_name && format == "%d" {
817 format!("{} ({})", formatted, user_id)
818 } else {
819 formatted
820 }
741} 821}