Commit f7ac4cc4d3
Unregistered key
Layout: unified · split
docs/iamb.5 +19
| @@ -809,6 +809,25 @@ Configure how server notices is styled. | |||
| 809 | .It Sy redacted | 809 | .It Sy redacted |
| 810 | Configure how redacted events in the timeline is styled. | 810 | Configure how redacted events in the timeline is styled. |
| 811 | .El | 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 | .It Sy windows | 831 | .It Sy windows |
| 813 | For controlling how the windows are styled. | 832 | For controlling how the windows are styled. |
| 814 | .Bl -tag -width Ds | 833 | .Bl -tag -width Ds |
src/base.rs +5 −5
| @@ -2845,7 +2845,6 @@ pub mod tests { | |||
| 2845 | use ratatui::style::Color; | 2845 | use ratatui::style::Color; |
| 2846 | use serde_json::{Map, Value}; | 2846 | use serde_json::{Map, Value}; |
| 2847 | 2847 | ||
| 2848 | use crate::config::user_style_from_color; | ||
| 2849 | use crate::tests::*; | 2848 | use crate::tests::*; |
| 2850 | 2849 | ||
| 2851 | fn mock_room_message_event( | 2850 | fn mock_room_message_event( |
| @@ -3058,6 +3057,7 @@ pub mod tests { | |||
| 3058 | fn test_typing_spans() { | 3057 | fn test_typing_spans() { |
| 3059 | let mut info = RoomInfo::default(); | 3058 | let mut info = RoomInfo::default(); |
| 3060 | let settings = mock_settings(); | 3059 | let settings = mock_settings(); |
| 3060 | let theme = &settings.theme; | ||
| 3061 | 3061 | ||
| 3062 | let users0 = vec![]; | 3062 | let users0 = vec![]; |
| 3063 | let users1 = vec![TEST_USER1.clone()]; | 3063 | let users1 = vec![TEST_USER1.clone()]; |
| @@ -3091,7 +3091,7 @@ pub mod tests { | |||
| 3091 | assert_eq!( | 3091 | assert_eq!( |
| 3092 | info.get_typing_spans(&settings), | 3092 | info.get_typing_spans(&settings), |
| 3093 | Line::from(vec![ | 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 | Span::from(" is typing...") | 3095 | Span::from(" is typing...") |
| 3096 | ]) | 3096 | ]) |
| 3097 | ); | 3097 | ); |
| @@ -3102,9 +3102,9 @@ pub mod tests { | |||
| 3102 | assert_eq!( | 3102 | assert_eq!( |
| 3103 | info.get_typing_spans(&settings), | 3103 | info.get_typing_spans(&settings), |
| 3104 | Line::from(vec![ | 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 | Span::raw(" and "), | 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 | Span::raw(" are typing...") | 3108 | Span::raw(" are typing...") |
| 3109 | ]) | 3109 | ]) |
| 3110 | ); | 3110 | ); |
| @@ -3125,7 +3125,7 @@ pub mod tests { | |||
| 3125 | assert_eq!( | 3125 | assert_eq!( |
| 3126 | info.get_typing_spans(&settings), | 3126 | info.get_typing_spans(&settings), |
| 3127 | Line::from(vec![ | 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 | Span::from(" is typing...") | 3129 | Span::from(" is typing...") |
| 3130 | ]) | 3130 | ]) |
| 3131 | ); | 3131 | ); |
src/config/mod.rs +5 −37
| @@ -1,9 +1,7 @@ | |||
| 1 | //! # Logic for loading and validating application configuration | 1 | //! # Logic for loading and validating application configuration |
| 2 | 2 | ||
| 3 | use std::collections::hash_map::DefaultHasher; | ||
| 4 | use std::env; | 3 | use std::env; |
| 5 | use std::fs::File; | 4 | use std::fs::File; |
| 6 | use std::hash::{Hash, Hasher}; | ||
| 7 | use std::io::{BufReader, BufWriter, Write as _}; | 5 | use std::io::{BufReader, BufWriter, Write as _}; |
| 8 | use std::process; | 6 | use std::process; |
| 9 | use std::sync::Arc; | 7 | use std::sync::Arc; |
| @@ -74,34 +72,6 @@ const DEFAULT_LOG_LEVEL: &str = if cfg!(feature = "max_level_error") { | |||
| 74 | "warn" | 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 | fn is_profile_char(c: char) -> bool { | 75 | fn is_profile_char(c: char) -> bool { |
| 106 | c.is_ascii_alphanumeric() || c == '.' || c == '-' | 76 | c.is_ascii_alphanumeric() || c == '.' || c == '-' |
| 107 | } | 77 | } |
| @@ -1428,9 +1398,7 @@ impl ApplicationSettings { | |||
| 1428 | .map(|user| (user.color, user.name.as_ref().and_then(|s| s.chars().next()))) | 1398 | .map(|user| (user.color, user.name.as_ref().and_then(|s| s.chars().next()))) |
| 1429 | .unwrap_or_default(); | 1399 | .unwrap_or_default(); |
| 1430 | 1400 | ||
| 1431 | let color = color.unwrap_or_else(|| user_color(user_id.as_str())); | 1401 | let style = self.theme.users.style(user_id.as_str(), color); |
| 1432 | let style = user_style_from_color(color); | ||
| 1433 | |||
| 1434 | let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' ')); | 1402 | let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' ')); |
| 1435 | 1403 | ||
| 1436 | Span::styled(String::from(c), style) | 1404 | Span::styled(String::from(c), style) |
| @@ -1452,18 +1420,18 @@ impl ApplicationSettings { | |||
| 1452 | .users | 1420 | .users |
| 1453 | .get(user_id) | 1421 | .get(user_id) |
| 1454 | .and_then(|user| user.color) | 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 | pub fn get_user_style(&self, user_id: &UserId) -> Style { | 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 | pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> { | 1431 | pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> { |
| 1463 | let (color, name) = self.get_user_overrides(user_id); | 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())); | 1434 | let style = self.theme.users.style(user_id.as_str(), color); |
| 1466 | let style = user_style_from_color(color); | ||
| 1467 | let name = match (name, &self.tunables.username_display) { | 1435 | let name = match (name, &self.tunables.username_display) { |
| 1468 | (Some(name), _) => name, | 1436 | (Some(name), _) => name, |
| 1469 | (None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()), | 1437 | (None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()), |
src/config/theme.rs +87
| @@ -1,4 +1,7 @@ | |||
| 1 | //! # Logic for parsing styling configuration into [Style]. | 1 | //! # Logic for parsing styling configuration into [Style]. |
| 2 | use std::collections::hash_map::DefaultHasher; | ||
| 3 | use std::hash::{Hash, Hasher}; | ||
| 4 | |||
| 2 | use serde::de::Error as SerdeError; | 5 | use serde::de::Error as SerdeError; |
| 3 | use serde::de::Visitor; | 6 | use serde::de::Visitor; |
| 4 | use serde::{Deserialize, Deserializer}; | 7 | use serde::{Deserialize, Deserializer}; |
| @@ -40,6 +43,24 @@ pub fn default_theme() -> Theme { | |||
| 40 | unread: Stylable::with_modifiers(StyleModifier::BOLD), | 43 | unread: Stylable::with_modifiers(StyleModifier::BOLD), |
| 41 | ..Default::default() | 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 | ..Default::default() | 64 | ..Default::default() |
| 44 | } | 65 | } |
| 45 | } | 66 | } |
| @@ -213,6 +234,10 @@ pub struct Theme { | |||
| 213 | #[serde(default)] | 234 | #[serde(default)] |
| 214 | tabs: ThemeTabs, | 235 | tabs: ThemeTabs, |
| 215 | 236 | ||
| 237 | /// Configuration for styling users. | ||
| 238 | #[serde(default)] | ||
| 239 | users: ThemeUsers, | ||
| 240 | |||
| 216 | /// Configuration for styling windows. | 241 | /// Configuration for styling windows. |
| 217 | #[serde(default)] | 242 | #[serde(default)] |
| 218 | windows: ThemeWindows, | 243 | windows: ThemeWindows, |
| @@ -229,6 +254,7 @@ impl Theme { | |||
| 229 | tabs: self.tabs.merge(other.tabs), | 254 | tabs: self.tabs.merge(other.tabs), |
| 230 | timeline: self.timeline.merge(other.timeline), | 255 | timeline: self.timeline.merge(other.timeline), |
| 231 | rooms: self.rooms.merge(other.rooms), | 256 | rooms: self.rooms.merge(other.rooms), |
| 257 | users: self.users.merge(other.users), | ||
| 232 | windows: self.windows.merge(other.windows), | 258 | windows: self.windows.merge(other.windows), |
| 233 | } | 259 | } |
| 234 | } | 260 | } |
| @@ -245,6 +271,7 @@ impl Theme { | |||
| 245 | timeline: self.timeline.values(base), | 271 | timeline: self.timeline.values(base), |
| 246 | rooms: self.rooms.values(base), | 272 | rooms: self.rooms.values(base), |
| 247 | tabs: self.tabs.values(base), | 273 | tabs: self.tabs.values(base), |
| 274 | users: self.users.values(base), | ||
| 248 | windows: self.windows.values(base), | 275 | windows: self.windows.values(base), |
| 249 | } | 276 | } |
| 250 | } | 277 | } |
| @@ -276,6 +303,9 @@ pub struct ThemeValues { | |||
| 276 | /// Styling for rendering tabs. | 303 | /// Styling for rendering tabs. |
| 277 | pub tabs: ThemeTabsValues, | 304 | pub tabs: ThemeTabsValues, |
| 278 | 305 | ||
| 306 | /// Styling for rendering users. | ||
| 307 | pub users: ThemeUsersValues, | ||
| 308 | |||
| 279 | /// Styling for rendering windows. | 309 | /// Styling for rendering windows. |
| 280 | pub windows: ThemeWindowsValues, | 310 | pub windows: ThemeWindowsValues, |
| 281 | } | 311 | } |
| @@ -657,6 +687,63 @@ pub struct ThemeWindowsValues { | |||
| 657 | pub title: Style, | 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 | #[cfg(test)] | 747 | #[cfg(test)] |
| 661 | mod tests { | 748 | mod tests { |
| 662 | use super::*; | 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 | pub fn mock_room1_message( | 53 | pub fn mock_room1_message( |
| 58 | content: RoomMessageEventContent, | 54 | content: RoomMessageEventContent, |
| 59 | sender: OwnedUserId, | 55 | sender: OwnedUserId, |
src/windows/mod.rs +9 −6
| @@ -1165,18 +1165,21 @@ impl ListItem<IambInfo> for MemberItem { | |||
| 1165 | let info = store.application.rooms.get_or_default(self.room_id.clone()); | 1165 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| 1166 | let user_id = self.member.user_id(); | 1166 | let user_id = self.member.user_id(); |
| 1167 | 1167 | ||
| 1168 | let theme = &store.application.settings.theme; | ||
| 1169 | |||
| 1168 | let (color, name) = store.application.settings.get_user_overrides(self.member.user_id()); | 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 | let style = if selected { | 1174 | let style = if selected { |
| 1173 | // Ensure the whole item has the same color when it's selected: | 1175 | // Ensure the whole item has the same color as `user_style` when it's selected: |
| 1174 | style.fg(color).add_modifier(StyleModifier::REVERSED) | 1176 | theme.default.fg(color).add_modifier(StyleModifier::REVERSED) |
| 1175 | } else { | 1177 | } else { |
| 1176 | style | 1178 | theme.default |
| 1177 | }; | 1179 | }; |
| 1178 | let user_style = style.patch(super::config::user_style_from_color(color)); | 1180 | |
| 1179 | let role_style = style.add_modifier(StyleModifier::BOLD); | 1181 | let role_style = style.add_modifier(StyleModifier::BOLD); |
| 1182 | let user_style = style.patch(user_style); | ||
| 1180 | 1183 | ||
| 1181 | let mut spans = vec![]; | 1184 | let mut spans = vec![]; |
| 1182 | let mut tags = vec![]; | 1185 | let mut tags = vec![]; |