| 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 |
|
| 28 |
use std::sync::OnceLock; |
| 29 |
|
| 30 |
use anyhow::{Context, Result}; |
| 31 |
use makeover::{ThemeColors, ThemeDefaults, ThemeMeta, ThemeSelection, Variant}; |
| 32 |
use makeover_tui::{Fidelity, Theme}; |
| 33 |
|
| 34 |
|
| 35 |
const DEFAULT_LIGHT: &str = "makenotwork"; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
const DEFAULT_DARK: &str = "carbonfox"; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
fn embedded() -> &'static [(String, ThemeColors)] { |
| 51 |
static THEMES: OnceLock<Vec<(String, ThemeColors)>> = OnceLock::new(); |
| 52 |
THEMES.get_or_init(|| { |
| 53 |
makeover::embedded_themes() |
| 54 |
.filter_map(|(id, source)| { |
| 55 |
match makeover::parse_theme_str(id, source, false) { |
| 56 |
Ok(colors) => Some((id.to_string(), colors)), |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
Err(e) => { |
| 62 |
tracing::error!(theme = id, error = %e, "embedded theme failed to parse"); |
| 63 |
None |
| 64 |
} |
| 65 |
} |
| 66 |
}) |
| 67 |
.collect() |
| 68 |
}) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
fn available() -> Vec<ThemeMeta> { |
| 73 |
embedded() |
| 74 |
.iter() |
| 75 |
.map(|(_, colors)| colors.meta.clone()) |
| 76 |
.collect() |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
fn defaults() -> ThemeDefaults { |
| 81 |
ThemeDefaults::new(DEFAULT_LIGHT, DEFAULT_DARK) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#[derive(Debug, Clone, Default)] |
| 93 |
pub(crate) struct ClientTerminal { |
| 94 |
|
| 95 |
pub(crate) term: String, |
| 96 |
|
| 97 |
pub(crate) colorterm: String, |
| 98 |
|
| 99 |
pub(crate) colorfgbg: Option<String>, |
| 100 |
} |
| 101 |
|
| 102 |
impl ClientTerminal { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
fn ambient(&self) -> Variant { |
| 112 |
self.colorfgbg |
| 113 |
.as_deref() |
| 114 |
.and_then(|value| value.rsplit(';').next()) |
| 115 |
.and_then(|bg| bg.trim().parse::<u8>().ok()) |
| 116 |
.map_or(Variant::Light, |bg| { |
| 117 |
if bg <= 6 || bg == 8 { |
| 118 |
Variant::Dark |
| 119 |
} else { |
| 120 |
Variant::Light |
| 121 |
} |
| 122 |
}) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
fn fidelity(&self) -> Fidelity { |
| 127 |
Fidelity::from_env(&self.colorterm, &self.term) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
pub(crate) fn selection(theme_id: Option<&str>) -> ThemeSelection { |
| 138 |
ThemeSelection::parse(theme_id) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
pub(crate) fn load(selection: &ThemeSelection, client: &ClientTerminal) -> Result<Theme> { |
| 148 |
let id = selection.resolve(client.ambient(), &defaults(), &available()); |
| 149 |
|
| 150 |
let colors = embedded() |
| 151 |
.iter() |
| 152 |
.find(|(embedded_id, _)| *embedded_id == id) |
| 153 |
.map(|(_, colors)| colors) |
| 154 |
.with_context(|| format!("theme `{id}` is not embedded in this binary"))?; |
| 155 |
|
| 156 |
Theme::from_theme(colors) |
| 157 |
.map(|theme| theme.for_terminal(client.fidelity())) |
| 158 |
.map_err(|e| anyhow::anyhow!("{e}")) |
| 159 |
.with_context(|| format!("theme `{id}` is incomplete")) |
| 160 |
} |
| 161 |
|
| 162 |
#[cfg(test)] |
| 163 |
pub(crate) mod tests { |
| 164 |
use super::*; |
| 165 |
|
| 166 |
|
| 167 |
pub(crate) fn fixed() -> Theme { |
| 168 |
load( |
| 169 |
&ThemeSelection::Fixed(DEFAULT_LIGHT.into()), |
| 170 |
&ClientTerminal::default(), |
| 171 |
) |
| 172 |
.expect("the platform's own theme loads") |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[test] |
| 179 |
fn the_binary_ships_its_own_themes() { |
| 180 |
assert!( |
| 181 |
!embedded().is_empty(), |
| 182 |
"no themes embedded; every session would fail to resolve one", |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#[test] |
| 193 |
fn the_named_defaults_are_embedded_and_complete() { |
| 194 |
for id in [DEFAULT_LIGHT, DEFAULT_DARK] { |
| 195 |
let (_, colors) = embedded() |
| 196 |
.iter() |
| 197 |
.find(|(embedded_id, _)| embedded_id == id) |
| 198 |
.unwrap_or_else(|| panic!("default theme `{id}` is not embedded")); |
| 199 |
assert!( |
| 200 |
Theme::from_theme(colors).is_ok(), |
| 201 |
"default theme `{id}` is incomplete", |
| 202 |
); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
#[test] |
| 208 |
fn following_resolves_to_the_theme_matching_the_terminal() { |
| 209 |
let available = available(); |
| 210 |
assert_eq!( |
| 211 |
ThemeSelection::Follow.resolve(Variant::Dark, &defaults(), &available), |
| 212 |
DEFAULT_DARK, |
| 213 |
); |
| 214 |
assert_eq!( |
| 215 |
ThemeSelection::Follow.resolve(Variant::Light, &defaults(), &available), |
| 216 |
DEFAULT_LIGHT, |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#[test] |
| 223 |
fn an_absent_theme_id_follows_the_terminal() { |
| 224 |
assert_eq!(selection(None), ThemeSelection::Follow); |
| 225 |
assert_eq!( |
| 226 |
selection(Some("nord")), |
| 227 |
ThemeSelection::Fixed("nord".into()) |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
#[test] |
| 233 |
fn the_clients_background_reads_as_a_variant() { |
| 234 |
for (raw, expect) in [ |
| 235 |
("15;0", Variant::Dark), |
| 236 |
("0;15", Variant::Light), |
| 237 |
("15;8", Variant::Dark), |
| 238 |
("15;7", Variant::Light), |
| 239 |
] { |
| 240 |
let client = ClientTerminal { |
| 241 |
colorfgbg: Some(raw.to_string()), |
| 242 |
..ClientTerminal::default() |
| 243 |
}; |
| 244 |
assert_eq!(client.ambient(), expect, "COLORFGBG={raw}"); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
#[test] |
| 250 |
fn a_silent_client_reads_as_light() { |
| 251 |
assert_eq!(ClientTerminal::default().ambient(), Variant::Light); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
#[test] |
| 258 |
fn the_fidelity_comes_from_the_client_not_the_daemon() { |
| 259 |
let console = ClientTerminal { |
| 260 |
term: "linux".to_string(), |
| 261 |
..ClientTerminal::default() |
| 262 |
}; |
| 263 |
assert_eq!(console.fidelity(), Fidelity::Ansi16); |
| 264 |
|
| 265 |
let modern = ClientTerminal { |
| 266 |
term: "xterm-256color".to_string(), |
| 267 |
colorterm: "truecolor".to_string(), |
| 268 |
..ClientTerminal::default() |
| 269 |
}; |
| 270 |
assert_eq!(modern.fidelity(), Fidelity::TrueColor); |
| 271 |
} |
| 272 |
} |
| 273 |
|