1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-11 13:51:01 -05:00

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.
This commit is contained in:
Joe Wilm 2016-06-04 19:40:30 -07:00
parent 2f058bd053
commit 4fdd5280f1
No known key found for this signature in database
GPG key ID: 39B57C6972F518DA
3 changed files with 36 additions and 20 deletions

View file

@ -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<Row> {
self.raw.iter()
}
pub fn rows_mut(&mut self) -> vec_deque::IterMut<Row> {
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<Cell> {
self.0.iter()
}
pub fn cells_mut(&mut self) -> IterMut<Cell> {
self.0.iter_mut()
}
}

View file

@ -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();
}

View file

@ -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::<Vec<bool>>();
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() {