1pub mod palette;
3
4pub use palette::Palette;
5
6use crate::Color;
7
8use std::borrow::Cow;
9use std::fmt;
10use std::sync::Arc;
11
12#[derive(Debug, Clone, PartialEq)]
14pub enum Theme {
15 Light,
17 Dark,
19 Dracula,
21 Nord,
23 SolarizedLight,
25 SolarizedDark,
27 GruvboxLight,
29 GruvboxDark,
31 CatppuccinLatte,
33 CatppuccinFrappe,
35 CatppuccinMacchiato,
37 CatppuccinMocha,
39 TokyoNight,
41 TokyoNightStorm,
43 TokyoNightLight,
45 KanagawaWave,
47 KanagawaDragon,
49 KanagawaLotus,
51 Moonfly,
53 Nightfly,
55 Oxocarbon,
57 Ferra,
59 Custom(Arc<Custom>),
61}
62
63impl Theme {
64 pub const ALL: &'static [Self] = &[
66 Self::Light,
67 Self::Dark,
68 Self::Dracula,
69 Self::Nord,
70 Self::SolarizedLight,
71 Self::SolarizedDark,
72 Self::GruvboxLight,
73 Self::GruvboxDark,
74 Self::CatppuccinLatte,
75 Self::CatppuccinFrappe,
76 Self::CatppuccinMacchiato,
77 Self::CatppuccinMocha,
78 Self::TokyoNight,
79 Self::TokyoNightStorm,
80 Self::TokyoNightLight,
81 Self::KanagawaWave,
82 Self::KanagawaDragon,
83 Self::KanagawaLotus,
84 Self::Moonfly,
85 Self::Nightfly,
86 Self::Oxocarbon,
87 Self::Ferra,
88 ];
89
90 pub fn custom(
92 name: impl Into<Cow<'static, str>>,
93 palette: Palette,
94 ) -> Self {
95 Self::custom_with_fn(name, palette, palette::Extended::generate)
96 }
97
98 pub fn custom_with_fn(
101 name: impl Into<Cow<'static, str>>,
102 palette: Palette,
103 generate: impl FnOnce(Palette) -> palette::Extended,
104 ) -> Self {
105 Self::Custom(Arc::new(Custom::with_fn(name, palette, generate)))
106 }
107
108 pub fn palette(&self) -> Palette {
110 match self {
111 Self::Light => Palette::LIGHT,
112 Self::Dark => Palette::DARK,
113 Self::Dracula => Palette::DRACULA,
114 Self::Nord => Palette::NORD,
115 Self::SolarizedLight => Palette::SOLARIZED_LIGHT,
116 Self::SolarizedDark => Palette::SOLARIZED_DARK,
117 Self::GruvboxLight => Palette::GRUVBOX_LIGHT,
118 Self::GruvboxDark => Palette::GRUVBOX_DARK,
119 Self::CatppuccinLatte => Palette::CATPPUCCIN_LATTE,
120 Self::CatppuccinFrappe => Palette::CATPPUCCIN_FRAPPE,
121 Self::CatppuccinMacchiato => Palette::CATPPUCCIN_MACCHIATO,
122 Self::CatppuccinMocha => Palette::CATPPUCCIN_MOCHA,
123 Self::TokyoNight => Palette::TOKYO_NIGHT,
124 Self::TokyoNightStorm => Palette::TOKYO_NIGHT_STORM,
125 Self::TokyoNightLight => Palette::TOKYO_NIGHT_LIGHT,
126 Self::KanagawaWave => Palette::KANAGAWA_WAVE,
127 Self::KanagawaDragon => Palette::KANAGAWA_DRAGON,
128 Self::KanagawaLotus => Palette::KANAGAWA_LOTUS,
129 Self::Moonfly => Palette::MOONFLY,
130 Self::Nightfly => Palette::NIGHTFLY,
131 Self::Oxocarbon => Palette::OXOCARBON,
132 Self::Ferra => Palette::FERRA,
133 Self::Custom(custom) => custom.palette,
134 }
135 }
136
137 pub fn extended_palette(&self) -> &palette::Extended {
139 match self {
140 Self::Light => &palette::EXTENDED_LIGHT,
141 Self::Dark => &palette::EXTENDED_DARK,
142 Self::Dracula => &palette::EXTENDED_DRACULA,
143 Self::Nord => &palette::EXTENDED_NORD,
144 Self::SolarizedLight => &palette::EXTENDED_SOLARIZED_LIGHT,
145 Self::SolarizedDark => &palette::EXTENDED_SOLARIZED_DARK,
146 Self::GruvboxLight => &palette::EXTENDED_GRUVBOX_LIGHT,
147 Self::GruvboxDark => &palette::EXTENDED_GRUVBOX_DARK,
148 Self::CatppuccinLatte => &palette::EXTENDED_CATPPUCCIN_LATTE,
149 Self::CatppuccinFrappe => &palette::EXTENDED_CATPPUCCIN_FRAPPE,
150 Self::CatppuccinMacchiato => {
151 &palette::EXTENDED_CATPPUCCIN_MACCHIATO
152 }
153 Self::CatppuccinMocha => &palette::EXTENDED_CATPPUCCIN_MOCHA,
154 Self::TokyoNight => &palette::EXTENDED_TOKYO_NIGHT,
155 Self::TokyoNightStorm => &palette::EXTENDED_TOKYO_NIGHT_STORM,
156 Self::TokyoNightLight => &palette::EXTENDED_TOKYO_NIGHT_LIGHT,
157 Self::KanagawaWave => &palette::EXTENDED_KANAGAWA_WAVE,
158 Self::KanagawaDragon => &palette::EXTENDED_KANAGAWA_DRAGON,
159 Self::KanagawaLotus => &palette::EXTENDED_KANAGAWA_LOTUS,
160 Self::Moonfly => &palette::EXTENDED_MOONFLY,
161 Self::Nightfly => &palette::EXTENDED_NIGHTFLY,
162 Self::Oxocarbon => &palette::EXTENDED_OXOCARBON,
163 Self::Ferra => &palette::EXTENDED_FERRA,
164 Self::Custom(custom) => &custom.extended,
165 }
166 }
167}
168
169impl fmt::Display for Theme {
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 f.write_str(self.name())
172 }
173}
174
175#[derive(Debug, Clone, PartialEq)]
177pub struct Custom {
178 name: Cow<'static, str>,
179 palette: Palette,
180 extended: palette::Extended,
181}
182
183impl Custom {
184 pub fn new(name: String, palette: Palette) -> Self {
186 Self::with_fn(name, palette, palette::Extended::generate)
187 }
188
189 pub fn with_fn(
192 name: impl Into<Cow<'static, str>>,
193 palette: Palette,
194 generate: impl FnOnce(Palette) -> palette::Extended,
195 ) -> Self {
196 Self {
197 name: name.into(),
198 palette,
199 extended: generate(palette),
200 }
201 }
202}
203
204impl fmt::Display for Custom {
205 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206 write!(f, "{}", self.name)
207 }
208}
209
210#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
212pub enum Mode {
213 #[default]
215 None,
216 Light,
218 Dark,
220}
221
222#[derive(Debug, Clone, Copy, PartialEq)]
224pub struct Style {
225 pub background_color: Color,
227
228 pub text_color: Color,
230}
231
232pub trait Base {
234 fn default(preference: Mode) -> Self;
236
237 fn mode(&self) -> Mode;
239
240 fn base(&self) -> Style;
242
243 fn palette(&self) -> Option<Palette>;
249
250 fn name(&self) -> &str;
255}
256
257impl Base for Theme {
258 fn default(preference: Mode) -> Self {
259 use std::env;
260 use std::sync::OnceLock;
261
262 static SYSTEM: OnceLock<Option<Theme>> = OnceLock::new();
263
264 let system = SYSTEM.get_or_init(|| {
265 let name = env::var("ICED_THEME").ok()?;
266
267 Theme::ALL
268 .iter()
269 .find(|theme| theme.to_string() == name)
270 .cloned()
271 });
272
273 if let Some(system) = system {
274 return system.clone();
275 }
276
277 match preference {
278 Mode::None | Mode::Light => Self::Light,
279 Mode::Dark => Self::Dark,
280 }
281 }
282
283 fn mode(&self) -> Mode {
284 if self.extended_palette().is_dark {
285 Mode::Dark
286 } else {
287 Mode::Light
288 }
289 }
290
291 fn base(&self) -> Style {
292 default(self)
293 }
294
295 fn palette(&self) -> Option<Palette> {
296 Some(self.palette())
297 }
298
299 fn name(&self) -> &str {
300 match self {
301 Self::Light => "Light",
302 Self::Dark => "Dark",
303 Self::Dracula => "Dracula",
304 Self::Nord => "Nord",
305 Self::SolarizedLight => "Solarized Light",
306 Self::SolarizedDark => "Solarized Dark",
307 Self::GruvboxLight => "Gruvbox Light",
308 Self::GruvboxDark => "Gruvbox Dark",
309 Self::CatppuccinLatte => "Catppuccin Latte",
310 Self::CatppuccinFrappe => "Catppuccin Frappé",
311 Self::CatppuccinMacchiato => "Catppuccin Macchiato",
312 Self::CatppuccinMocha => "Catppuccin Mocha",
313 Self::TokyoNight => "Tokyo Night",
314 Self::TokyoNightStorm => "Tokyo Night Storm",
315 Self::TokyoNightLight => "Tokyo Night Light",
316 Self::KanagawaWave => "Kanagawa Wave",
317 Self::KanagawaDragon => "Kanagawa Dragon",
318 Self::KanagawaLotus => "Kanagawa Lotus",
319 Self::Moonfly => "Moonfly",
320 Self::Nightfly => "Nightfly",
321 Self::Oxocarbon => "Oxocarbon",
322 Self::Ferra => "Ferra",
323 Self::Custom(custom) => &custom.name,
324 }
325 }
326}
327
328pub fn default(theme: &Theme) -> Style {
330 let palette = theme.extended_palette();
331
332 Style {
333 background_color: palette.background.base.color,
334 text_color: palette.background.base.text,
335 }
336}