From 4fdd5280f1e79ea6575a6a110951c564a7dd235e Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Sat, 4 Jun 2016 19:40:30 -0700 Subject: [PATCH] Add iterator methods to Grid and Row types The iterator methods simplify logic in the main grid render function. To disambiguate iterator methods from those returning counts (and to free up names), the `rows()` and `cols()` methods on `Grid` have been renamed to `num_rows()` and `num_cols()`, respectively. --- src/grid.rs | 24 ++++++++++++++++++------ src/renderer/mod.rs | 20 ++++++++++++-------- src/term.rs | 12 ++++++------ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/grid.rs b/src/grid.rs index 765a5e1a..a3009f8b 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -1,8 +1,8 @@ //! Functions for computing properties of the terminal grid -use std::collections::VecDeque; - +use std::collections::{vec_deque, VecDeque}; use std::ops::{Index, IndexMut, Deref, DerefMut}; +use std::slice::{Iter, IterMut}; use term::Cursor; use ::Rgb; @@ -60,11 +60,19 @@ impl Grid { } } - pub fn rows(&self) -> usize { + pub fn rows(&self) -> vec_deque::Iter { + self.raw.iter() + } + + pub fn rows_mut(&mut self) -> vec_deque::IterMut { + self.raw.iter_mut() + } + + pub fn num_rows(&self) -> usize { self.rows } - pub fn cols(&self) -> usize { + pub fn num_cols(&self) -> usize { self.cols } @@ -130,8 +138,12 @@ impl Row { Row(vec![Cell::new(' '); columns]) } - pub fn cols(&self) -> usize { - self.0.len() + pub fn cells(&self) -> Iter { + self.0.iter() + } + + pub fn cells_mut(&mut self) -> IterMut { + self.0.iter_mut() } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index c983beba..2ba5b94c 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -182,17 +182,21 @@ impl QuadRenderer { pub fn render_grid(&mut self, grid: &Grid, glyph_cache: &GlyphCache, props: &TermProps) { self.prepare_render(props); - for i in 0..grid.rows() { - let row = &grid[i]; - for j in 0..row.cols() { - let cell = &row[j]; - if cell.c != ' ' { - if let Some(glyph) = glyph_cache.get(&cell.c) { - self.render(glyph, i as f32, j as f32, &cell.fg, cell.c); - } + + for (i, row) in grid.rows().enumerate() { + for (j, cell) in row.cells().enumerate() { + // Skip empty cells + if cell.c == ' ' { + continue; + } + + // Render if glyph is loaded + if let Some(glyph) = glyph_cache.get(&cell.c) { + self.render(glyph, i as f32, j as f32, &cell.fg, cell.c); } } } + self.finish_render(); } diff --git a/src/term.rs b/src/term.rs index 35088db7..2838c668 100644 --- a/src/term.rs +++ b/src/term.rs @@ -95,7 +95,7 @@ pub struct Term { impl Term { pub fn new(tty: tty::Tty, grid: Grid) -> Term { - let mut tabs = (0..grid.cols()).map(|i| i % TAB_SPACES == 0) + let mut tabs = (0..grid.num_cols()).map(|i| i % TAB_SPACES == 0) .collect::>(); tabs[0] = false; @@ -206,10 +206,10 @@ impl ansi::Handler for Term { println!("put_tab: {}", count); let mut x = self.cursor_x(); - while x < self.grid.cols() as u16 && count != 0 { + while x < self.grid.num_cols() as u16 && count != 0 { count -= 1; loop { - if x == self.grid.cols() as u16 || self.tabs[x as usize] { + if x == self.grid.num_cols() as u16 || self.tabs[x as usize] { break; } x += 1; @@ -237,7 +237,7 @@ impl ansi::Handler for Term { fn linefeed(&mut self) { println!("linefeed"); // TODO handle scroll? not clear what parts of this the pty handle - if self.cursor_y() + 1 == self.grid.rows() as u16 { + if self.cursor_y() + 1 == self.grid.num_rows() as u16 { self.grid.feed(); self.clear_line(ansi::LineClearMode::Right); } else { @@ -264,7 +264,7 @@ impl ansi::Handler for Term { println!("clear_line: {:?}", mode); match mode { ansi::LineClearMode::Right => { - let cols = self.grid.cols(); + let cols = self.grid.num_cols(); let row = &mut self.grid[self.cursor.y as usize]; let start = self.cursor.x as usize; for col in start..cols { @@ -279,7 +279,7 @@ impl ansi::Handler for Term { match mode { ansi::ClearMode::Below => { let start = self.cursor_y() as usize; - let end = self.grid.rows(); + let end = self.grid.num_rows(); for i in start..end { let row = &mut self.grid[i]; for cell in row.iter_mut() {