diff options
Diffstat (limited to 'src/grid/mod.rs')
-rw-r--r-- | src/grid/mod.rs | 159 |
1 files changed, 48 insertions, 111 deletions
diff --git a/src/grid/mod.rs b/src/grid/mod.rs index a1903535..3a6bacf8 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -14,10 +14,10 @@ //! A specialized 2d grid implementation optimized for use in a terminal. -use std::cmp::{min, max, Ordering}; -use std::ops::{Deref, Range, Index, IndexMut, RangeTo, RangeFrom, RangeFull, RangeInclusive}; +use std::cmp::{max, min, Ordering}; +use std::ops::{Deref, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo}; -use crate::index::{self, Point, Line, Column, IndexRange}; +use crate::index::{self, Column, IndexRange, Line, Point}; use crate::selection::Selection; mod row; @@ -55,13 +55,13 @@ impl<T> Deref for Indexed<T> { impl<T: PartialEq> ::std::cmp::PartialEq for Grid<T> { fn eq(&self, other: &Self) -> bool { // Compare struct fields and check result of grid comparison - self.raw.eq(&other.raw) && - self.cols.eq(&other.cols) && - self.lines.eq(&other.lines) && - self.display_offset.eq(&other.display_offset) && - self.scroll_limit.eq(&other.scroll_limit) && - self.selection.eq(&other.selection) && - self.url_highlight.eq(&other.url_highlight) + self.raw.eq(&other.raw) + && self.cols.eq(&other.cols) + && self.lines.eq(&other.lines) + && self.display_offset.eq(&other.display_offset) + && self.scroll_limit.eq(&other.scroll_limit) + && self.selection.eq(&other.selection) + && self.url_highlight.eq(&other.url_highlight) } } @@ -142,10 +142,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { } pub fn visible_to_buffer(&self, point: Point) -> Point<usize> { - Point { - line: self.visible_line_to_buffer(point.line), - col: point.col - } + Point { line: self.visible_line_to_buffer(point.line), col: point.col } } pub fn buffer_line_to_visible(&self, line: usize) -> ViewportPosition { @@ -164,8 +161,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { } /// Update the size of the scrollback history - pub fn update_history(&mut self, history_size: usize, template: &T) - { + pub fn update_history(&mut self, history_size: usize, template: &T) { self.raw.update_history(history_size, Row::new(self.cols, &template)); self.max_scroll_limit = history_size; self.scroll_limit = min(self.scroll_limit, history_size); @@ -177,20 +173,14 @@ impl<T: GridCell + Copy + Clone> Grid<T> { Scroll::Lines(count) => { self.display_offset = min( max((self.display_offset as isize) + count, 0isize) as usize, - self.scroll_limit + self.scroll_limit, ); }, Scroll::PageUp => { - self.display_offset = min( - self.display_offset + self.lines.0, - self.scroll_limit - ); + self.display_offset = min(self.display_offset + self.lines.0, self.scroll_limit); }, Scroll::PageDown => { - self.display_offset -= min( - self.display_offset, - self.lines.0 - ); + self.display_offset -= min(self.display_offset, self.lines.0); }, Scroll::Top => self.display_offset = self.scroll_limit, Scroll::Bottom => self.display_offset = 0, @@ -222,8 +212,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { } } - fn increase_scroll_limit(&mut self, count: usize, template: &T) - { + fn increase_scroll_limit(&mut self, count: usize, template: &T) { self.scroll_limit = min(self.scroll_limit + count, self.max_scroll_limit); // Initialize new lines when the history buffer is smaller than the scroll limit @@ -246,11 +235,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { /// Alacritty keeps the cursor at the bottom of the terminal as long as there /// is scrollback available. Once scrollback is exhausted, new lines are /// simply added to the bottom of the screen. - fn grow_lines( - &mut self, - new_line_count: index::Line, - template: &T, - ) { + fn grow_lines(&mut self, new_line_count: index::Line, template: &T) { let lines_added = new_line_count - self.lines; // Need to "resize" before updating buffer @@ -435,7 +420,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { // Now, restore any scroll region lines let lines = self.lines; - for i in IndexRange(region.end .. lines) { + for i in IndexRange(region.end..lines) { self.raw.swap_lines(i, i + positions); } @@ -449,7 +434,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { self.raw.swap_lines(line, line - positions); } - for line in IndexRange(region.start .. (region.start + positions)) { + for line in IndexRange(region.start..(region.start + positions)) { self.raw[line].reset(&template); } } @@ -458,19 +443,12 @@ impl<T: GridCell + Copy + Clone> Grid<T> { /// scroll_up moves lines at the bottom towards the top /// /// This is the performance-sensitive part of scrolling. - pub fn scroll_up( - &mut self, - region: &Range<index::Line>, - positions: index::Line, - template: &T - ) { + pub fn scroll_up(&mut self, region: &Range<index::Line>, positions: index::Line, template: &T) { if region.start == Line(0) { // Update display offset when not pinned to active area if self.display_offset != 0 { - self.display_offset = min( - self.display_offset + *positions, - self.len() - self.num_lines().0, - ); + self.display_offset = + min(self.display_offset + *positions, self.len() - self.num_lines().0); } self.increase_scroll_limit(*positions, template); @@ -506,7 +484,7 @@ impl<T: GridCell + Copy + Clone> Grid<T> { } // Clear reused lines - for line in IndexRange((region.end - positions) .. region.end) { + for line in IndexRange((region.end - positions)..region.end) { self.raw[line].reset(&template); } } @@ -569,7 +547,7 @@ impl<T> Grid<T> { /// This is used only for initializing after loading ref-tests pub fn initialize_all(&mut self, template: &T) where - T: Copy + T: Copy, { let history_size = self.raw.len().saturating_sub(*self.lines); self.raw.initialize(self.max_scroll_limit - history_size, Row::new(self.cols, template)); @@ -581,10 +559,7 @@ impl<T> Grid<T> { } pub fn iter_from(&self, point: Point<usize>) -> GridIterator<'_, T> { - GridIterator { - grid: self, - cur: point, - } + GridIterator { grid: self, cur: point } } #[inline] @@ -613,8 +588,7 @@ impl<'a, T> Iterator for GridIterator<'a, T> { let last_col = self.grid.num_cols() - Column(1); match self.cur { Point { line, col } if line == 0 && col == last_col => None, - Point { col, .. } if - (col == last_col) => { + Point { col, .. } if (col == last_col) => { self.cur.line -= 1; self.cur.col = Column(0); Some(&self.grid[self.cur.line][self.cur.col]) @@ -622,7 +596,7 @@ impl<'a, T> Iterator for GridIterator<'a, T> { _ => { self.cur.col += Column(1); Some(&self.grid[self.cur.line][self.cur.col]) - } + }, } } } @@ -641,7 +615,7 @@ impl<'a, T> BidirectionalIterator for GridIterator<'a, T> { _ => { self.cur.col -= Column(1); Some(&self.grid[self.cur.line][self.cur.col]) - } + }, } } } @@ -742,77 +716,48 @@ impl<T> IndexRegion<Range<Line>, T> for Grid<T> { assert!(index.start < self.num_lines()); assert!(index.end <= self.num_lines()); assert!(index.start <= index.end); - Region { - start: index.start, - end: index.end, - raw: &self.raw - } + Region { start: index.start, end: index.end, raw: &self.raw } } + fn region_mut(&mut self, index: Range<Line>) -> RegionMut<'_, T> { assert!(index.start < self.num_lines()); assert!(index.end <= self.num_lines()); assert!(index.start <= index.end); - RegionMut { - start: index.start, - end: index.end, - raw: &mut self.raw - } + RegionMut { start: index.start, end: index.end, raw: &mut self.raw } } } impl<T> IndexRegion<RangeTo<Line>, T> for Grid<T> { fn region(&self, index: RangeTo<Line>) -> Region<'_, T> { assert!(index.end <= self.num_lines()); - Region { - start: Line(0), - end: index.end, - raw: &self.raw - } + Region { start: Line(0), end: index.end, raw: &self.raw } } + fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<'_, T> { assert!(index.end <= self.num_lines()); - RegionMut { - start: Line(0), - end: index.end, - raw: &mut self.raw - } + RegionMut { start: Line(0), end: index.end, raw: &mut self.raw } } } impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> { fn region(&self, index: RangeFrom<Line>) -> Region<'_, T> { assert!(index.start < self.num_lines()); - Region { - start: index.start, - end: self.num_lines(), - raw: &self.raw - } + Region { start: index.start, end: self.num_lines(), raw: &self.raw } } + fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<'_, T> { assert!(index.start < self.num_lines()); - RegionMut { - start: index.start, - end: self.num_lines(), - raw: &mut self.raw - } + RegionMut { start: index.start, end: self.num_lines(), raw: &mut self.raw } } } 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 - } + 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 - } + RegionMut { start: Line(0), end: self.num_lines(), raw: &mut self.raw } } } @@ -829,33 +774,26 @@ pub struct RegionIterMut<'a, T> { } impl<'a, T> IntoIterator for Region<'a, T> { - type Item = &'a Row<T>; type IntoIter = RegionIter<'a, T>; + type Item = &'a Row<T>; fn into_iter(self) -> Self::IntoIter { - RegionIter { - end: self.end, - cur: self.start, - raw: self.raw - } + RegionIter { end: self.end, cur: self.start, raw: self.raw } } } impl<'a, T> IntoIterator for RegionMut<'a, T> { - type Item = &'a mut Row<T>; type IntoIter = RegionIterMut<'a, T>; + type Item = &'a mut Row<T>; fn into_iter(self) -> Self::IntoIter { - RegionIterMut { - end: self.end, - cur: self.start, - raw: self.raw - } + RegionIterMut { end: self.end, cur: self.start, raw: self.raw } } } impl<'a, T> Iterator for RegionIter<'a, T> { type Item = &'a Row<T>; + fn next(&mut self) -> Option<Self::Item> { if self.cur < self.end { let index = self.cur; @@ -869,13 +807,12 @@ impl<'a, T> Iterator for RegionIter<'a, T> { impl<'a, T> Iterator for RegionIterMut<'a, T> { type Item = &'a mut Row<T>; + fn next(&mut self) -> Option<Self::Item> { if self.cur < self.end { let index = self.cur; self.cur += 1; - unsafe { - Some(&mut *(&mut self.raw[index] as *mut _)) - } + unsafe { Some(&mut *(&mut self.raw[index] as *mut _)) } } else { None } @@ -898,7 +835,7 @@ pub struct DisplayIter<'a, T> { impl<'a, T: 'a> DisplayIter<'a, T> { pub fn new(grid: &'a Grid<T>) -> DisplayIter<'a, T> { let offset = grid.display_offset + *grid.num_lines() - 1; - let limit = grid.display_offset; + let limit = grid.display_offset; let col = Column(0); let line = Line(0); @@ -932,7 +869,7 @@ impl<'a, T: Copy + 'a> Iterator for DisplayIter<'a, T> { let item = Some(Indexed { inner: self.grid.raw[self.offset][self.col], line: self.line, - column: self.col + column: self.col, }); // Update line/col to point to next item |