Commit 4348ffda60

4348ffda60d1fb907e1cf6e4f6891a962cf711c4

parent: 172d66adb3

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-10-13 03:11 UTC
committer: <noreply@github.com>

Add `rainbow-light` and `rainbow-dark` username palettes (#812)

Layout: unified · split

Cargo.lock +3
@@ -2569,6 +2569,7 @@ dependencies = [
25692569 "nom 8.0.0",
25702570 "notify-rust",
25712571 "open",
2572 "palette",
25722573 "percent-encoding",
25732574 "pretty_assertions",
25742575 "ratatui",
@@ -4271,6 +4272,7 @@ dependencies = [
42714272 "libm",
42724273 "palette_derive",
42734274 "palette_math",
4275 "phf 0.13.1",
42744276]
42754277
42764278[[package]]
@@ -4984,6 +4986,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
49844986checksum = "3274ba0a2c5e1bcad2a2005d20f4dc59dad26b2eb0940fb094500dba4099d57d"
49854987dependencies = [
49864988 "instability",
4989 "palette",
49874990 "ratatui-core",
49884991 "ratatui-crossterm",
49894992 "ratatui-macros",
Cargo.toml +2 −1
@@ -39,6 +39,7 @@ css-color-parser = "0.1.2"
3939dirs = "7.0"
4040edit = "0.1.4"
4141emojis = "0.9"
42palette = "0.7.7"
4243feruca = "0.12"
4344futures = "0.3"
4445gethostname = "1.1"
@@ -55,7 +56,7 @@ mime = "0.3.16"
5556mime_guess = "2.0.4"
5657nom = "8.0"
5758open = "5.0.0"
58ratatui = { version = "0.30.2", features = ["serde"] }
59ratatui = { version = "0.30.2", features = ["palette", "serde"] }
5960regex = "1.5"
6061rpassword = "7.2"
6162serde = "1.0"
docs/iamb.5 +6 −1
@@ -940,7 +940,12 @@ and
940940.Sy modifiers
941941properties, you can also specify a
942942.Sy colors
943array for providing a broad palette of colors from which to style usernames.
943field for providing a broad palette of colors from which to style usernames.
944You can provide an array of specific colors, which you may want if you have a list of specific colors that work for your theme, but you can also set it to
945.Sy "rainbow-light"
946or
947.Sy "rainbow-dark"
948to use a large light- or dark-colored rainbow palette for usernames.
944949.Pp
945950By default, this is a selection of 13 standard ANSI colors that should look okay on both light and dark backgrounds.
946951You can set this array to
src/config/theme.rs +63 −18
@@ -3,6 +3,7 @@ use std::collections::hash_map::DefaultHasher;
33use std::hash::{Hash, Hasher};
44
55use anyhow::Context;
6use palette::Hsl;
67use ratatui::widgets::BorderType;
78use serde::de::Error as SerdeError;
89use serde::de::Visitor;
@@ -93,7 +94,7 @@ pub fn default_theme() -> Theme {
9394 ..Default::default()
9495 },
9596 users: ThemeUsers {
96 colors: Some(vec![
97 colors: Some(UserColors::Palette(vec![
9798 Color::Blue,
9899 Color::Cyan,
99100 Color::Green,
@@ -107,7 +108,7 @@ pub fn default_theme() -> Theme {
107108 Color::Red,
108109 Color::Reset,
109110 Color::Yellow,
110 ]),
111 ])),
111112 stylable: Stylable::with_modifiers(StyleModifier::BOLD),
112113 },
113114 ..Default::default()
@@ -1042,9 +1043,61 @@ pub struct ThemeWindowsValues {
10421043 pub title: Style,
10431044}
10441045
1046#[derive(Clone, Debug, Default, Deserialize)]
1047#[serde(rename_all = "kebab-case")]
1048enum UserColors {
1049 RainbowDark,
1050 RainbowLight,
1051 #[default]
1052 #[serde(skip)]
1053 Unset,
1054 #[serde(untagged)]
1055 Palette(Vec<Color>),
1056}
1057
1058impl 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
10451098#[derive(Clone, Debug, Default, Deserialize)]
10461099struct ThemeUsers {
1047 colors: Option<Vec<Color>>,
1100 colors: Option<UserColors>,
10481101 #[serde(flatten)]
10491102 stylable: Stylable,
10501103}
@@ -1058,8 +1111,8 @@ impl ThemeUsers {
10581111 }
10591112
10601113 fn values(self, base: Style) -> ThemeUsersValues {
1061 let colors = self.colors.unwrap_or_default();
10621114 let style = base.patch(self.stylable);
1115 let colors = self.colors.unwrap_or_default();
10631116
10641117 ThemeUsersValues { colors, style }
10651118 }
@@ -1067,20 +1120,13 @@ impl ThemeUsers {
10671120
10681121#[derive(Clone, Debug, Default)]
10691122pub struct ThemeUsersValues {
1070 colors: Vec<Color>,
1123 colors: UserColors,
10711124 style: Style,
10721125}
10731126
10741127impl ThemeUsersValues {
10751128 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)
10841130 }
10851131
10861132 pub fn style(&self, user_id: &str, explicit: Option<Color>) -> Style {
@@ -1089,12 +1135,11 @@ impl ThemeUsersValues {
10891135 if let Some(c) = explicit {
10901136 // Use explicit color from config file:
10911137 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)
10951141 } else {
1096 // Hash on user ID to pick a color from the array:
1097 style.fg(self.color(user_id))
1142 style
10981143 }
10991144 }
11001145}