use crate::ui::core::widget::InputEvent;
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
use std::time::Duration;
pub fn poll_input(timeout: Duration) -> std::io::Result<InputEvent> {
if event::poll(timeout)? {
if let Event::Key(key_event) = event::read()? {
if key_event.kind == KeyEventKind::Press {
return Ok(InputEvent::from(key_event.code));
}
}
}
Ok(InputEvent::Other)
}
pub fn poll_key(timeout: Duration) -> std::io::Result<Option<KeyCode>> {
if event::poll(timeout)? {
if let Event::Key(key_event) = event::read()? {
if key_event.kind == KeyEventKind::Press {
return Ok(Some(key_event.code));
}
}
}
Ok(None)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "requires terminal input"]
fn test_poll_input_timeout() {
let result = poll_input(Duration::from_millis(1));
assert!(result.is_ok());
}
#[test]
#[ignore = "requires terminal input"]
fn test_poll_key_timeout() {
let result = poll_key(Duration::from_millis(1));
assert!(result.is_ok());
}
}