Eliminate ClearRegion trait

This commit is contained in:
Joe Wilm 2017-10-12 20:12:29 -07:00
parent 277425956f
commit 6fc0e1ec49
2 changed files with 48 additions and 47 deletions

View File

@ -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,

View File

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