pub trait Widget {
fn handle_input(&mut self, _event: InputEvent) -> bool {
false }
fn widget_type(&self) -> &'static str {
"Widget"
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputEvent {
Up,
Down,
Left,
Right,
Select,
Cancel,
Tab,
Char(char),
Function(u8),
Other,
}
impl From<crossterm::event::KeyCode> for InputEvent {
fn from(key: crossterm::event::KeyCode) -> Self {
use crossterm::event::KeyCode;
match key {
KeyCode::Up | KeyCode::Char('k') => InputEvent::Up,
KeyCode::Down | KeyCode::Char('j') => InputEvent::Down,
KeyCode::Left | KeyCode::Char('h') => InputEvent::Left,
KeyCode::Right | KeyCode::Char('l') => InputEvent::Right,
KeyCode::Enter => InputEvent::Select,
KeyCode::Esc | KeyCode::Char('q') => InputEvent::Cancel,
KeyCode::Tab => InputEvent::Tab,
KeyCode::Char(c) => InputEvent::Char(c),
KeyCode::F(n) => InputEvent::Function(n),
_ => InputEvent::Other,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_input_event_conversion() {
use crossterm::event::KeyCode;
assert_eq!(InputEvent::from(KeyCode::Up), InputEvent::Up);
assert_eq!(InputEvent::from(KeyCode::Enter), InputEvent::Select);
assert_eq!(InputEvent::from(KeyCode::Char('k')), InputEvent::Up);
assert_eq!(InputEvent::from(KeyCode::Char('a')), InputEvent::Char('a'));
}
}