Skip to main content

md_tui/util/
general.rs

1use std::sync::LazyLock;
2
3use config::{Config, Environment, File};
4use serde::Deserialize;
5
6#[derive(Debug)]
7pub struct GeneralConfig {
8    pub width: u16,
9    pub gitignore: bool,
10    pub centering: Centering,
11    pub help_menu: bool,
12}
13
14#[derive(Debug, Deserialize)]
15pub enum Centering {
16    Left,
17    Center,
18    Right,
19}
20
21pub static GENERAL_CONFIG: LazyLock<GeneralConfig> = LazyLock::new(|| {
22    let config_dir = dirs::home_dir().unwrap();
23    let config_file = config_dir.join(".config").join("mdt").join("config.toml");
24    let settings = Config::builder()
25        .add_source(File::with_name(config_file.to_str().unwrap()).required(false))
26        .add_source(Environment::with_prefix("MDT").separator("_"))
27        .build()
28        .unwrap();
29
30    let width = settings.get::<u16>("width").unwrap_or(100);
31    GeneralConfig {
32        // width = 0 means "use full terminal width"
33        width: if width == 0 { u16::MAX } else { width },
34        gitignore: settings.get::<bool>("gitignore").unwrap_or(false),
35        centering: settings
36            .get::<Centering>("alignment")
37            .unwrap_or(Centering::Left),
38        help_menu: settings.get::<bool>("help_menu").unwrap_or(true),
39    }
40});