use std::time::Duration;
use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use super::app::App;
pub fn handle_events(app: &mut App) -> Result<bool> {
if event::poll(Duration::from_millis(250))? {
if let Event::Key(key) = event::read()? {
use crossterm::event::KeyEventKind;
if key.kind != KeyEventKind::Press {
return Ok(false);
}
match key.code {
KeyCode::Char('q') => app.quit(),
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => app.quit(),
KeyCode::Char('j') | KeyCode::Down => app.next(),
KeyCode::Char('k') | KeyCode::Up => app.prev(),
KeyCode::Char('g') => app.go_top(),
KeyCode::Char('G') => app.go_bottom(),
KeyCode::Enter => app.enter()?,
KeyCode::Esc | KeyCode::Backspace => app.back(),
KeyCode::Char('r') => app.refresh()?,
_ => return Ok(false),
}
return Ok(true);
}
}
Ok(false)
}