use std::path::Path;
use color_eyre::eyre::Result;
use crossterm::event::KeyEvent;
use crate::{keybinding::parse_keysequence, ui::panel::Config as PanelConfig};
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct Config {
pub leadr_key: String,
pub redraw_prompt_line: bool,
pub panel: PanelConfig,
}
impl Default for Config {
fn default() -> Self {
Self {
leadr_key: "<C-g>".into(),
redraw_prompt_line: true,
panel: PanelConfig::default(),
}
}
}
impl Config {
pub fn load(config_dir: &Path) -> Result<Self> {
let config_path = config_dir.join("config.toml");
if config_path.exists() {
let contents = std::fs::read_to_string(&config_path)?;
let config = toml::from_str(&contents)?;
Ok(config)
} else {
Ok(Config::default())
}
}
pub fn create_default(config_dir: &Path) -> Result<()> {
std::fs::create_dir_all(config_dir)?;
let config_path = config_dir.join("config.toml");
if !config_path.exists() {
let default_config = Config::default();
let contents = toml::to_string(&default_config)?;
std::fs::write(config_path, contents)?;
}
Ok(())
}
pub fn leadr_key_events(&self) -> Result<Vec<KeyEvent>> {
parse_keysequence(&self.leadr_key)
}
}