|
| 1 | +use yal_core::Theme; |
| 2 | + |
| 3 | +#[derive(Copy, Clone, Debug)] |
| 4 | +pub struct ThemeRef { |
| 5 | + pub name: &'static str, |
| 6 | + pub bg_color: &'static str, |
| 7 | + pub fg_color: &'static str, |
| 8 | + pub bg_font_color: &'static str, |
| 9 | + pub fg_font_color: &'static str, |
| 10 | +} |
| 11 | + |
| 12 | +impl ThemeRef { |
| 13 | + pub const fn new( |
| 14 | + name: &'static str, |
| 15 | + bg_color: &'static str, |
| 16 | + fg_color: &'static str, |
| 17 | + bg_font_color: &'static str, |
| 18 | + fg_font_color: &'static str, |
| 19 | + ) -> Self { |
| 20 | + Self { |
| 21 | + name, |
| 22 | + bg_color, |
| 23 | + fg_color, |
| 24 | + bg_font_color, |
| 25 | + fg_font_color, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn to_owned(self) -> Theme { |
| 30 | + Theme { |
| 31 | + name: Some(self.name.to_string()), |
| 32 | + bg_color: Some(self.bg_color.to_string()), |
| 33 | + fg_color: Some(self.fg_color.to_string()), |
| 34 | + bg_font_color: Some(self.bg_font_color.to_string()), |
| 35 | + fg_font_color: Some(self.fg_font_color.to_string()), |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl From<ThemeRef> for Theme { |
| 41 | + fn from(t: ThemeRef) -> Self { |
| 42 | + t.to_owned() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/* ------------------------------ Theme constants ------------------------------ */ |
| 47 | +/* Notes: |
| 48 | + * - `fg_color` is treated as the *highlight background*. |
| 49 | + * - `bg_font_color` is normal text on `bg_color`. |
| 50 | + * - `fg_font_color` is text on the highlighted row (`fg_color`). |
| 51 | + * Tweak to taste per your UI. |
| 52 | + */ |
| 53 | + |
| 54 | +// Monokai (classic) |
| 55 | +pub const MONOKAI: ThemeRef = ThemeRef::new( |
| 56 | + "monokai", "#272822", // bg |
| 57 | + "#49483E", // highlight bg |
| 58 | + "#F8F8F2", // text on bg |
| 59 | + "#FFFFFF", // text on highlight |
| 60 | +); |
| 61 | + |
| 62 | +// Dracula |
| 63 | +pub const DRACULA: ThemeRef = ThemeRef::new("dracula", "#282A36", "#44475A", "#F8F8F2", "#FFFFFF"); |
| 64 | + |
| 65 | +// Catppuccin — Latte (light) |
| 66 | +pub const CATPPUCCIN_LATTE: ThemeRef = ThemeRef::new( |
| 67 | + "catppuccin-latte", |
| 68 | + "#EFF1F5", // base |
| 69 | + "#CCD0DA", // surface2-ish as highlight |
| 70 | + "#4C4F69", // text |
| 71 | + "#1E1E2E", // strong text on highlight |
| 72 | +); |
| 73 | + |
| 74 | +// Catppuccin — Frappé (dark) |
| 75 | +pub const CATPPUCCIN_FRAPPE: ThemeRef = ThemeRef::new( |
| 76 | + "catppuccin-frappe", |
| 77 | + "#303446", // base |
| 78 | + "#414559", // surface1 |
| 79 | + "#C6D0F5", // text |
| 80 | + "#E6E9EF", // text on highlight |
| 81 | +); |
| 82 | + |
| 83 | +// Catppuccin — Macchiato (dark) |
| 84 | +pub const CATPPUCCIN_MACCHIATO: ThemeRef = ThemeRef::new( |
| 85 | + "catppuccin-macchiato", |
| 86 | + "#24273A", |
| 87 | + "#363A4F", |
| 88 | + "#CAD3F5", |
| 89 | + "#E8E8E8", |
| 90 | +); |
| 91 | + |
| 92 | +// Catppuccin — Mocha (dark) |
| 93 | +pub const CATPPUCCIN_MOCHA: ThemeRef = ThemeRef::new( |
| 94 | + "catppuccin-mocha", |
| 95 | + "#1E1E2E", |
| 96 | + "#313244", |
| 97 | + "#CDD6F4", |
| 98 | + "#E6E6E6", |
| 99 | +); |
| 100 | + |
| 101 | +// Solarized Dark |
| 102 | +pub const SOLARIZED_DARK: ThemeRef = |
| 103 | + ThemeRef::new("solarized-dark", "#002B36", "#073642", "#93A1A1", "#FDF6E3"); |
| 104 | + |
| 105 | +// Solarized Light |
| 106 | +pub const SOLARIZED_LIGHT: ThemeRef = ThemeRef::new( |
| 107 | + "solarized-light", |
| 108 | + "#FDF6E3", |
| 109 | + "#EEE8D5", |
| 110 | + "#586E75", |
| 111 | + "#073642", |
| 112 | +); |
| 113 | + |
| 114 | +// Gruvbox Dark (hard-ish) |
| 115 | +pub const GRUVBOX_DARK: ThemeRef = |
| 116 | + ThemeRef::new("gruvbox-dark", "#282828", "#3C3836", "#EBDBB2", "#FBF1C7"); |
| 117 | + |
| 118 | +// Gruvbox Light |
| 119 | +pub const GRUVBOX_LIGHT: ThemeRef = |
| 120 | + ThemeRef::new("gruvbox-light", "#FBF1C7", "#EBDBB2", "#3C3836", "#282828"); |
| 121 | + |
| 122 | +// Nord |
| 123 | +pub const NORD: ThemeRef = ThemeRef::new("nord", "#2E3440", "#3B4252", "#D8DEE9", "#ECEFF4"); |
| 124 | + |
| 125 | +// One Dark |
| 126 | +pub const ONE_DARK: ThemeRef = |
| 127 | + ThemeRef::new("one-dark", "#282C34", "#3E4451", "#ABB2BF", "#FFFFFF"); |
| 128 | + |
| 129 | +// Tokyo Night (classic) |
| 130 | +pub const TOKYO_NIGHT: ThemeRef = |
| 131 | + ThemeRef::new("tokyo-night", "#1A1B26", "#2F334D", "#C0CAF5", "#E6E6E6"); |
| 132 | + |
| 133 | +// Tokyo Night Storm |
| 134 | +pub const TOKYO_NIGHT_STORM: ThemeRef = ThemeRef::new( |
| 135 | + "tokyo-night-storm", |
| 136 | + "#24283B", |
| 137 | + "#283457", |
| 138 | + "#C0CAF5", |
| 139 | + "#E6E6E6", |
| 140 | +); |
| 141 | + |
| 142 | +/* --------------------------------- Registry --------------------------------- */ |
| 143 | + |
| 144 | +pub const ALL: &[ThemeRef] = &[ |
| 145 | + MONOKAI, |
| 146 | + DRACULA, |
| 147 | + CATPPUCCIN_LATTE, |
| 148 | + CATPPUCCIN_FRAPPE, |
| 149 | + CATPPUCCIN_MACCHIATO, |
| 150 | + CATPPUCCIN_MOCHA, |
| 151 | + SOLARIZED_DARK, |
| 152 | + SOLARIZED_LIGHT, |
| 153 | + GRUVBOX_DARK, |
| 154 | + GRUVBOX_LIGHT, |
| 155 | + NORD, |
| 156 | + ONE_DARK, |
| 157 | + TOKYO_NIGHT, |
| 158 | + TOKYO_NIGHT_STORM, |
| 159 | +]; |
| 160 | + |
| 161 | +/// Case-insensitive lookup. Supports a few aliases. |
| 162 | +pub fn by_name(name: &str) -> Option<ThemeRef> { |
| 163 | + let n = name.trim().to_lowercase(); |
| 164 | + let normalized = match n.as_str() { |
| 165 | + // aliases |
| 166 | + "one" | "onedark" | "one-dark-pro" => "one-dark", |
| 167 | + "tokyo" | "tokyonight" => "tokyo-night", |
| 168 | + "tokyo-storm" | "tokyonight-storm" => "tokyo-night-storm", |
| 169 | + "catppuccin" => "catppuccin-mocha", // default flavor |
| 170 | + "catppuccin-latte" | "latte" => "catppuccin-latte", |
| 171 | + "catppuccin-frappe" | "frappe" => "catppuccin-frappe", |
| 172 | + "catppuccin-macchiato" | "macchiato" => "catppuccin-macchiato", |
| 173 | + "catppuccin-mocha" | "mocha" => "catppuccin-mocha", |
| 174 | + other => other, |
| 175 | + }; |
| 176 | + |
| 177 | + ALL.iter().copied().find(|t| t.name == normalized) |
| 178 | +} |
| 179 | + |
| 180 | +/// Owned theme list (useful for config UIs). |
| 181 | +pub fn list_owned() -> Vec<Theme> { |
| 182 | + ALL.iter().copied().map(Theme::from).collect() |
| 183 | +} |
| 184 | + |
| 185 | +/* ----------------------------------- Tests ---------------------------------- */ |
| 186 | + |
| 187 | +#[cfg(test)] |
| 188 | +mod tests { |
| 189 | + use super::*; |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn from_ref_to_owned() { |
| 193 | + let t: Theme = MONOKAI.into(); |
| 194 | + assert_eq!(t.name.as_deref(), Some("monokai")); |
| 195 | + assert_eq!(t.bg_color.as_deref(), Some("#272822")); |
| 196 | + assert_eq!(t.fg_color.as_deref(), Some("#49483E")); |
| 197 | + } |
| 198 | + |
| 199 | + #[test] |
| 200 | + fn lookup_aliases() { |
| 201 | + assert_eq!(by_name("OneDark").unwrap().name, "one-dark"); |
| 202 | + assert_eq!(by_name("catppuccin").unwrap().name, "catppuccin-mocha"); |
| 203 | + assert_eq!( |
| 204 | + by_name("tokyonight-storm").unwrap().name, |
| 205 | + "tokyo-night-storm" |
| 206 | + ); |
| 207 | + } |
| 208 | +} |
0 commit comments