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
use {
crate::{
Bounds,
Cell,
Position,
Size,
Result
},
};
pub struct Buffer {
content: Box<[Cell]>,
pub bounds: Bounds,
}
impl std::fmt::Debug for Buffer {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for row in 0..self.bounds.dim.height() {
for col in 0..self.bounds.dim.width() {
let pos = Position(col, row);
write!(f, "{} ", self.cell_at(pos).unwrap()).unwrap();
}
write!(f, "\n\n").unwrap();
}
Ok(())
}
}
impl Buffer {
pub fn filled_with(fill: Cell, bounds: Bounds) -> Self {
let content = vec![
fill;
(bounds.dim.width() * bounds.dim.height()) as usize
].into_boxed_slice();
Self { content, bounds }
}
pub fn blank(bounds: Bounds) -> Self {
Self::filled_with(Cell::default(), bounds)
}
pub fn cell_at(&self, pos: Position) -> Result<&Cell> {
Ok(&self.content[self.bounds.index_of(pos)?])
}
}