use core::fmt; use std::{ collections::HashMap, sync::{Arc, Mutex}, }; use ratatui::crossterm::event::KeyEvent; use crate::lua::runnable::Runnable; type KeyMapHandler = Arc>>; #[derive(Default, Clone)] pub struct KeyMapStore { store: Option>, } impl KeyMapStore { pub const fn new() -> Self { Self { store: None } } pub fn get(&mut self, event: KeyEvent) -> Option>>> { match &self.store { Some(store) => match store.get(&event) { Some(func) => Some(Arc::clone(func)), None => None, }, None => { self.store = Some(HashMap::new()); None } } } pub fn map(&mut self, event: KeyEvent, func: impl Runnable<(), bool> + 'static) { match &self.store { Some(_) => {} None => self.store = Some(HashMap::new()), } if let Some(store) = &mut self.store { store.insert(event, Arc::new(Mutex::new(func))); } } } impl fmt::Debug for KeyMapStore { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "KeyMapStore") } }