tty_interface/
test.rs

1use crate::{Device, Position, Result, Vector, pos};
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 Default for VirtualDevice {
9    fn default() -> Self {
10        Self::new()
11    }
12}
13
14impl VirtualDevice {
15    /// Create a new device based around a virtual terminal.
16    pub fn new() -> Self {
17        Self(vt100::Parser::default())
18    }
19
20    /// Access this device's underlying parser.
21    pub fn parser(&mut self) -> &mut vt100::Parser {
22        &mut self.0
23    }
24}
25
26impl Device for VirtualDevice {
27    fn get_terminal_size(&mut self) -> Result<Vector> {
28        let (lines, columns) = self.0.screen().size();
29        Ok(Vector::new(columns, lines))
30    }
31
32    fn enable_raw_mode(&mut self) -> Result<()> {
33        Ok(())
34    }
35
36    fn disable_raw_mode(&mut self) -> Result<()> {
37        Ok(())
38    }
39
40    fn get_cursor_position(&mut self) -> Result<Position> {
41        Ok(pos!(0, 0))
42    }
43}
44
45impl std::io::Write for VirtualDevice {
46    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
47        self.0.write(buf)
48    }
49
50    fn flush(&mut self) -> std::io::Result<()> {
51        self.0.flush()
52    }
53}