added auto update & changed config path

This commit is contained in:
Uber Veng
2026-05-21 22:59:02 +07:00
parent 1599cfcafa
commit f2349c9f15
7 changed files with 435 additions and 7 deletions

View File

@@ -9,6 +9,8 @@ pub struct Config {
pub timeout_sec: u64,
#[serde(default = "default_theme")]
pub theme: Theme,
#[serde(default)]
pub update_api: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
@@ -29,9 +31,16 @@ fn default_theme() -> Theme {
impl Config {
pub fn load() -> Result<Self> {
let config_path = dirs::config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("tui-client")
// Respect XDG_CONFIG_HOME; fall back to ~/.config on all platforms.
let config_base = std::env::var("XDG_CONFIG_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".config")
});
let config_path = config_base
.join(env!("CARGO_PKG_NAME"))
.join("config.toml");
if !config_path.exists() {
@@ -39,6 +48,7 @@ impl Config {
server_url: "http://localhost:8080/api/menu".to_string(),
timeout_sec: 10,
theme: default_theme(),
update_api: None,
};
fs::create_dir_all(config_path.parent().unwrap())?;
fs::write(config_path, toml::to_string_pretty(&default)?)?;