1use anyhow::Result;
2use dirs::home_dir;
3use serde::{Deserialize, Serialize};
4use std::fs;
5use std::path::PathBuf;
6
7use crate::ThemeColors;
8use crate::DARK_MODE_COLORS;
9use crate::LIGHT_MODE_COLORS;
10
11#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
12pub enum ThemeMode {
13 Light,
14 #[default]
15 Dark,
16}
17
18#[derive(Debug, Serialize, Deserialize, Default)]
19pub struct ThothConfig {
20 pub theme: ThemeMode,
21}
22
23impl ThothConfig {
24 pub fn load() -> Result<Self> {
25 let config_path = get_config_path();
26
27 if !config_path.exists() {
28 let default_config = Self::default();
29 default_config.save()?;
30 return Ok(default_config);
31 }
32
33 let config_str = fs::read_to_string(config_path)?;
34 let config: ThothConfig = toml::from_str(&config_str)?;
35 Ok(config)
36 }
37
38 pub fn save(&self) -> Result<()> {
39 let config_path = get_config_path();
40
41 if let Some(parent) = config_path.parent() {
43 if !parent.exists() {
44 fs::create_dir_all(parent)?;
45 }
46 }
47
48 let config_str = toml::to_string(self)?;
49 fs::write(config_path, config_str)?;
50 Ok(())
51 }
52
53 pub fn set_theme(&mut self, theme: ThemeMode) -> Result<()> {
54 self.theme = theme;
55 self.save()?;
56 Ok(())
57 }
58
59 pub fn get_theme_colors(&self) -> &'static ThemeColors {
60 match self.theme {
61 ThemeMode::Light => &LIGHT_MODE_COLORS,
62 ThemeMode::Dark => &DARK_MODE_COLORS,
63 }
64 }
65}
66
67pub fn get_config_path() -> PathBuf {
68 let mut path = home_dir().unwrap_or_default();
69 path.push(".config");
70 path.push("thoth");
71 path.push("config.toml");
72 path
73}