rusty_textui/
frame.rs

1use std::ops::{Index, IndexMut};
2
3use crossterm::style::{Color, Stylize};
4
5/// A `Frame` is what the [`Screen`] uses internally to store text that you draw to it.
6#[derive(Clone)]
7pub struct Frame {
8    pub cols: usize,
9    pub rows: usize,
10    inner: Vec<Vec<<char as Stylize>::Styled>>,
11}
12
13impl Frame {
14    /// Create a new frame.
15    pub fn new(cols: usize, rows: usize) -> Self {
16        let inner = vec![vec![' '.with(Color::White); rows]; cols];
17        Self { cols, rows, inner }
18    }
19    /// Clear the frame to all space characters.
20    pub fn clear(&mut self) {
21        for row in &mut self.inner {
22            row.fill(' '.with(Color::White));
23        }
24    }
25}
26
27// Make it so you can use square brackets to read into a frame
28impl Index<usize> for Frame {
29    type Output = Vec<<char as Stylize>::Styled>;
30
31    fn index(&self, index: usize) -> &Self::Output {
32        &self.inner[index]
33    }
34}
35
36// Make it so you can use square brackets to read/write into a frame
37impl IndexMut<usize> for Frame {
38    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
39        &mut self.inner[index]
40    }
41}