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
|
use ::ratatui::backend::Backend;
use ::ratatui::buffer::Cell;
use ::ratatui::layout::Rect;
use ::std::collections::HashMap;
use ::std::io;
use ::std::sync::{Arc, Mutex};
#[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 flush(&mut self) -> io::Result<()> {
self.events.lock().unwrap().push(TerminalEvent::Flush);
Ok(())
}
}
|