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
use crate::{Result, Vector};

/// An output device to be controlled for displaying an interface.
pub trait Device: std::io::Write {
    /// Retrieve the device's terminal viewport size.
    fn get_terminal_size(&mut self) -> Result<Vector>;

    /// Enable "raw mode" in the terminal.
    fn enable_raw_mode(&mut self) -> Result<()>;

    /// Restore the configuration before the terminal was placed in "raw mode".
    fn disable_raw_mode(&mut self) -> Result<()>;
}

impl Device for std::io::Stdout {
    fn get_terminal_size(&mut self) -> Result<Vector> {
        let (columns, lines) = crossterm::terminal::size()?;
        Ok(Vector::new(columns, lines))
    }

    fn enable_raw_mode(&mut self) -> Result<()> {
        crossterm::terminal::enable_raw_mode()?;
        Ok(())
    }

    fn disable_raw_mode(&mut self) -> Result<()> {
        crossterm::terminal::disable_raw_mode()?;
        Ok(())
    }
}