Debian Codegraph @

helix-tui/src/backend/test.rs

raw symbols SCIP
1 use crate::{
2 backend::Backend,
3 buffer::{Buffer, Cell},
4 terminal::Config,
5 };
6 use helix_core::unicode::width::UnicodeWidthStr;
7 use helix_view::graphics::{CursorKind, Rect};
8 use std::{fmt::Write, io};
9
10 /// A backend used for the integration tests.
11 #[derive(Debug)]
12 pub struct TestBackend {
13 width: u16,
14 buffer: Buffer,
15 height: u16,
16 cursor: bool,
17 pos: (u16, u16),
18 }
19
20 /// Returns a string representation of the given buffer for debugging purpose.
21 fn buffer_view(buffer: &Buffer) -> String {
22 let mut view = String::with_capacity(buffer.content.len() + buffer.area.height as usize * 3);
23 for cells in buffer.content.chunks(buffer.area.width as usize) {
24 let mut overwritten = vec![];
25 let mut skip: usize = 0;
26 view.push('"');
27 for (x, c) in cells.iter().enumerate() {
28 if skip == 0 {
29 view.push_str(&c.symbol);
30 } else {
31 overwritten.push((x, &c.symbol))
32 }
33 skip = std::cmp::max(skip, c.symbol.width()).saturating_sub(1);
34 }
35 view.push('"');
36 if !overwritten.is_empty() {
37 write!(
38 &mut view,
39 " Hidden by multi-width symbols: {:?}",
40 overwritten
41 )
42 .unwrap();
43 }
44 view.push('\n');
45 }
46 view
47 }
48
49 impl TestBackend {
50 pub fn new(width: u16, height: u16) -> TestBackend {
51 TestBackend {
52 width,
53 height,
54 buffer: Buffer::empty(Rect::new(0, 0, width, height)),
55 cursor: false,
56 pos: (0, 0),
57 }
58 }
59
60 pub fn buffer(&self) -> &Buffer {
61 &self.buffer
62 }
63
64 pub fn resize(&mut self, width: u16, height: u16) {
65 self.buffer.resize(Rect::new(0, 0, width, height));
66 self.width = width;
67 self.height = height;
68 }
69
70 pub fn assert_buffer(&self, expected: &Buffer) {
71 assert_eq!(expected.area, self.buffer.area);
72 let diff = expected.diff(&self.buffer);
73 if diff.is_empty() {
74 return;
75 }
76
77 let mut debug_info = String::from("Buffers are not equal");
78 debug_info.push('\n');
79 debug_info.push_str("Expected:");
80 debug_info.push('\n');
81 let expected_view = buffer_view(expected);
82 debug_info.push_str(&expected_view);
83 debug_info.push('\n');
84 debug_info.push_str("Got:");
85 debug_info.push('\n');
86 let view = buffer_view(&self.buffer);
87 debug_info.push_str(&view);
88 debug_info.push('\n');
89
90 debug_info.push_str("Diff:");
91 debug_info.push('\n');
92 let nice_diff = diff
93 .iter()
94 .enumerate()
95 .map(|(i, (x, y, cell))| {
96 let expected_cell = expected.get(*x, *y);
97 format!(
98 "{}: at ({}, {}) expected {:?} got {:?}",
99 i, x, y, expected_cell, cell
100 )
101 })
102 .collect::<Vec<String>>()
103 .join("\n");
104 debug_info.push_str(&nice_diff);
105 panic!("{}", debug_info);
106 }
107 }
108
109 impl Backend for TestBackend {
110 fn claim(&mut self) -> Result<(), io::Error> {
111 Ok(())
112 }
113
114 fn reconfigure(&mut self, _config: Config) -> Result<(), io::Error> {
115 Ok(())
116 }
117
118 fn restore(&mut self) -> Result<(), io::Error> {
119 Ok(())
120 }
121
122 fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>
123 where
124 I: Iterator<Item = (u16, u16, &'a Cell)>,
125 {
126 for (x, y, c) in content {
127 self.buffer[(x, y)] = c.clone();
128 }
129 Ok(())
130 }
131
132 fn hide_cursor(&mut self) -> Result<(), io::Error> {
133 self.cursor = false;
134 Ok(())
135 }
136
137 fn show_cursor(&mut self, _kind: CursorKind) -> Result<(), io::Error> {
138 self.cursor = true;
139 Ok(())
140 }
141
142 fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error> {
143 self.pos = (x, y);
144 Ok(())
145 }
146
147 fn clear(&mut self) -> Result<(), io::Error> {
148 self.buffer.reset();
149 Ok(())
150 }
151
152 fn start_sync(&mut self) -> Result<(), io::Error> {
153 Ok(())
154 }
155
156 fn end_sync(&mut self) -> Result<(), io::Error> {
157 Ok(())
158 }
159
160 fn size(&self) -> Result<Rect, io::Error> {
161 Ok(Rect::new(0, 0, self.width, self.height))
162 }
163
164 fn flush(&mut self) -> Result<(), io::Error> {
165 Ok(())
166 }
167
168 fn supports_true_color(&self) -> bool {
169 false
170 }
171
172 fn get_theme_mode(&self) -> Option<helix_view::theme::Mode> {
173 None
174 }
175
176 fn set_background_color(&mut self, _color: Option<helix_view::theme::Color>) -> io::Result<()> {
177 Ok(())
178 }
179 }