thoth_cli/
config.rs

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    #[serde(default)]
21    pub theme: ThemeMode,
22    #[serde(default)]
23    pub notes_dir: Option<String>,
24}
25
26impl ThothConfig {
27    pub fn load() -> Result<Self> {
28        let config_path = get_config_path();
29
30        if !config_path.exists() {
31            let default_config = Self::default();
32            default_config.save()?;
33            return Ok(default_config);
34        }
35
36        let config_str = fs::read_to_string(config_path)?;
37        let config: ThothConfig = toml::from_str(&config_str)?;
38        Ok(config)
39    }
40
41    pub fn save(&self) -> Result<()> {
42        let config_path = get_config_path();
43
44        // Create directory if it doesn't exist
45        if let Some(parent) = config_path.parent() {
46            if !parent.exists() {
47                fs::create_dir_all(parent)?;
48            }
49        }
50
51        let config_str = toml::to_string(self)?;
52        fs::write(config_path, config_str)?;
53        Ok(())
54    }
55
56    pub fn set_theme(&mut self, theme: ThemeMode) -> Result<()> {
57        self.theme = theme;
58        self.save()?;
59        Ok(())
60    }
61
62    pub fn get_theme_colors(&self) -> &'static ThemeColors {
63        match self.theme {
64            ThemeMode::Light => &LIGHT_MODE_COLORS,
65            ThemeMode::Dark => &DARK_MODE_COLORS,
66        }
67    }
68}
69
70pub fn get_config_path() -> PathBuf {
71    let mut path = home_dir().unwrap_or_default();
72    path.push(".config");
73    path.push("thoth");
74    path.push("config.toml");
75    path
76}