diff --git a/cove-input/src/input.rs b/cove-input/src/input.rs new file mode 100644 index 0000000..5e424ce --- /dev/null +++ b/cove-input/src/input.rs @@ -0,0 +1,65 @@ +use crossterm::event::KeyEvent; + +use crate::KeyBinding; + +pub enum Entry { + Space, + Category(String), + Binding(KeyBinding, String), +} + +pub struct Input { + event: Option, + + record_entries: bool, + entries: Vec, +} + +impl Input { + pub fn new_from_event(event: KeyEvent) -> Self { + Self { + event: Some(event), + record_entries: false, + entries: vec![], + } + } + + pub fn new_recording() -> Self { + Self { + event: None, + record_entries: true, + entries: vec![], + } + } + + pub fn space(&mut self) { + if self.record_entries { + self.entries.push(Entry::Space); + } + } + + pub fn category(&mut self, name: S) { + if self.record_entries { + self.entries.push(Entry::Category(name.to_string())); + } + } + + pub fn matches(&mut self, binding: &KeyBinding, description: S) -> bool { + let matches = if let Some(event) = self.event { + binding.matches(event) + } else { + false + }; + + if self.record_entries { + self.entries + .push(Entry::Binding(binding.clone(), description.to_string())); + } + + matches + } + + pub fn entries(&self) -> &[Entry] { + &self.entries + } +} diff --git a/cove-input/src/lib.rs b/cove-input/src/lib.rs index 1da1a6f..c89a689 100644 --- a/cove-input/src/lib.rs +++ b/cove-input/src/lib.rs @@ -1,3 +1,5 @@ +mod input; mod keys; +pub use input::*; pub use keys::*;