Commit f7ac4cc4d3
Unregistered key
Layout: unified · split
docs/iamb.5 +19
| @@ -809,6 +809,25 @@ Configure how server notices is styled. | ||
| 809 | 809 | .It Sy redacted |
| 810 | 810 | Configure how redacted events in the timeline is styled. |
| 811 | 811 | .El |
| 812 | .It Sy users | |
| 813 | For controlling how usernames and identifiers are styled. | |
| 814 | .Pp | |
| 815 | In addition to the common | |
| 816 | .Sy color , | |
| 817 | .Sy background , | |
| 818 | and | |
| 819 | .Sy modifiers | |
| 820 | properties, you can also specify a | |
| 821 | .Sy colors | |
| 822 | array for providing a broad palette of colors from which to style usernames. | |
| 823 | .Pp | |
| 824 | By default, this is a selection of 13 standard ANSI colors that should look okay on both light and dark backgrounds. | |
| 825 | You can set this array to | |
| 826 | .Sy "[]" | |
| 827 | to force all usernames to use the value in | |
| 828 | .Sy color | |
| 829 | or its inherited value from | |
| 830 | .Sy "theme.default" . | |
| 812 | 831 | .It Sy windows |
| 813 | 832 | For controlling how the windows are styled. |
| 814 | 833 | .Bl -tag -width Ds |
src/base.rs +5 −5
| @@ -2845,7 +2845,6 @@ pub mod tests { | ||
| 2845 | 2845 | use ratatui::style::Color; |
| 2846 | 2846 | use serde_json::{Map, Value}; |
| 2847 | 2847 | |
| 2848 | use crate::config::user_style_from_color; | |
| 2849 | 2848 | use crate::tests::*; |
| 2850 | 2849 | |
| 2851 | 2850 | fn mock_room_message_event( |
| @@ -3058,6 +3057,7 @@ pub mod tests { | ||
| 3058 | 3057 | fn test_typing_spans() { |
| 3059 | 3058 | let mut info = RoomInfo::default(); |
| 3060 | 3059 | let settings = mock_settings(); |
| 3060 | let theme = &settings.theme; | |
| 3061 | 3061 | |
| 3062 | 3062 | let users0 = vec![]; |
| 3063 | 3063 | let users1 = vec![TEST_USER1.clone()]; |
| @@ -3091,7 +3091,7 @@ pub mod tests { | ||
| 3091 | 3091 | assert_eq!( |
| 3092 | 3092 | info.get_typing_spans(&settings), |
| 3093 | 3093 | 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)), | |
| 3095 | 3095 | Span::from(" is typing...") |
| 3096 | 3096 | ]) |
| 3097 | 3097 | ); |
| @@ -3102,9 +3102,9 @@ pub mod tests { | ||
| 3102 | 3102 | assert_eq!( |
| 3103 | 3103 | info.get_typing_spans(&settings), |
| 3104 | 3104 | 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)), | |
| 3106 | 3106 | 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)), | |
| 3108 | 3108 | Span::raw(" are typing...") |
| 3109 | 3109 | ]) |
| 3110 | 3110 | ); |
| @@ -3125,7 +3125,7 @@ pub mod tests { | ||
| 3125 | 3125 | assert_eq!( |
| 3126 | 3126 | info.get_typing_spans(&settings), |
| 3127 | 3127 | 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()), | |
| 3129 | 3129 | Span::from(" is typing...") |
| 3130 | 3130 | ]) |
| 3131 | 3131 | ); |
src/config/mod.rs +5 −37
| @@ -1,9 +1,7 @@ | ||
| 1 | 1 | //! # Logic for loading and validating application configuration |
| 2 | 2 | |
| 3 | use std::collections::hash_map::DefaultHasher; | |
| 4 | 3 | use std::env; |
| 5 | 4 | use std::fs::File; |
| 6 | use std::hash::{Hash, Hasher}; | |
| 7 | 5 | use std::io::{BufReader, BufWriter, Write as _}; |
| 8 | 6 | use std::process; |
| 9 | 7 | use std::sync::Arc; |
| @@ -74,34 +72,6 @@ const DEFAULT_LOG_LEVEL: &str = if cfg!(feature = "max_level_error") { | ||
| 74 | 72 | "warn" |
| 75 | 73 | }; |
| 76 | 74 | |
| 77 | const 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 | ||
| 93 | pub 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 | ||
| 101 | pub fn user_style_from_color(color: Color) -> Style { | |
| 102 | Style::default().fg(color).add_modifier(StyleModifier::BOLD) | |
| 103 | } | |
| 104 | ||
| 105 | 75 | fn is_profile_char(c: char) -> bool { |
| 106 | 76 | c.is_ascii_alphanumeric() || c == '.' || c == '-' |
| 107 | 77 | } |
| @@ -1428,9 +1398,7 @@ impl ApplicationSettings { | ||
| 1428 | 1398 | .map(|user| (user.color, user.name.as_ref().and_then(|s| s.chars().next()))) |
| 1429 | 1399 | .unwrap_or_default(); |
| 1430 | 1400 | |
| 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); | |
| 1434 | 1402 | let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' ')); |
| 1435 | 1403 | |
| 1436 | 1404 | Span::styled(String::from(c), style) |
| @@ -1452,18 +1420,18 @@ impl ApplicationSettings { | ||
| 1452 | 1420 | .users |
| 1453 | 1421 | .get(user_id) |
| 1454 | 1422 | .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())) | |
| 1456 | 1424 | } |
| 1457 | 1425 | |
| 1458 | 1426 | 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) | |
| 1460 | 1429 | } |
| 1461 | 1430 | |
| 1462 | 1431 | pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> { |
| 1463 | 1432 | let (color, name) = self.get_user_overrides(user_id); |
| 1464 | 1433 | |
| 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); | |
| 1467 | 1435 | let name = match (name, &self.tunables.username_display) { |
| 1468 | 1436 | (Some(name), _) => name, |
| 1469 | 1437 | (None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()), |
src/config/theme.rs +87
| @@ -1,4 +1,7 @@ | ||
| 1 | 1 | //! # Logic for parsing styling configuration into [Style]. |
| 2 | use std::collections::hash_map::DefaultHasher; | |
| 3 | use std::hash::{Hash, Hasher}; | |
| 4 | ||
| 2 | 5 | use serde::de::Error as SerdeError; |
| 3 | 6 | use serde::de::Visitor; |
| 4 | 7 | use serde::{Deserialize, Deserializer}; |
| @@ -40,6 +43,24 @@ pub fn default_theme() -> Theme { | ||
| 40 | 43 | unread: Stylable::with_modifiers(StyleModifier::BOLD), |
| 41 | 44 | ..Default::default() |
| 42 | 45 | }, |
| 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 | }, | |
| 43 | 64 | ..Default::default() |
| 44 | 65 | } |
| 45 | 66 | } |
| @@ -213,6 +234,10 @@ pub struct Theme { | ||
| 213 | 234 | #[serde(default)] |
| 214 | 235 | tabs: ThemeTabs, |
| 215 | 236 | |
| 237 | /// Configuration for styling users. | |
| 238 | #[serde(default)] | |
| 239 | users: ThemeUsers, | |
| 240 | ||
| 216 | 241 | /// Configuration for styling windows. |
| 217 | 242 | #[serde(default)] |
| 218 | 243 | windows: ThemeWindows, |
| @@ -229,6 +254,7 @@ impl Theme { | ||
| 229 | 254 | tabs: self.tabs.merge(other.tabs), |
| 230 | 255 | timeline: self.timeline.merge(other.timeline), |
| 231 | 256 | rooms: self.rooms.merge(other.rooms), |
| 257 | users: self.users.merge(other.users), | |
| 232 | 258 | windows: self.windows.merge(other.windows), |
| 233 | 259 | } |
| 234 | 260 | } |
| @@ -245,6 +271,7 @@ impl Theme { | ||
| 245 | 271 | timeline: self.timeline.values(base), |
| 246 | 272 | rooms: self.rooms.values(base), |
| 247 | 273 | tabs: self.tabs.values(base), |
| 274 | users: self.users.values(base), | |
| 248 | 275 | windows: self.windows.values(base), |
| 249 | 276 | } |
| 250 | 277 | } |
| @@ -276,6 +303,9 @@ pub struct ThemeValues { | ||
| 276 | 303 | /// Styling for rendering tabs. |
| 277 | 304 | pub tabs: ThemeTabsValues, |
| 278 | 305 | |
| 306 | /// Styling for rendering users. | |
| 307 | pub users: ThemeUsersValues, | |
| 308 | ||
| 279 | 309 | /// Styling for rendering windows. |
| 280 | 310 | pub windows: ThemeWindowsValues, |
| 281 | 311 | } |
| @@ -657,6 +687,63 @@ pub struct ThemeWindowsValues { | ||
| 657 | 687 | pub title: Style, |
| 658 | 688 | } |
| 659 | 689 | |
| 690 | #[derive(Clone, Debug, Default, Deserialize)] | |
| 691 | struct ThemeUsers { | |
| 692 | colors: Option<Vec<Color>>, | |
| 693 | #[serde(flatten)] | |
| 694 | stylable: Stylable, | |
| 695 | } | |
| 696 | ||
| 697 | impl 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)] | |
| 714 | pub struct ThemeUsersValues { | |
| 715 | colors: Vec<Color>, | |
| 716 | style: Style, | |
| 717 | } | |
| 718 | ||
| 719 | impl 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 | ||
| 660 | 747 | #[cfg(test)] |
| 661 | 748 | mod tests { |
| 662 | 749 | use super::*; |
src/tests.rs −4
| @@ -50,10 +50,6 @@ lazy_static! { | ||
| 50 | 50 | }; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | pub fn user_style(user: &str) -> Style { | |
| 54 | user_style_from_color(user_color(user)) | |
| 55 | } | |
| 56 | ||
| 57 | 53 | pub fn mock_room1_message( |
| 58 | 54 | content: RoomMessageEventContent, |
| 59 | 55 | sender: OwnedUserId, |
src/windows/mod.rs +9 −6
| @@ -1165,18 +1165,21 @@ impl ListItem<IambInfo> for MemberItem { | ||
| 1165 | 1165 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| 1166 | 1166 | let user_id = self.member.user_id(); |
| 1167 | 1167 | |
| 1168 | let theme = &store.application.settings.theme; | |
| 1169 | ||
| 1168 | 1170 | 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); | |
| 1170 | 1173 | |
| 1171 | let style = store.application.settings.theme.default; | |
| 1172 | 1174 | 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) | |
| 1175 | 1177 | } else { |
| 1176 | style | |
| 1178 | theme.default | |
| 1177 | 1179 | }; |
| 1178 | let user_style = style.patch(super::config::user_style_from_color(color)); | |
| 1180 | ||
| 1179 | 1181 | let role_style = style.add_modifier(StyleModifier::BOLD); |
| 1182 | let user_style = style.patch(user_style); | |
| 1180 | 1183 | |
| 1181 | 1184 | let mut spans = vec![]; |
| 1182 | 1185 | let mut tags = vec![]; |