Skip to main content

sericom_core/screen_buffer/
line.rs

1use super::Cell;
2use std::ops::{Index, IndexMut};
3
4/// Line is a wrapper around [`Vec<Cell>`] and represents a line within the [`ScreenBuffer`][`super::ScreenBuffer`].
5#[derive(Clone, Debug)]
6pub struct Line(Vec<Cell>);
7
8impl Line {
9    /// Create a new line with the length/size of `width`.
10    ///
11    /// Filled with [`Cell::default()`].
12    pub fn new(width: usize) -> Self {
13        Self(vec![Cell::default(); width])
14    }
15
16    /// Iterates over all the [`Cell`]s within the line and sets them to [`Cell::default()`].
17    pub fn reset(&mut self) {
18        self.0.iter_mut().for_each(|cell| *cell = Cell::default());
19    }
20
21    /// Iterates over the [`Cell`]s to index `idx` within [`Self`]
22    /// and sets them to [`Cell::default()`].
23    pub fn reset_to(&mut self, idx: usize) {
24        self.0[..idx]
25            .iter_mut()
26            .for_each(|cell| *cell = Cell::default());
27    }
28
29    /// Iterates over the [`Cell`]s from index `idx` within [`Self`]
30    /// to the end of [`Self`] and sets them to [`Cell::default()`].
31    pub fn reset_from(&mut self, idx: usize) {
32        self.0
33            .iter_mut()
34            .skip(idx)
35            .for_each(|cell| *cell = Cell::default());
36    }
37
38    /// Sets the character in [`Cell`] at [`Self`]\[`idx`\] to `ch`.
39    pub fn set_char(&mut self, idx: usize, ch: char) {
40        self.0[idx].character = ch;
41    }
42
43    /// Util function to return the length of [`Self`].
44    #[allow(clippy::len_without_is_empty)]
45    pub const fn len(&self) -> usize {
46        self.0.len()
47    }
48
49    /// Iterates over the [`Cell`]s and resets their selected state.
50    pub fn clear_selection(&mut self) {
51        self.0.iter_mut().for_each(|cell| cell.is_selected = false);
52    }
53
54    /// Returns a reference to [`Cell`] at `idx`.
55    pub fn get_cell(&self, idx: usize) -> Option<&Cell> {
56        self.0.get(idx)
57    }
58
59    /// Returns a mutable reference to [`Cell`] at `idx`.
60    pub fn get_mut_cell(&mut self, idx: usize) -> Option<&mut Cell> {
61        self.0.get_mut(idx)
62    }
63}
64
65impl IntoIterator for Line {
66    type Item = Cell;
67    type IntoIter = std::vec::IntoIter<Self::Item>;
68
69    fn into_iter(self) -> Self::IntoIter {
70        self.0.into_iter()
71    }
72}
73
74impl<'a> IntoIterator for &'a Line {
75    type Item = &'a Cell;
76    type IntoIter = std::slice::Iter<'a, Cell>;
77
78    fn into_iter(self) -> Self::IntoIter {
79        self.0.iter()
80    }
81}
82
83impl<'a> IntoIterator for &'a mut Line {
84    type Item = &'a mut Cell;
85    type IntoIter = std::slice::IterMut<'a, Cell>;
86
87    fn into_iter(self) -> Self::IntoIter {
88        self.0.iter_mut()
89    }
90}
91
92impl Index<usize> for Line {
93    type Output = Cell;
94    fn index(&self, index: usize) -> &Self::Output {
95        &self.0[index]
96    }
97}
98
99impl IndexMut<usize> for Line {
100    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
101        &mut self.0[index]
102    }
103}