embedded_term/
text_buffer.rs

1use crate::cell::Cell;
2
3/// A 2D array of `Cell` to render on screen
4pub trait TextBuffer {
5    /// Columns
6    fn width(&self) -> usize;
7
8    /// Rows
9    fn height(&self) -> usize;
10
11    /// Read the character at `(row, col)`
12    ///
13    /// Avoid use this because it's usually very slow on real hardware.
14    fn read(&self, row: usize, col: usize) -> Cell;
15
16    /// Write a character `ch` at `(row, col)`
17    fn write(&mut self, row: usize, col: usize, cell: Cell);
18
19    /// Delete one character at `(row, col)`.
20    fn delete(&mut self, row: usize, col: usize) {
21        self.write(row, col, Cell::default());
22    }
23
24    /// Insert one blank line at the bottom, and scroll up one line.
25    ///
26    /// The default method does single read and write for each pixel.
27    /// Usually it needs rewrite for better performance.
28    fn new_line(&mut self, cell: Cell) {
29        for i in 1..self.height() {
30            for j in 0..self.width() {
31                self.write(i - 1, j, self.read(i, j));
32            }
33        }
34        for j in 0..self.width() {
35            self.write(self.height() - 1, j, cell);
36        }
37    }
38
39    /// Clear the buffer
40    fn clear(&mut self, cell: Cell) {
41        for i in 0..self.height() {
42            for j in 0..self.width() {
43                self.write(i, j, cell);
44            }
45        }
46    }
47}