termrs_runtime 0.3.0

Runtime for termrs applications
Documentation
use core::input::TerminalEvent;
use std::time::Duration;

use super::InputError;

pub trait EventLoop {
    /// Blocks caller thread and waits for an event.
    fn poll(&self) -> Result<TerminalEvent, InputError>;
}

pub struct DefaultEventLoop;

impl EventLoop for DefaultEventLoop {
    fn poll(&self) -> Result<TerminalEvent, InputError> {
        // Block thread until an available event appears
        match crossterm::event::poll(Duration::MAX) {
            // if there is an event available
            Ok(true) => match crossterm::event::read() {
                // try to convert to event supported by applications
                Ok(event) => match event.try_into() {
                    // if the event can be handled by applications
                    Ok(terminal_event) => Ok(terminal_event),
                    // if the event is not supported
                    Err(_) => Err(InputError::Unsupported),
                },
                // if an error occured when try to get event
                Err(error) => Err(InputError::IO(error)),
            },
            // This should never happen, because
            // we wait for Duration::MAX (584,942,417,355 years)
            Ok(false) => panic!("Time's up"),
            Err(error) => Err(InputError::IO(error)),
        }
    }
}