summaryrefslogtreecommitdiff
path: root/src/config/mod.rs
blob: 4aac0cf4e5b9f017c71e855f42bac050331904d4 (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
use std::ptr::addr_of_mut;

use mlua::{UserData, UserDataFields};

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

pub mod env;
pub mod theme;
pub mod keymap;

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

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

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

    pub fn get() -> &'static mut Self {
        static mut GLOBAL_CONFIG: GlobalConfig = GlobalConfig::new();
        unsafe { &mut *addr_of_mut!(GLOBAL_CONFIG) }
    }
}

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));
    }
}