tty_interface/
test.rs

1use crate::{pos, Device, Position, Result, Vector};
2
3/// A virtual testing device based on the vte/vt100 parser. Ideally, this would be hidden from
4/// production builds and only available to functional, documentation, and unit tests, but that does
5/// not seem to be possible currently.
6pub struct VirtualDevice(vt100::Parser);
7
8impl VirtualDevice {
9    /// Create a new device based around a virtual terminal.
10    pub fn new() -> Self {
11        Self(vt100::Parser::default())
12    }
13
14    /// Access this device's underlying parser.
15    pub fn parser(&mut self) -> &mut vt100::Parser {
16        &mut self.0
17    }
18}
19
20impl Device for VirtualDevice {
21    fn get_terminal_size(&mut self) -> Result<Vector> {
22        let (lines, columns) = self.0.screen().size();
23        Ok(Vector::new(columns, lines))
24    }
25
26    fn enable_raw_mode(&mut self) -> Result<()> {
27        Ok(())
28    }
29
30    fn disable_raw_mode(&mut self) -> Result<()> {
31        Ok(())
32    }
33
34    fn get_cursor_position(&mut self) -> Result<Position> {
35        Ok(pos!(0, 0))
36    }
37}
38
39impl std::io::Write for VirtualDevice {
40    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
41        self.0.write(buf)
42    }
43
44    fn flush(&mut self) -> std::io::Result<()> {
45        self.0.flush()
46    }
47}