sericom_core/screen_buffer/
line.rs1use super::Cell;
2use std::ops::{Index, IndexMut};
3
4#[derive(Clone, Debug)]
6pub struct Line(Vec<Cell>);
7
8impl Line {
9 pub fn new(width: usize) -> Self {
13 Self(vec![Cell::default(); width])
14 }
15
16 pub fn reset(&mut self) {
18 self.0.iter_mut().for_each(|cell| *cell = Cell::default());
19 }
20
21 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 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 pub fn set_char(&mut self, idx: usize, ch: char) {
40 self.0[idx].character = ch;
41 }
42
43 #[allow(clippy::len_without_is_empty)]
45 pub const fn len(&self) -> usize {
46 self.0.len()
47 }
48
49 pub fn clear_selection(&mut self) {
51 self.0.iter_mut().for_each(|cell| cell.is_selected = false);
52 }
53
54 pub fn get_cell(&self, idx: usize) -> Option<&Cell> {
56 self.0.get(idx)
57 }
58
59 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}