gitfetch_rs/config/
manager.rs1use anyhow::Result;
2use directories::ProjectDirs;
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6#[derive(Debug, Serialize, Deserialize, Default)]
7pub struct Config {
8 pub provider: Option<String>,
9 pub provider_url: Option<String>,
10 pub token: Option<String>,
11 pub default_username: Option<String>,
12 pub cache_expiry_minutes: u32,
13 pub custom_box: Option<String>,
14 pub show_date: bool,
15 pub colors: ColorConfig,
16}
17
18#[derive(Debug, Serialize, Deserialize)]
19pub struct ColorConfig {
20 pub level_0: String,
21 pub level_1: String,
22 pub level_2: String,
23 pub level_3: String,
24 pub level_4: String,
25}
26
27impl Default for ColorConfig {
28 fn default() -> Self {
29 Self {
30 level_0: "#ebedf0".to_string(), level_1: "#9be9a8".to_string(),
32 level_2: "#40c463".to_string(),
33 level_3: "#30a14e".to_string(),
34 level_4: "#216e39".to_string(),
35 }
36 }
37}
38
39pub struct ConfigManager {
40 config_path: PathBuf,
41 pub config: Config,
42}
43
44impl ConfigManager {
45 pub fn new() -> Result<Self> {
46 let project_dirs = ProjectDirs::from("com", "gitfetch", "gitfetch")
47 .ok_or_else(|| anyhow::anyhow!("Could not determine config directory"))?;
48
49 let config_dir = project_dirs.config_dir();
50 std::fs::create_dir_all(config_dir)?;
51
52 let config_path = config_dir.join("config.toml");
53
54 let config = if config_path.exists() {
55 let content = std::fs::read_to_string(&config_path)?;
56 toml::from_str(&content).unwrap_or_else(|_| Config::default())
57 } else {
58 Config::default()
60 };
61
62 Ok(Self {
63 config_path,
64 config,
65 })
66 }
67
68 pub fn is_initialized(&self) -> bool {
69 self.config.provider.is_some()
70 }
71
72 pub fn save(&self) -> Result<()> {
73 let content = toml::to_string_pretty(&self.config)?;
74 std::fs::write(&self.config_path, content)?;
75 Ok(())
76 }
77
78 pub fn get_provider(&self) -> Option<&str> {
79 self.config.provider.as_deref()
80 }
81
82 pub fn set_provider(&mut self, provider: String) {
83 self.config.provider = Some(provider);
84 }
85
86 pub fn get_provider_url(&self) -> Option<&str> {
87 self.config.provider_url.as_deref()
88 }
89
90 pub fn set_provider_url(&mut self, url: String) {
91 self.config.provider_url = Some(url);
92 }
93
94 pub fn get_token(&self) -> Option<&str> {
95 self.config.token.as_deref()
96 }
97
98 pub fn set_token(&mut self, token: String) {
99 self.config.token = Some(token);
100 }
101
102 pub fn get_default_username(&self) -> Option<&str> {
103 self.config.default_username.as_deref()
104 }
105
106 pub fn set_default_username(&mut self, username: String) {
107 self.config.default_username = Some(username);
108 }
109}