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
use crate::cell::Cell;
/// A 2D array of `Cell` to render on screen
pub trait TextBuffer {
/// Columns
fn width(&self) -> usize;
/// Rows
fn height(&self) -> usize;
/// Read the character at `(row, col)`
///
/// Avoid use this because it's usually very slow on real hardware.
fn read(&self, row: usize, col: usize) -> Cell;
/// Write a character `ch` at `(row, col)`
fn write(&mut self, row: usize, col: usize, cell: Cell);
/// Delete one character at `(row, col)`.
fn delete(&mut self, row: usize, col: usize) {
self.write(row, col, Cell::default());
}
/// Insert one blank line at the bottom, and scroll up one line.
///
/// The default method does single read and write for each pixel.
/// Usually it needs rewrite for better performance.
fn new_line(&mut self, cell: Cell) {
for i in 1..self.height() {
for j in 0..self.width() {
self.write(i - 1, j, self.read(i, j));
}
}
for j in 0..self.width() {
self.write(self.height() - 1, j, cell);
}
}
/// Clear the buffer
fn clear(&mut self, cell: Cell) {
for i in 0..self.height() {
for j in 0..self.width() {
self.write(i, j, cell);
}
}
}
}