summaryrefslogtreecommitdiffstats
path: root/src/tests/fakes/fake_output.rs
blob: 524fa6cff92e9efcc3cb3a000a445e02cfb7af4c (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::{
    collections::HashMap,
    io,
    sync::{Arc, Mutex},
};

use ratatui::{
    backend::{Backend, WindowSize},
    buffer::Cell,
    layout::{Rect, Size},
};

#[derive(Hash, Debug, PartialEq)]
pub enum TerminalEvent {
    Clear,
    HideCursor,
    ShowCursor,
    GetCursor,
    Flush,
    Draw,
}

pub struct TestBackend {
    pub events: Arc<Mutex<Vec<TerminalEvent>>>,
    pub draw_events: Arc<Mutex<Vec<String>>>,
    terminal_width: Arc<Mutex<u16>>,
    terminal_height: Arc<Mutex<u16>>,
}

impl TestBackend {
    pub fn new(
        log: Arc<Mutex<Vec<TerminalEvent>>>,
        draw_log: Arc<Mutex<Vec<String>>>,
        terminal_width: Arc<Mutex<u16>>,
        terminal_height: Arc<Mutex<u16>>,
    ) -> TestBackend {
        TestBackend {
            events: log,
            draw_events: draw_log,
            terminal_width,
            terminal_height,
        }
    }
}

#[derive(Hash, Eq, PartialEq)]
struct Point {
    x: u16,
    y: u16,
}

impl Backend for TestBackend {
    fn clear(&mut self) -> io::Result<()> {
        self.events.lock().unwrap().push(TerminalEvent::Clear);
        Ok(())
    }

    fn hide_cursor(&mut self) -> io::Result<()> {
        self.events.lock().unwrap().push(TerminalEvent::HideCursor);
        Ok(())
    }

    fn show_cursor(&mut self) -> io::Result<()> {
        self.events.lock().unwrap().push(TerminalEvent::ShowCursor);
        Ok(())
    }

    fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
        self.events.lock().unwrap().push(TerminalEvent::GetCursor);
        Ok((0, 0))
    }

    fn set_cursor(&mut self, _x: u16, _y: u16) -> io::Result<()> {
        Ok(())
    }

    fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
    where
        I: Iterator<Item = (u16, u16, &'a Cell)>,
    {
        // use std::fmt::Write;
        self.events.lock().unwrap().push(TerminalEvent::Draw);
        let mut string = String::with_capacity(content.size_hint().0 * 3);
        let mut coordinates = HashMap::new();
        for (x, y, cell) in content {
            coordinates.insert(Point { x, y }, cell);
        }
        let terminal_height = self.terminal_height.lock().unwrap();
        let terminal_width = self.terminal_width.lock().unwrap();
        for y in 0..*terminal_height {
            for x in 0..*terminal_width {
                match coordinates.get(&Point { x, y }) {
                    Some(cell) => {
                        // this will contain no style information at all
                        // should be good enough for testing
                        string.push_str(cell.symbol());
                    }
                    None => {
                        string.push(' ');
                    }
                }
            }
            string.push('\n');
        }
        self.draw_events.lock().unwrap().push(string);
        Ok(())
    }

    fn size(&self) -> io::Result<Rect> {
        let terminal_height = self.terminal_height.lock().unwrap();
        let terminal_width = self.terminal_width.lock().unwrap();

        Ok(Rect::new(0, 0, *terminal_width, *terminal_height))
    }

    fn window_size(&mut self) -> io::Result<WindowSize> {
        let width = *self.terminal_width.lock().unwrap();
        let height = *self.terminal_height.lock().unwrap();

        Ok(WindowSize {
            columns_rows: Size { width, height },
            pixels: Size::default(),
        })
    }

    fn flush(&mut self) -> io::Result<()> {
        self.events.lock().unwrap().push(TerminalEvent::Flush);
        Ok(())
    }
}