use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::theme::Theme;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
pub theme: String,
pub menu_delay_ms: u64,
pub autosave_secs: u64,
pub wrap: bool,
pub wrap_margin: usize,
pub help_level: u8,
}
impl Default for Config {
fn default() -> Self {
Config {
theme: String::from("wp-blue"),
menu_delay_ms: 700,
autosave_secs: 60,
wrap: true,
wrap_margin: 0,
help_level: 1,
}
}
}
fn config_path() -> Option<PathBuf> {
Some(dirs::config_dir()?.join("perfectstar2k").join("config.toml"))
}
impl Config {
pub fn load() -> Self {
let Some(path) = config_path() else {
return Config::default();
};
match std::fs::read_to_string(&path) {
Ok(data) => toml::from_str(&data).unwrap_or_default(),
Err(_) => Config::default(),
}
}
pub fn theme(&self) -> Theme {
match self.theme.as_str() {
"wordstar" => Theme::wordstar(),
"terminal" => Theme::terminal_default(),
_ => Theme::wp_blue(),
}
}
}