| @@ -3,6 +3,7 @@ use std::collections::hash_map::DefaultHasher; |
| 3 | 3 | use std::hash::{Hash, Hasher}; |
| 4 | 4 | |
| 5 | 5 | use anyhow::Context; |
| 6 | use palette::Hsl; |
| 6 | 7 | use ratatui::widgets::BorderType; |
| 7 | 8 | use serde::de::Error as SerdeError; |
| 8 | 9 | use serde::de::Visitor; |
| @@ -93,7 +94,7 @@ pub fn default_theme() -> Theme { |
| 93 | 94 | ..Default::default() |
| 94 | 95 | }, |
| 95 | 96 | users: ThemeUsers { |
| 96 | | colors: Some(vec![ |
| 97 | colors: Some(UserColors::Palette(vec![ |
| 97 | 98 | Color::Blue, |
| 98 | 99 | Color::Cyan, |
| 99 | 100 | Color::Green, |
| @@ -107,7 +108,7 @@ pub fn default_theme() -> Theme { |
| 107 | 108 | Color::Red, |
| 108 | 109 | Color::Reset, |
| 109 | 110 | Color::Yellow, |
| 110 | | ]), |
| 111 | ])), |
| 111 | 112 | stylable: Stylable::with_modifiers(StyleModifier::BOLD), |
| 112 | 113 | }, |
| 113 | 114 | ..Default::default() |
| @@ -1042,9 +1043,61 @@ pub struct ThemeWindowsValues { |
| 1042 | 1043 | pub title: Style, |
| 1043 | 1044 | } |
| 1044 | 1045 | |
| 1046 | #[derive(Clone, Debug, Default, Deserialize)] |
| 1047 | #[serde(rename_all = "kebab-case")] |
| 1048 | enum UserColors { |
| 1049 | RainbowDark, |
| 1050 | RainbowLight, |
| 1051 | #[default] |
| 1052 | #[serde(skip)] |
| 1053 | Unset, |
| 1054 | #[serde(untagged)] |
| 1055 | Palette(Vec<Color>), |
| 1056 | } |
| 1057 | |
| 1058 | impl UserColors { |
| 1059 | fn hash(user_id: &str) -> u64 { |
| 1060 | let mut hasher = DefaultHasher::new(); |
| 1061 | user_id.hash(&mut hasher); |
| 1062 | hasher.finish() |
| 1063 | } |
| 1064 | |
| 1065 | fn hash_to_color(hash: f64, l_base: f64) -> Color { |
| 1066 | let h = hash % 360.0; |
| 1067 | let s = hash % 60.0 + 40.0; |
| 1068 | let l = hash % 35.0 + l_base; |
| 1069 | let hsl: Hsl = Hsl::new(h as f32, s as f32 / 100.0, l as f32 / 100.0); |
| 1070 | Color::from_hsl(hsl) |
| 1071 | } |
| 1072 | |
| 1073 | fn color(&self, user_id: &str) -> Option<Color> { |
| 1074 | let color = match self { |
| 1075 | Self::Unset => return None, |
| 1076 | Self::Palette(colors) => { |
| 1077 | if colors.is_empty() { |
| 1078 | return None; |
| 1079 | } else { |
| 1080 | let hash = Self::hash(user_id) as usize; |
| 1081 | colors[hash % colors.len()] |
| 1082 | } |
| 1083 | }, |
| 1084 | Self::RainbowDark => { |
| 1085 | let hash = Self::hash(user_id); |
| 1086 | Self::hash_to_color(hash as f64, 15.0) |
| 1087 | }, |
| 1088 | Self::RainbowLight => { |
| 1089 | let hash = Self::hash(user_id); |
| 1090 | Self::hash_to_color(hash as f64, 50.0) |
| 1091 | }, |
| 1092 | }; |
| 1093 | |
| 1094 | Some(color) |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1045 | 1098 | #[derive(Clone, Debug, Default, Deserialize)] |
| 1046 | 1099 | struct ThemeUsers { |
| 1047 | | colors: Option<Vec<Color>>, |
| 1100 | colors: Option<UserColors>, |
| 1048 | 1101 | #[serde(flatten)] |
| 1049 | 1102 | stylable: Stylable, |
| 1050 | 1103 | } |
| @@ -1058,8 +1111,8 @@ impl ThemeUsers { |
| 1058 | 1111 | } |
| 1059 | 1112 | |
| 1060 | 1113 | fn values(self, base: Style) -> ThemeUsersValues { |
| 1061 | | let colors = self.colors.unwrap_or_default(); |
| 1062 | 1114 | let style = base.patch(self.stylable); |
| 1115 | let colors = self.colors.unwrap_or_default(); |
| 1063 | 1116 | |
| 1064 | 1117 | ThemeUsersValues { colors, style } |
| 1065 | 1118 | } |
| @@ -1067,20 +1120,13 @@ impl ThemeUsers { |
| 1067 | 1120 | |
| 1068 | 1121 | #[derive(Clone, Debug, Default)] |
| 1069 | 1122 | pub struct ThemeUsersValues { |
| 1070 | | colors: Vec<Color>, |
| 1123 | colors: UserColors, |
| 1071 | 1124 | style: Style, |
| 1072 | 1125 | } |
| 1073 | 1126 | |
| 1074 | 1127 | impl ThemeUsersValues { |
| 1075 | 1128 | pub fn color(&self, user_id: &str) -> Color { |
| 1076 | | if self.colors.is_empty() { |
| 1077 | | return self.style.fg.unwrap_or(Color::Reset); |
| 1078 | | } |
| 1079 | | |
| 1080 | | let mut hasher = DefaultHasher::new(); |
| 1081 | | user_id.hash(&mut hasher); |
| 1082 | | let color = hasher.finish() as usize % self.colors.len(); |
| 1083 | | self.colors[color] |
| 1129 | self.colors.color(user_id).or(self.style.fg).unwrap_or(Color::Reset) |
| 1084 | 1130 | } |
| 1085 | 1131 | |
| 1086 | 1132 | pub fn style(&self, user_id: &str, explicit: Option<Color>) -> Style { |
| @@ -1089,12 +1135,11 @@ impl ThemeUsersValues { |
| 1089 | 1135 | if let Some(c) = explicit { |
| 1090 | 1136 | // Use explicit color from config file: |
| 1091 | 1137 | style.fg(c) |
| 1092 | | } else if self.colors.is_empty() { |
| 1093 | | // User has explicitly set an empty array: |
| 1094 | | style |
| 1138 | } else if let Some(c) = self.colors.color(user_id) { |
| 1139 | // Apply the user color we hashed to: |
| 1140 | style.fg(c) |
| 1095 | 1141 | } else { |
| 1096 | | // Hash on user ID to pick a color from the array: |
| 1097 | | style.fg(self.color(user_id)) |
| 1142 | style |
| 1098 | 1143 | } |
| 1099 | 1144 | } |
| 1100 | 1145 | } |