| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
use makeover::{Rgb, ThemeColors}; |
| 28 |
use ratatui::style::Color; |
| 29 |
|
| 30 |
|
| 31 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 32 |
pub enum Mode { |
| 33 |
Light, |
| 34 |
Dark, |
| 35 |
HighContrast, |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#[derive(Debug, Clone, Copy)] |
| 46 |
#[non_exhaustive] |
| 47 |
pub struct Theme { |
| 48 |
pub mode: Mode, |
| 49 |
|
| 50 |
pub surface_page: Color, |
| 51 |
pub surface_raised: Color, |
| 52 |
pub surface_sunken: Color, |
| 53 |
pub surface_overlay: Color, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
pub surface_well: Option<Color>, |
| 69 |
|
| 70 |
pub content_primary: Color, |
| 71 |
pub content_secondary: Color, |
| 72 |
pub content_muted: Color, |
| 73 |
|
| 74 |
pub action_primary: Color, |
| 75 |
|
| 76 |
pub status_danger: Color, |
| 77 |
pub status_success: Color, |
| 78 |
pub status_warning: Color, |
| 79 |
pub status_info: Color, |
| 80 |
|
| 81 |
|
| 82 |
pub line_border: Color, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub border_strong: Color, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub bevel_light: Color, |
| 97 |
pub bevel_dark: Color, |
| 98 |
|
| 99 |
pub category: [Color; 6], |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[derive(Debug, Clone)] |
| 107 |
pub enum ThemeError { |
| 108 |
MissingKey(&'static str), |
| 109 |
InvalidHex { key: &'static str, value: String }, |
| 110 |
} |
| 111 |
|
| 112 |
impl std::fmt::Display for ThemeError { |
| 113 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 114 |
match self { |
| 115 |
Self::MissingKey(k) => write!(f, "theme missing required key `{k}`"), |
| 116 |
Self::InvalidHex { key, value } => { |
| 117 |
write!(f, "theme key `{key}` has invalid hex value `{value}`") |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl std::error::Error for ThemeError {} |
| 124 |
|
| 125 |
impl Theme { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
pub fn from_theme(theme: &ThemeColors) -> Result<Self, ThemeError> { |
| 134 |
let authored = |key: &'static str| -> Result<Rgb, ThemeError> { |
| 135 |
let hex = theme.colors.get(key).ok_or(ThemeError::MissingKey(key))?; |
| 136 |
Rgb::from_hex(hex).ok_or_else(|| ThemeError::InvalidHex { |
| 137 |
key, |
| 138 |
value: hex.clone(), |
| 139 |
}) |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
let resolved = makeover::resolve(theme); |
| 147 |
let derived = |key: &'static str| -> Result<Rgb, ThemeError> { |
| 148 |
let hex = resolved.hex(key).ok_or(ThemeError::MissingKey(key))?; |
| 149 |
Rgb::from_hex(hex).ok_or_else(|| ThemeError::InvalidHex { |
| 150 |
key, |
| 151 |
value: hex.to_string(), |
| 152 |
}) |
| 153 |
}; |
| 154 |
|
| 155 |
let mode = match theme.meta.variant.as_str() { |
| 156 |
"dark" => Mode::Dark, |
| 157 |
"high-contrast" => Mode::HighContrast, |
| 158 |
_ => Mode::Light, |
| 159 |
}; |
| 160 |
|
| 161 |
Ok(Self { |
| 162 |
mode, |
| 163 |
|
| 164 |
surface_page: rgb(authored("surface.page")?), |
| 165 |
surface_raised: rgb(authored("surface.raised")?), |
| 166 |
surface_sunken: rgb(authored("surface.sunken")?), |
| 167 |
surface_overlay: rgb(authored("surface.overlay")?), |
| 168 |
surface_well: resolved |
| 169 |
.hex("surface-well") |
| 170 |
.and_then(Rgb::from_hex) |
| 171 |
.map(rgb), |
| 172 |
|
| 173 |
content_primary: rgb(authored("content.primary")?), |
| 174 |
content_secondary: rgb(authored("content.secondary")?), |
| 175 |
content_muted: rgb(authored("content.muted")?), |
| 176 |
|
| 177 |
action_primary: rgb(authored("action.primary")?), |
| 178 |
|
| 179 |
status_danger: rgb(authored("status.danger")?), |
| 180 |
status_success: rgb(authored("status.success")?), |
| 181 |
status_warning: rgb(authored("status.warning")?), |
| 182 |
status_info: rgb(authored("status.info")?), |
| 183 |
|
| 184 |
line_border: rgb(authored("line.border")?), |
| 185 |
border_strong: rgb(derived("border-strong")?), |
| 186 |
|
| 187 |
bevel_light: rgb(derived("bevel-light")?), |
| 188 |
bevel_dark: rgb(derived("bevel-dark")?), |
| 189 |
|
| 190 |
category: [ |
| 191 |
rgb(authored("category.one")?), |
| 192 |
rgb(authored("category.two")?), |
| 193 |
rgb(authored("category.three")?), |
| 194 |
rgb(authored("category.four")?), |
| 195 |
rgb(authored("category.five")?), |
| 196 |
rgb(authored("category.six")?), |
| 197 |
], |
| 198 |
}) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
#[must_use] |
| 209 |
pub const fn palette(&self, fidelity: crate::Fidelity) -> crate::Palette { |
| 210 |
crate::Palette { |
| 211 |
page: self.surface_page, |
| 212 |
raised: self.surface_raised, |
| 213 |
overlay: self.surface_overlay, |
| 214 |
well: self.surface_well, |
| 215 |
bevel_light: self.bevel_light, |
| 216 |
bevel_dark: self.bevel_dark, |
| 217 |
fidelity, |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
fn rgb(c: Rgb) -> Color { |
| 223 |
Color::Rgb(c.r, c.g, c.b) |
| 224 |
} |
| 225 |
|
| 226 |
#[cfg(test)] |
| 227 |
mod tests { |
| 228 |
use super::*; |
| 229 |
|
| 230 |
fn bundled(id: &str) -> ThemeColors { |
| 231 |
let dir = makeover::bundled_themes_dir().expect("makeover ships themes"); |
| 232 |
makeover::load_theme(&[(dir, false)], id).expect("bundled theme loads") |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn every_bundled_theme_resolves() { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
let dir = makeover::bundled_themes_dir().expect("makeover ships themes"); |
| 241 |
let metas = makeover::list_themes_from_dirs(&[(dir, false)]); |
| 242 |
assert!( |
| 243 |
!metas.is_empty(), |
| 244 |
"makeover shipped no themes to test against" |
| 245 |
); |
| 246 |
for meta in &metas { |
| 247 |
let colors = bundled(&meta.id); |
| 248 |
assert!( |
| 249 |
Theme::from_theme(&colors).is_ok(), |
| 250 |
"bundled theme `{}` failed to resolve", |
| 251 |
meta.id |
| 252 |
); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn a_missing_intent_names_the_key_it_wanted() { |
| 258 |
let mut colors = bundled("goingson"); |
| 259 |
colors.colors.remove("content.muted"); |
| 260 |
match Theme::from_theme(&colors) { |
| 261 |
Err(ThemeError::MissingKey(k)) => assert_eq!(k, "content.muted"), |
| 262 |
other => panic!("expected MissingKey(content.muted), got {other:?}"), |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn an_unparseable_hex_names_the_key_and_the_value() { |
| 268 |
let mut colors = bundled("goingson"); |
| 269 |
colors |
| 270 |
.colors |
| 271 |
.insert("content.muted".into(), "not-a-colour".into()); |
| 272 |
match Theme::from_theme(&colors) { |
| 273 |
Err(ThemeError::InvalidHex { key, value }) => { |
| 274 |
assert_eq!(key, "content.muted"); |
| 275 |
assert_eq!(value, "not-a-colour"); |
| 276 |
} |
| 277 |
other => panic!("expected InvalidHex, got {other:?}"), |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn the_palette_takes_the_well_and_not_the_sunken_surface() { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
let colors = bundled("goingson"); |
| 287 |
let theme = Theme::from_theme(&colors).expect("resolves"); |
| 288 |
let palette = theme.palette(crate::Fidelity::TrueColor); |
| 289 |
assert_eq!(palette.well, theme.surface_well); |
| 290 |
assert_ne!(palette.well, Some(theme.surface_sunken)); |
| 291 |
} |
| 292 |
} |
| 293 |
|