aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2017-10-12 20:12:29 -0700
committerJoe Wilm <joe@jwilm.com>2018-03-07 09:45:25 -0800
commit971e4b2dc1f9c6a79256d747c30f9986076dd571 (patch)
treedcd88686c68bf0ca92adab697bacbe3bdcea1791
parent65fba0251677efbad2f9242e60ac86e4d5c7f35d (diff)
downloadalacritty-971e4b2dc1f9c6a79256d747c30f9986076dd571.tar.gz
alacritty-971e4b2dc1f9c6a79256d747c30f9986076dd571.zip
Eliminate ClearRegion trait
-rw-r--r--src/grid/mod.rs59
-rw-r--r--src/term/mod.rs36
2 files changed, 48 insertions, 47 deletions
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<T> Grid<T> {
self.raw.swap(*src, *dst);
}
- #[inline]
- pub fn clear<F: Fn(&mut T)>(&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<T> {
}
}
-pub trait ClearRegion<R, T> {
- fn clear_region<F: Fn(&mut T)>(&mut self, region: R, func: F);
-}
-
-macro_rules! clear_region_impl {
- ($range:ty) => {
- impl<T> ClearRegion<$range, T> for Grid<T> {
- fn clear_region<F: Fn(&mut T)>(&mut self, region: $range, func: F) {
- for row in self.region_mut(region) {
- for cell in row {
- func(cell);
- }
- }
- }
- }
- }
-}
-
-clear_region_impl!(Range<index::Line>);
-clear_region_impl!(RangeTo<index::Line>);
-clear_region_impl!(RangeFrom<index::Line>);
-
// =================================================================================================
// Regions =========================================================================================
// =================================================================================================
@@ -359,6 +331,17 @@ pub struct RegionMut<'a, T: 'a> {
raw: &'a mut VecDeque<Row<T>>,
}
+impl<'a, T> RegionMut<'a, T> {
+ /// Call the provided function for every item in this region
+ pub fn each<F: Fn(&mut T)>(self, func: F) {
+ for row in self {
+ for item in row {
+ func(item)
+ }
+ }
+ }
+}
+
pub trait IndexRegion<I, T> {
/// Get an immutable region of Self
fn region<'a>(&'a self, _: I) -> Region<'a, T>;
@@ -428,6 +411,24 @@ impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> {
}
}
+impl<T> IndexRegion<RangeFull, T> for Grid<T> {
+ fn region(&self, _: RangeFull) -> Region<T> {
+ Region {
+ start: Line(0),
+ end: self.num_lines(),
+ raw: &self.raw
+ }
+ }
+
+ fn region_mut(&mut self, _: RangeFull) -> RegionMut<T> {
+ 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 c9e3d9b2..2bf5f9c1 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};
@@ -1087,8 +1087,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));
}
}
@@ -1111,7 +1115,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;
@@ -1133,7 +1137,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
@@ -1155,7 +1161,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
@@ -1170,7 +1176,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]
@@ -1706,25 +1712,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());