diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-04 09:08:38 +0000 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-04 09:08:38 +0000 |
commit | d52d752af9a4bde993af7b0cb871e60b41eca2e0 (patch) | |
tree | 015d6392684beb19a53083cc3015bd13055e76bf | |
parent | 3447af1b2197235ff68d3b228f9fd0b2a1a133c3 (diff) | |
download | vt100-rust-d52d752af9a4bde993af7b0cb871e60b41eca2e0.tar.gz vt100-rust-d52d752af9a4bde993af7b0cb871e60b41eca2e0.zip |
stop reallocating vecs so much
-rw-r--r-- | src/attrs.rs | 6 | ||||
-rw-r--r-- | src/cell.rs | 5 | ||||
-rw-r--r-- | src/grid.rs | 22 | ||||
-rw-r--r-- | src/row.rs | 5 | ||||
-rw-r--r-- | src/screen.rs | 8 |
5 files changed, 33 insertions, 13 deletions
diff --git a/src/attrs.rs b/src/attrs.rs index ef7bf38..2fffd2b 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -27,6 +27,12 @@ pub struct Attrs { } impl Attrs { + pub fn clear(&mut self) { + self.fgcolor = Color::default(); + self.bgcolor = Color::default(); + self.mode = enumset::EnumSet::default(); + } + pub fn bold(&self) -> bool { self.mode.contains(TextMode::Bold) } diff --git a/src/cell.rs b/src/cell.rs index 1151748..2f83d73 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -26,8 +26,9 @@ impl Cell { } } - pub(crate) fn reset(&mut self) { - *self = Self::default(); + pub(crate) fn clear(&mut self) { + self.contents.clear(); + self.attrs.clear(); } pub fn contents(&self) -> &str { diff --git a/src/grid.rs b/src/grid.rs index 20fe17f..b277e34 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -27,6 +27,18 @@ impl Grid { crate::row::Row::new(self.size.cols) } + pub fn clear(&mut self) { + self.pos = Pos::default(); + self.saved_pos = Pos::default(); + for row in self.rows_mut() { + row.clear(); + } + self.scroll_top = 0; + self.scroll_bottom = self.size.rows - 1; + self.origin_mode = false; + self.saved_origin_mode = false; + } + pub fn size(&self) -> Size { self.size } @@ -141,7 +153,9 @@ impl Grid { } pub fn erase_all(&mut self) { - self.rows = vec![self.new_row(); self.size.rows as usize]; + for row in self.rows_mut() { + row.clear(); + } } pub fn erase_all_forward(&mut self) { @@ -171,7 +185,7 @@ impl Grid { let row = self.current_row_mut(); row.wrap(false); for cell in row.cells_mut().skip(pos.col as usize) { - *cell = crate::cell::Cell::default(); + cell.clear(); } } @@ -179,7 +193,7 @@ impl Grid { let pos = self.pos; let row = self.current_row_mut(); for cell in row.cells_mut().take(pos.col as usize + 1) { - *cell = crate::cell::Cell::default(); + cell.clear(); } } @@ -209,7 +223,7 @@ impl Grid { for cell in row.cells_mut().skip(pos.col as usize).take(count as usize) { - *cell = crate::cell::Cell::default(); + cell.clear(); } } @@ -15,7 +15,10 @@ impl Row { } pub fn clear(&mut self) { - *self = Self::new(self.cells.len().try_into().unwrap()); + for cell in &mut self.cells { + cell.clear(); + } + self.wrapped = false; } pub fn cells_mut( diff --git a/src/screen.rs b/src/screen.rs index d40e9ea..d29c907 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -83,10 +83,6 @@ impl State { } } - fn new_grid(&self) -> crate::grid::Grid { - crate::grid::Grid::new(self.grid().size()) - } - fn grid(&self) -> &crate::grid::Grid { if self.mode(Mode::AlternateScreen) { &self.alternate_grid @@ -198,7 +194,7 @@ impl State { }) .unwrap(); if prev_cell.is_wide() { - prev_cell.reset(); + prev_cell.clear(); } } @@ -442,7 +438,7 @@ impl State { 1006 => self.set_mouse_encoding(MouseProtocolEncoding::Sgr), 1049 => { self.decsc(); - self.alternate_grid = self.new_grid(); + self.alternate_grid.clear(); self.enter_alternate_grid(); } 2004 => self.set_mode(Mode::BracketedPaste), |