Commit f7ac4cc4d3

f7ac4cc4d367e0cced4d81b8537c8f1b386fbf99

parent: 74e6cbdca6

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-09-25 03:43 UTC
committer: <noreply@github.com>

Add `theme.users` for styling the username palette (#744)

Layout: unified · split

docs/iamb.5 +19
@@ -809,6 +809,25 @@ Configure how server notices is styled.
809809.It Sy redacted
810810Configure how redacted events in the timeline is styled.
811811.El
812.It Sy users
813For controlling how usernames and identifiers are styled.
814.Pp
815In addition to the common
816.Sy color ,
817.Sy background ,
818and
819.Sy modifiers
820properties, you can also specify a
821.Sy colors
822array for providing a broad palette of colors from which to style usernames.
823.Pp
824By default, this is a selection of 13 standard ANSI colors that should look okay on both light and dark backgrounds.
825You can set this array to
826.Sy "[]"
827to force all usernames to use the value in
828.Sy color
829or its inherited value from
830.Sy "theme.default" .
812831.It Sy windows
813832For controlling how the windows are styled.
814833.Bl -tag -width Ds
src/base.rs +5 −5
@@ -2845,7 +2845,6 @@ pub mod tests {
28452845 use ratatui::style::Color;
28462846 use serde_json::{Map, Value};
28472847
2848 use crate::config::user_style_from_color;
28492848 use crate::tests::*;
28502849
28512850 fn mock_room_message_event(
@@ -3058,6 +3057,7 @@ pub mod tests {
30583057 fn test_typing_spans() {
30593058 let mut info = RoomInfo::default();
30603059 let settings = mock_settings();
3060 let theme = &settings.theme;
30613061
30623062 let users0 = vec![];
30633063 let users1 = vec![TEST_USER1.clone()];
@@ -3091,7 +3091,7 @@ pub mod tests {
30913091 assert_eq!(
30923092 info.get_typing_spans(&settings),
30933093 Line::from(vec![
3094 Span::styled("@user1:example.com", user_style("@user1:example.com")),
3094 Span::styled("@user1:example.com", theme.users.style("@user1:example.com", None)),
30953095 Span::from(" is typing...")
30963096 ])
30973097 );
@@ -3102,9 +3102,9 @@ pub mod tests {
31023102 assert_eq!(
31033103 info.get_typing_spans(&settings),
31043104 Line::from(vec![
3105 Span::styled("@user1:example.com", user_style("@user1:example.com")),
3105 Span::styled("@user1:example.com", theme.users.style("@user1:example.com", None)),
31063106 Span::raw(" and "),
3107 Span::styled("@user2:example.com", user_style("@user2:example.com")),
3107 Span::styled("@user2:example.com", theme.users.style("@user2:example.com", None)),
31083108 Span::raw(" are typing...")
31093109 ])
31103110 );
@@ -3125,7 +3125,7 @@ pub mod tests {
31253125 assert_eq!(
31263126 info.get_typing_spans(&settings),
31273127 Line::from(vec![
3128 Span::styled("USER 5", user_style_from_color(Color::Black)),
3128 Span::styled("USER 5", Style::default().fg(Color::Black).bold()),
31293129 Span::from(" is typing...")
31303130 ])
31313131 );
src/config/mod.rs +5 −37
@@ -1,9 +1,7 @@
11//! # Logic for loading and validating application configuration
22
3use std::collections::hash_map::DefaultHasher;
43use std::env;
54use std::fs::File;
6use std::hash::{Hash, Hasher};
75use std::io::{BufReader, BufWriter, Write as _};
86use std::process;
97use std::sync::Arc;
@@ -74,34 +72,6 @@ const DEFAULT_LOG_LEVEL: &str = if cfg!(feature = "max_level_error") {
7472 "warn"
7573};
7674
77const COLORS: [Color; 13] = [
78 Color::Blue,
79 Color::Cyan,
80 Color::Green,
81 Color::LightBlue,
82 Color::LightGreen,
83 Color::LightCyan,
84 Color::LightMagenta,
85 Color::LightRed,
86 Color::LightYellow,
87 Color::Magenta,
88 Color::Red,
89 Color::Reset,
90 Color::Yellow,
91];
92
93pub fn user_color(user: &str) -> Color {
94 let mut hasher = DefaultHasher::new();
95 user.hash(&mut hasher);
96 let color = hasher.finish() as usize % COLORS.len();
97
98 COLORS[color]
99}
100
101pub fn user_style_from_color(color: Color) -> Style {
102 Style::default().fg(color).add_modifier(StyleModifier::BOLD)
103}
104
10575fn is_profile_char(c: char) -> bool {
10676 c.is_ascii_alphanumeric() || c == '.' || c == '-'
10777}
@@ -1428,9 +1398,7 @@ impl ApplicationSettings {
14281398 .map(|user| (user.color, user.name.as_ref().and_then(|s| s.chars().next())))
14291399 .unwrap_or_default();
14301400
1431 let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
1432 let style = user_style_from_color(color);
1433
1401 let style = self.theme.users.style(user_id.as_str(), color);
14341402 let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' '));
14351403
14361404 Span::styled(String::from(c), style)
@@ -1452,18 +1420,18 @@ impl ApplicationSettings {
14521420 .users
14531421 .get(user_id)
14541422 .and_then(|user| user.color)
1455 .unwrap_or_else(|| user_color(user_id.as_str()))
1423 .unwrap_or_else(|| self.theme.users.color(user_id.as_str()))
14561424 }
14571425
14581426 pub fn get_user_style(&self, user_id: &UserId) -> Style {
1459 user_style_from_color(self.get_user_color(user_id))
1427 let (color, _) = self.get_user_overrides(user_id);
1428 self.theme.users.style(user_id.as_str(), color)
14601429 }
14611430
14621431 pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> {
14631432 let (color, name) = self.get_user_overrides(user_id);
14641433
1465 let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
1466 let style = user_style_from_color(color);
1434 let style = self.theme.users.style(user_id.as_str(), color);
14671435 let name = match (name, &self.tunables.username_display) {
14681436 (Some(name), _) => name,
14691437 (None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()),
src/config/theme.rs +87
@@ -1,4 +1,7 @@
11//! # Logic for parsing styling configuration into [Style].
2use std::collections::hash_map::DefaultHasher;
3use std::hash::{Hash, Hasher};
4
25use serde::de::Error as SerdeError;
36use serde::de::Visitor;
47use serde::{Deserialize, Deserializer};
@@ -40,6 +43,24 @@ pub fn default_theme() -> Theme {
4043 unread: Stylable::with_modifiers(StyleModifier::BOLD),
4144 ..Default::default()
4245 },
46 users: ThemeUsers {
47 colors: Some(vec![
48 Color::Blue,
49 Color::Cyan,
50 Color::Green,
51 Color::LightBlue,
52 Color::LightGreen,
53 Color::LightCyan,
54 Color::LightMagenta,
55 Color::LightRed,
56 Color::LightYellow,
57 Color::Magenta,
58 Color::Red,
59 Color::Reset,
60 Color::Yellow,
61 ]),
62 stylable: Stylable::with_modifiers(StyleModifier::BOLD),
63 },
4364 ..Default::default()
4465 }
4566}
@@ -213,6 +234,10 @@ pub struct Theme {
213234 #[serde(default)]
214235 tabs: ThemeTabs,
215236
237 /// Configuration for styling users.
238 #[serde(default)]
239 users: ThemeUsers,
240
216241 /// Configuration for styling windows.
217242 #[serde(default)]
218243 windows: ThemeWindows,
@@ -229,6 +254,7 @@ impl Theme {
229254 tabs: self.tabs.merge(other.tabs),
230255 timeline: self.timeline.merge(other.timeline),
231256 rooms: self.rooms.merge(other.rooms),
257 users: self.users.merge(other.users),
232258 windows: self.windows.merge(other.windows),
233259 }
234260 }
@@ -245,6 +271,7 @@ impl Theme {
245271 timeline: self.timeline.values(base),
246272 rooms: self.rooms.values(base),
247273 tabs: self.tabs.values(base),
274 users: self.users.values(base),
248275 windows: self.windows.values(base),
249276 }
250277 }
@@ -276,6 +303,9 @@ pub struct ThemeValues {
276303 /// Styling for rendering tabs.
277304 pub tabs: ThemeTabsValues,
278305
306 /// Styling for rendering users.
307 pub users: ThemeUsersValues,
308
279309 /// Styling for rendering windows.
280310 pub windows: ThemeWindowsValues,
281311}
@@ -657,6 +687,63 @@ pub struct ThemeWindowsValues {
657687 pub title: Style,
658688}
659689
690#[derive(Clone, Debug, Default, Deserialize)]
691struct ThemeUsers {
692 colors: Option<Vec<Color>>,
693 #[serde(flatten)]
694 stylable: Stylable,
695}
696
697impl ThemeUsers {
698 fn merge(self, other: Self) -> Self {
699 Self {
700 colors: self.colors.or(other.colors),
701 stylable: self.stylable.merge(other.stylable),
702 }
703 }
704
705 fn values(self, base: Style) -> ThemeUsersValues {
706 let colors = self.colors.unwrap_or_default();
707 let style = base.patch(self.stylable);
708
709 ThemeUsersValues { colors, style }
710 }
711}
712
713#[derive(Clone, Debug, Default)]
714pub struct ThemeUsersValues {
715 colors: Vec<Color>,
716 style: Style,
717}
718
719impl ThemeUsersValues {
720 pub fn color(&self, user_id: &str) -> Color {
721 if self.colors.is_empty() {
722 return self.style.fg.unwrap_or(Color::Reset);
723 }
724
725 let mut hasher = DefaultHasher::new();
726 user_id.hash(&mut hasher);
727 let color = hasher.finish() as usize % self.colors.len();
728 self.colors[color]
729 }
730
731 pub fn style(&self, user_id: &str, explicit: Option<Color>) -> Style {
732 let style = self.style;
733
734 if let Some(c) = explicit {
735 // Use explicit color from config file:
736 style.fg(c)
737 } else if self.colors.is_empty() {
738 // User has explicitly set an empty array:
739 style
740 } else {
741 // Hash on user ID to pick a color from the array:
742 style.fg(self.color(user_id))
743 }
744 }
745}
746
660747#[cfg(test)]
661748mod tests {
662749 use super::*;
src/tests.rs −4
@@ -50,10 +50,6 @@ lazy_static! {
5050 };
5151}
5252
53pub fn user_style(user: &str) -> Style {
54 user_style_from_color(user_color(user))
55}
56
5753pub fn mock_room1_message(
5854 content: RoomMessageEventContent,
5955 sender: OwnedUserId,
src/windows/mod.rs +9 −6
@@ -1165,18 +1165,21 @@ impl ListItem<IambInfo> for MemberItem {
11651165 let info = store.application.rooms.get_or_default(self.room_id.clone());
11661166 let user_id = self.member.user_id();
11671167
1168 let theme = &store.application.settings.theme;
1169
11681170 let (color, name) = store.application.settings.get_user_overrides(self.member.user_id());
1169 let color = color.unwrap_or_else(|| super::config::user_color(user_id.as_str()));
1171 let user_style = theme.users.style(user_id.as_str(), color);
1172 let color = user_style.fg.unwrap_or(Color::Reset);
11701173
1171 let style = store.application.settings.theme.default;
11721174 let style = if selected {
1173 // Ensure the whole item has the same color when it's selected:
1174 style.fg(color).add_modifier(StyleModifier::REVERSED)
1175 // Ensure the whole item has the same color as `user_style` when it's selected:
1176 theme.default.fg(color).add_modifier(StyleModifier::REVERSED)
11751177 } else {
1176 style
1178 theme.default
11771179 };
1178 let user_style = style.patch(super::config::user_style_from_color(color));
1180
11791181 let role_style = style.add_modifier(StyleModifier::BOLD);
1182 let user_style = style.patch(user_style);
11801183
11811184 let mut spans = vec![];
11821185 let mut tags = vec![];