summaryrefslogtreecommitdiff
path: root/src/config/mod.rs
blob: cc4154cb7651d4e9dbd2c4b9e25a55faa5ca7637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};

use lazy_static::lazy_static;
use mlua::{UserData, UserDataFields};

use self::{keymap::KeyMap, theme::Theme};

pub mod constants;
pub mod theme;
pub mod keymap;

#[derive(Debug, Default)]
pub struct GlobalConfig {
    pub theme: Theme,
    pub keymap: KeyMap,
}

lazy_static! {
    static ref GLOBAL_CONFIG: RwLock<GlobalConfig> = RwLock::new(GlobalConfig::new());
}

const DUMMY_CONFIG: GlobalConfig = GlobalConfig::new();

impl GlobalConfig {
    const fn new() -> Self {
        Self {
            theme: Theme::new(),
            keymap: KeyMap::new(),
        }
    }

    pub fn instance() -> RwLockReadGuard<'static, Self> {
        GLOBAL_CONFIG.read().unwrap()
    }

    pub fn instance_mut() -> RwLockWriteGuard<'static, Self> {
        GLOBAL_CONFIG.write().unwrap()
    }
}

impl UserData for GlobalConfig {
    fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
        fields.add_field_function_get("theme", |_, _| Ok(DUMMY_CONFIG.theme));
        fields.add_field_function_get("keymap", |_, _| Ok(DUMMY_CONFIG.keymap));
    }
}