mirror of
https://github.com/alacritty/alacritty.git
synced 2025-02-24 16:06:43 -05:00
Eliminate ClearRegion trait
This commit is contained in:
parent
3619e3df2c
commit
1d7ba0588e
2 changed files with 48 additions and 47 deletions
|
@ -23,7 +23,7 @@
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::collections::{VecDeque, vec_deque};
|
use std::collections::{VecDeque, vec_deque};
|
||||||
use std::iter::IntoIterator;
|
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};
|
use index::{self, Point, Line, Column, IndexRange, RangeInclusive};
|
||||||
|
|
||||||
|
@ -207,12 +207,6 @@ impl<T> Grid<T> {
|
||||||
self.raw.swap(*src, *dst);
|
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) {
|
fn shrink_lines(&mut self, lines: index::Line) {
|
||||||
while index::Line(self.raw.len()) != lines {
|
while index::Line(self.raw.len()) != lines {
|
||||||
self.raw.pop_back();
|
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 =========================================================================================
|
// Regions =========================================================================================
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
|
@ -359,6 +331,17 @@ pub struct RegionMut<'a, T: 'a> {
|
||||||
raw: &'a mut VecDeque<Row<T>>,
|
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> {
|
pub trait IndexRegion<I, T> {
|
||||||
/// Get an immutable region of Self
|
/// Get an immutable region of Self
|
||||||
fn region<'a>(&'a self, _: I) -> Region<'a, T>;
|
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> {
|
pub struct RegionIter<'a, T: 'a> {
|
||||||
end: Line,
|
end: Line,
|
||||||
cur: Line,
|
cur: Line,
|
||||||
|
|
|
@ -24,7 +24,7 @@ use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
use font::{self, Size};
|
use font::{self, Size};
|
||||||
use ansi::{self, Color, NamedColor, Attr, Handler, CharsetIndex, StandardCharset, CursorStyle};
|
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 index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclusive};
|
||||||
use selection::{self, Span, Selection};
|
use selection::{self, Span, Selection};
|
||||||
use config::{Config, VisualBellAnimation};
|
use config::{Config, VisualBellAnimation};
|
||||||
|
@ -1087,8 +1087,12 @@ impl Term {
|
||||||
if num_lines > old_lines {
|
if num_lines > old_lines {
|
||||||
// Make sure bottom of terminal is clear
|
// Make sure bottom of terminal is clear
|
||||||
let template = self.cursor.template;
|
let template = self.cursor.template;
|
||||||
self.grid.clear_region((self.cursor.point.line + 1).., |c| c.reset(&template));
|
self.grid
|
||||||
self.alt_grid.clear_region((self.cursor_save_alt.point.line + 1).., |c| c.reset(&template));
|
.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) {
|
pub fn swap_alt(&mut self) {
|
||||||
if self.alt {
|
if self.alt {
|
||||||
let template = &self.cursor.template;
|
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;
|
self.alt = !self.alt;
|
||||||
|
@ -1133,7 +1137,9 @@ impl Term {
|
||||||
// Clear `lines` lines at bottom of area
|
// Clear `lines` lines at bottom of area
|
||||||
{
|
{
|
||||||
let start = max(origin, Line(self.scroll_region.end.0.saturating_sub(lines.0)));
|
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
|
// Scroll between origin and bottom
|
||||||
|
@ -1155,7 +1161,7 @@ impl Term {
|
||||||
// Clear `lines` lines starting from origin to origin + lines
|
// Clear `lines` lines starting from origin to origin + lines
|
||||||
{
|
{
|
||||||
let end = min(origin + lines, self.scroll_region.end);
|
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
|
// Scroll from origin to bottom less number of lines
|
||||||
|
@ -1170,7 +1176,7 @@ impl Term {
|
||||||
|
|
||||||
// Clear grid
|
// Clear grid
|
||||||
let template = self.cursor.template;
|
let template = self.cursor.template;
|
||||||
self.grid.clear(|c| c.reset(&template));
|
self.grid.region_mut(..).each(|c| c.reset(&template));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1706,25 +1712,19 @@ impl ansi::Handler for Term {
|
||||||
cell.reset(&template);
|
cell.reset(&template);
|
||||||
}
|
}
|
||||||
if self.cursor.point.line < self.grid.num_lines() - 1 {
|
if self.cursor.point.line < self.grid.num_lines() - 1 {
|
||||||
for row in self.grid.region_mut((self.cursor.point.line + 1)..) {
|
self.grid.region_mut((self.cursor.point.line + 1)..)
|
||||||
for cell in row {
|
.each(|cell| cell.reset(&template));
|
||||||
cell.reset(&template);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ansi::ClearMode::All => {
|
ansi::ClearMode::All => {
|
||||||
self.grid.clear(|c| c.reset(&template));
|
self.grid.region_mut(..).each(|c| c.reset(&template));
|
||||||
},
|
},
|
||||||
ansi::ClearMode::Above => {
|
ansi::ClearMode::Above => {
|
||||||
// If clearing more than one line
|
// If clearing more than one line
|
||||||
if self.cursor.point.line > Line(1) {
|
if self.cursor.point.line > Line(1) {
|
||||||
// Fully clear all lines before the current line
|
// Fully clear all lines before the current line
|
||||||
for row in self.grid.region_mut(..self.cursor.point.line) {
|
self.grid.region_mut(..self.cursor.point.line)
|
||||||
for cell in row {
|
.each(|cell| cell.reset(&template));
|
||||||
cell.reset(&template);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Clear up to the current column in the current line
|
// Clear up to the current column in the current line
|
||||||
let end = min(self.cursor.point.col + 1, self.grid.num_cols());
|
let end = min(self.cursor.point.col + 1, self.grid.num_cols());
|
||||||
|
|
Loading…
Add table
Reference in a new issue