Query key bindings against input event

This commit is contained in:
Joscha 2023-04-26 15:01:50 +02:00
parent 3fbb9127a6
commit 072290511b
2 changed files with 67 additions and 0 deletions

65
cove-input/src/input.rs Normal file
View file

@ -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<KeyEvent>,
record_entries: bool,
entries: Vec<Entry>,
}
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<S: ToString>(&mut self) {
if self.record_entries {
self.entries.push(Entry::Space);
}
}
pub fn category<S: ToString>(&mut self, name: S) {
if self.record_entries {
self.entries.push(Entry::Category(name.to_string()));
}
}
pub fn matches<S: ToString>(&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
}
}

View file

@ -1,3 +1,5 @@
mod input;
mod keys; mod keys;
pub use input::*;
pub use keys::*; pub use keys::*;