1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::time::Duration;

use crossterm::event::{self, Event};
use crossterm::terminal;

use crate::error::Result;

/// Terminal is a wrapper of crossterm::terminal.
/// This is intended for mocking terminal-specific functions.
pub trait Terminal {
    fn size(&self) -> Result<(u16, u16)> {
        let pair = terminal::size()?;
        Ok(pair)
    }

    fn enable_raw_mode(&self) -> Result<()> {
        let _ = terminal::enable_raw_mode()?;
        Ok(())
    }

    fn disable_raw_mode(&self) -> Result<()> {
        let _ = terminal::disable_raw_mode()?;
        Ok(())
    }
}

/// TerminalEvent is a wrapper of crossterm::event.
/// This is intended for mocking terminal-specific functions.
pub trait TerminalEvent {
    fn poll(&self, timeout: Duration) -> Result<bool> {
        let b = event::poll(timeout)?;
        Ok(b)
    }

    fn read(&mut self) -> Result<Event> {
        let e = event::read()?;
        Ok(e)
    }
}

pub struct DefaultTerminal;

pub struct DefaultTerminalEvent;

impl Terminal for DefaultTerminal {}

impl TerminalEvent for DefaultTerminalEvent {}