diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 123e13fa..bd994033 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -23,7 +23,7 @@ use std::cmp::Ordering; use std::collections::{VecDeque, vec_deque}; use std::iter::IntoIterator; -use std::ops::{Deref, Range, RangeTo, RangeFrom, Index, IndexMut}; +use std::ops::{Deref, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut}; use index::{self, Point, Line, Column, IndexRange, RangeInclusive}; @@ -207,12 +207,6 @@ impl Grid { self.raw.swap(*src, *dst); } - #[inline] - pub fn clear(&mut self, func: F) { - let region = index::Line(0)..self.num_lines(); - self.clear_region(region, func); - } - fn shrink_lines(&mut self, lines: index::Line) { while index::Line(self.raw.len()) != lines { self.raw.pop_back(); @@ -315,28 +309,6 @@ impl<'a, T> IntoIterator for &'a Grid { } } -pub trait ClearRegion { - fn clear_region(&mut self, region: R, func: F); -} - -macro_rules! clear_region_impl { - ($range:ty) => { - impl ClearRegion<$range, T> for Grid { - fn clear_region(&mut self, region: $range, func: F) { - for row in self.region_mut(region) { - for cell in row { - func(cell); - } - } - } - } - } -} - -clear_region_impl!(Range); -clear_region_impl!(RangeTo); -clear_region_impl!(RangeFrom); - // ================================================================================================= // Regions ========================================================================================= // ================================================================================================= @@ -359,6 +331,17 @@ pub struct RegionMut<'a, T: 'a> { raw: &'a mut VecDeque>, } +impl<'a, T> RegionMut<'a, T> { + /// Call the provided function for every item in this region + pub fn each(self, func: F) { + for row in self { + for item in row { + func(item) + } + } + } +} + pub trait IndexRegion { /// Get an immutable region of Self fn region<'a>(&'a self, _: I) -> Region<'a, T>; @@ -428,6 +411,24 @@ impl IndexRegion, T> for Grid { } } +impl IndexRegion for Grid { + fn region(&self, _: RangeFull) -> Region { + Region { + start: Line(0), + end: self.num_lines(), + raw: &self.raw + } + } + + fn region_mut(&mut self, _: RangeFull) -> RegionMut { + RegionMut { + start: Line(0), + end: self.num_lines(), + raw: &mut self.raw + } + } +} + pub struct RegionIter<'a, T: 'a> { end: Line, cur: Line, diff --git a/src/term/mod.rs b/src/term/mod.rs index dd853368..fa6145f9 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -24,7 +24,7 @@ use unicode_width::UnicodeWidthChar; use font::{self, Size}; use ansi::{self, Color, NamedColor, Attr, Handler, CharsetIndex, StandardCharset, CursorStyle}; -use grid::{BidirectionalIterator, Grid, ClearRegion, ToRange, Indexed, IndexRegion}; +use grid::{BidirectionalIterator, Grid, ToRange, Indexed, IndexRegion}; use index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclusive}; use selection::{self, Span, Selection}; use config::{Config, VisualBellAnimation}; @@ -1089,8 +1089,12 @@ impl Term { if num_lines > old_lines { // Make sure bottom of terminal is clear let template = self.cursor.template; - self.grid.clear_region((self.cursor.point.line + 1).., |c| c.reset(&template)); - self.alt_grid.clear_region((self.cursor_save_alt.point.line + 1).., |c| c.reset(&template)); + self.grid + .region_mut((self.cursor.point.line + 1)..) + .each(|c| c.reset(&template)); + self.alt_grid + .region_mut((self.cursor_save_alt.point.line + 1)..) + .each(|c| c.reset(&template)); } } @@ -1113,7 +1117,7 @@ impl Term { pub fn swap_alt(&mut self) { if self.alt { let template = &self.cursor.template; - self.grid.clear(|c| c.reset(template)); + self.grid.region_mut(..).each(|c| c.reset(template)); } self.alt = !self.alt; @@ -1135,7 +1139,9 @@ impl Term { // Clear `lines` lines at bottom of area { let start = max(origin, Line(self.scroll_region.end.0.saturating_sub(lines.0))); - self.grid.clear_region(start..self.scroll_region.end, |c| c.reset(&template)); + self.grid + .region_mut(start..self.scroll_region.end) + .each(|c| c.reset(&template)); } // Scroll between origin and bottom @@ -1157,7 +1163,7 @@ impl Term { // Clear `lines` lines starting from origin to origin + lines { let end = min(origin + lines, self.scroll_region.end); - self.grid.clear_region(origin..end, |c| c.reset(&template)); + self.grid.region_mut(origin..end).each(|c| c.reset(&template)); } // Scroll from origin to bottom less number of lines @@ -1172,7 +1178,7 @@ impl Term { // Clear grid let template = self.cursor.template; - self.grid.clear(|c| c.reset(&template)); + self.grid.region_mut(..).each(|c| c.reset(&template)); } #[inline] @@ -1708,25 +1714,19 @@ impl ansi::Handler for Term { cell.reset(&template); } if self.cursor.point.line < self.grid.num_lines() - 1 { - for row in self.grid.region_mut((self.cursor.point.line + 1)..) { - for cell in row { - cell.reset(&template); - } - } + self.grid.region_mut((self.cursor.point.line + 1)..) + .each(|cell| cell.reset(&template)); } }, ansi::ClearMode::All => { - self.grid.clear(|c| c.reset(&template)); + self.grid.region_mut(..).each(|c| c.reset(&template)); }, ansi::ClearMode::Above => { // If clearing more than one line if self.cursor.point.line > Line(1) { // Fully clear all lines before the current line - for row in self.grid.region_mut(..self.cursor.point.line) { - for cell in row { - cell.reset(&template); - } - } + self.grid.region_mut(..self.cursor.point.line) + .each(|cell| cell.reset(&template)); } // Clear up to the current column in the current line let end = min(self.cursor.point.col + 1, self.grid.num_cols());