Skip to main content

rumatui_tui/backend/
test.rs

1use crate::backend::Backend;
2use crate::buffer::{Buffer, Cell};
3use crate::layout::Rect;
4use std::io;
5
6#[derive(Debug)]
7pub struct TestBackend {
8    width: u16,
9    buffer: Buffer,
10    height: u16,
11    cursor: bool,
12    pos: (u16, u16),
13}
14
15impl TestBackend {
16    pub fn new(width: u16, height: u16) -> TestBackend {
17        TestBackend {
18            width,
19            height,
20            buffer: Buffer::empty(Rect::new(0, 0, width, height)),
21            cursor: false,
22            pos: (0, 0),
23        }
24    }
25
26    pub fn buffer(&self) -> &Buffer {
27        &self.buffer
28    }
29}
30
31impl Backend for TestBackend {
32    fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>
33    where
34        I: Iterator<Item = (u16, u16, &'a Cell)>,
35    {
36        for (x, y, c) in content {
37            let cell = self.buffer.get_mut(x, y);
38            cell.symbol = c.symbol.clone();
39            cell.style = c.style;
40        }
41        Ok(())
42    }
43    fn hide_cursor(&mut self) -> Result<(), io::Error> {
44        self.cursor = false;
45        Ok(())
46    }
47    fn show_cursor(&mut self) -> Result<(), io::Error> {
48        self.cursor = true;
49        Ok(())
50    }
51    fn get_cursor(&mut self) -> Result<(u16, u16), io::Error> {
52        Ok(self.pos)
53    }
54    fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error> {
55        self.pos = (x, y);
56        Ok(())
57    }
58    fn clear(&mut self) -> Result<(), io::Error> {
59        Ok(())
60    }
61    fn size(&self) -> Result<Rect, io::Error> {
62        Ok(Rect::new(0, 0, self.width, self.height))
63    }
64    fn flush(&mut self) -> Result<(), io::Error> {
65        Ok(())
66    }
67}