// Copyright 2016 Joe Wilm, The Alacritty Project Contributors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! A generic 2d grid implementation optimized for use in a terminal. //! //! The current implementation uses a vector of vectors to store cell data. //! Reimplementing the store as a single contiguous vector may be desirable in //! the future. Rotation and indexing would need to be reconsidered at that //! time; rotation currently reorganize Vecs in the lines Vec, and indexing with //! ranges is currently supported. use std::borrow::ToOwned; use std::cmp::Ordering; use std::collections::{VecDeque, vec_deque}; use std::iter::IntoIterator; use std::ops::{Deref, DerefMut, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut}; use std::slice; use index::{self, Point, Line, Column, IndexRange, RangeInclusive}; /// Convert a type to a linear index range. pub trait ToRange { fn to_range(&self) -> RangeInclusive; } /// Bidirection iterator pub trait BidirectionalIterator: Iterator { fn prev(&mut self) -> Option; } pub struct Indexed { pub line: Line, pub column: Column, pub inner: T } impl Deref for Indexed { type Target = T; #[inline] fn deref(&self) -> &T { &self.inner } } /// Represents the terminal display contents #[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] pub struct Grid { /// Lines in the grid. Each row holds a list of cells corresponding to the /// columns in that row. raw: VecDeque>, /// Number of columns cols: index::Column, /// Number of lines. /// /// Invariant: lines is equivalent to raw.len() lines: index::Line, } pub struct GridIterator<'a, T: 'a> { grid: &'a Grid, pub cur: Point, } impl Grid { pub fn new(lines: index::Line, cols: index::Column, template: &T) -> Grid { let mut raw = VecDeque::with_capacity(*lines); for _ in IndexRange(index::Line(0)..lines) { raw.push_back(Row::new(cols, template)); } Grid { raw, cols, lines, } } pub fn resize(&mut self, lines: index::Line, cols: index::Column, template: &T) { // Check that there's actually work to do and return early if not if lines == self.lines && cols == self.cols { return; } match self.lines.cmp(&lines) { Ordering::Less => self.grow_lines(lines, template), Ordering::Greater => self.shrink_lines(lines), Ordering::Equal => (), } match self.cols.cmp(&cols) { Ordering::Less => self.grow_cols(cols, template), Ordering::Greater => self.shrink_cols(cols), Ordering::Equal => (), } } fn grow_lines(&mut self, lines: index::Line, template: &T) { for _ in IndexRange(self.num_lines()..lines) { self.raw.push_back(Row::new(self.cols, template)); } self.lines = lines; } fn grow_cols(&mut self, cols: index::Column, template: &T) { for row in self.lines_mut() { row.grow(cols, template); } self.cols = cols; } } /// A subset of lines in the grid /// /// May be constructed using Grid::region(..) pub struct Region<'a, T: 'a> { start: Line, end: Line, raw: &'a VecDeque>, } /// A mutable subset of lines in the grid /// /// May be constructed using Grid::region_mut(..) pub struct RegionMut<'a, T: 'a> { start: Line, end: Line, raw: &'a mut VecDeque>, } pub trait IndexRegion { /// Get an immutable region of Self fn region<'a>(&'a self, _: I) -> Region<'a, T>; /// Get a mutable region of Self fn region_mut<'a>(&'a mut self, _: I) -> RegionMut<'a, T>; } impl IndexRegion, T> for Grid { fn region(&self, index: Range) -> Region { 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 } } fn region_mut(&mut self, index: Range) -> RegionMut { 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 } } } impl IndexRegion, T> for Grid { fn region(&self, index: RangeTo) -> Region { assert!(index.end <= self.num_lines()); Region { start: Line(0), end: index.end, raw: &self.raw } } fn region_mut(&mut self, index: RangeTo) -> RegionMut { assert!(index.end <= self.num_lines()); RegionMut { start: Line(0), end: index.end, raw: &mut self.raw } } } impl IndexRegion, T> for Grid { fn region(&self, index: RangeFrom) -> Region { assert!(index.start < self.num_lines()); Region { start: index.start, end: self.num_lines(), raw: &self.raw } } fn region_mut(&mut self, index: RangeFrom) -> RegionMut { assert!(index.start < self.num_lines()); RegionMut { start: index.start, end: self.num_lines(), raw: &mut self.raw } } } pub struct RegionIter<'a, T: 'a> { end: Line, cur: Line, raw: &'a VecDeque>, } pub struct RegionIterMut<'a, T: 'a> { end: Line, cur: Line, raw: &'a mut VecDeque>, } impl<'a, T> IntoIterator for Region<'a, T> { type Item = &'a Row; type IntoIter = RegionIter<'a, T>; fn into_iter(self) -> Self::IntoIter { RegionIter { end: self.end, cur: self.start, raw: self.raw } } } impl<'a, T> IntoIterator for RegionMut<'a, T> { type Item = &'a mut Row; type IntoIter = RegionIterMut<'a, T>; fn into_iter(self) -> Self::IntoIter { RegionIterMut { end: self.end, cur: self.start, raw: self.raw } } } impl<'a, T> Iterator for RegionIter<'a, T> { type Item = &'a Row; fn next(&mut self) -> Option { if self.cur < self.end { let index = self.cur; self.cur += 1; Some(&self.raw[*index]) } else { None } } } impl<'a, T> Iterator for RegionIterMut<'a, T> { type Item = &'a mut Row; fn next(&mut self) -> Option { if self.cur < self.end { let index = self.cur; self.cur += 1; unsafe { Some(&mut *(&mut self.raw[index.0] as *mut _)) } } else { None } } } impl Grid { #[inline] pub fn lines(&self) -> vec_deque::Iter> { self.raw.iter() } #[inline] pub fn lines_mut(&mut self) -> vec_deque::IterMut> { self.raw.iter_mut() } #[inline] pub fn num_lines(&self) -> index::Line { self.lines } #[inline] pub fn num_cols(&self) -> index::Column { self.cols } #[inline] pub fn scroll_down(&mut self, region: &Range, positions: index::Line) { if region.start == Line(0) && region.end == self.num_lines() { // Full rotation for _ in 0..positions.0 { let item = self.raw.pop_back().unwrap(); self.raw.push_front(item); } } else { // Subregion rotation for line in IndexRange((region.start + positions)..region.end).rev() { self.swap_lines(line, line - positions); } } } #[inline] pub fn scroll_up(&mut self, region: &Range, positions: index::Line) { if region.start == Line(0) && region.end == self.num_lines() { // Full rotation for _ in 0..positions.0 { let item = self.raw.pop_front().unwrap(); self.raw.push_back(item); } } else { // Subregion rotation for line in IndexRange(region.start..(region.end - positions)) { self.swap_lines(line, line + positions); } } } pub fn iter_from(&self, point: Point) -> GridIterator { GridIterator { grid: self, cur: point, } } #[inline] pub fn contains(&self, point: &Point) -> bool { self.lines > point.line && self.cols > point.col } /// Swap two lines in the grid /// /// This could have used slice::swap internally, but we are able to have /// better error messages by doing the bounds checking ourselves. #[inline] pub fn swap_lines(&mut self, src: index::Line, dst: index::Line) { 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(); } self.lines = lines; } fn shrink_cols(&mut self, cols: index::Column) { for row in self.lines_mut() { row.shrink(cols); } self.cols = cols; } } impl<'a, T> Iterator for GridIterator<'a, T> { type Item = &'a T; fn next(&mut self) -> Option { let last_line = self.grid.num_lines() - Line(1); let last_col = self.grid.num_cols() - Column(1); match self.cur { Point { line, col } if (line == last_line) && (col == last_col) => None, Point { col, .. } if (col == last_col) => { self.cur.line += Line(1); self.cur.col = Column(0); Some(&self.grid[self.cur.line][self.cur.col]) }, _ => { self.cur.col += Column(1); Some(&self.grid[self.cur.line][self.cur.col]) } } } } impl<'a, T> BidirectionalIterator for GridIterator<'a, T> { fn prev(&mut self) -> Option { let num_cols = self.grid.num_cols(); match self.cur { Point { line: Line(0), col: Column(0) } => None, Point { col: Column(0), .. } => { self.cur.line -= Line(1); self.cur.col = num_cols - Column(1); Some(&self.grid[self.cur.line][self.cur.col]) }, _ => { self.cur.col -= Column(1); Some(&self.grid[self.cur.line][self.cur.col]) } } } } impl Index for Grid { type Output = Row; #[inline] fn index(&self, index: index::Line) -> &Row { &self.raw[index.0] } } impl IndexMut for Grid { #[inline] fn index_mut(&mut self, index: index::Line) -> &mut Row { &mut self.raw[index.0] } } impl<'point, T> Index<&'point Point> for Grid { type Output = T; #[inline] fn index<'a>(&'a self, point: &Point) -> &'a T { &self.raw[point.line.0][point.col] } } impl<'point, T> IndexMut<&'point Point> for Grid { #[inline] fn index_mut<'a, 'b>(&'a mut self, point: &'b Point) -> &'a mut T { &mut self.raw[point.line.0][point.col] } } /// A row in the grid #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct Row(Vec); impl Row { pub fn new(columns: index::Column, template: &T) -> Row { Row(vec![template.to_owned(); *columns]) } pub fn grow(&mut self, cols: index::Column, template: &T) { while self.len() != *cols { self.push(template.to_owned()); } } } impl Row { pub fn shrink(&mut self, cols: index::Column) { while self.len() != *cols { self.pop(); } } #[inline] pub fn cells(&self) -> slice::Iter { self.0.iter() } #[inline] pub fn cells_mut(&mut self) -> slice::IterMut { self.0.iter_mut() } } impl<'a, T> IntoIterator for &'a Grid { type Item = &'a Row; type IntoIter = vec_deque::Iter<'a, Row>; #[inline] fn into_iter(self) -> vec_deque::Iter<'a, Row> { self.raw.iter() } } impl<'a, T> IntoIterator for &'a Row { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; #[inline] fn into_iter(self) -> slice::Iter<'a, T> { self.iter() } } impl<'a, T> IntoIterator for &'a mut Row { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; #[inline] fn into_iter(self) -> slice::IterMut<'a, T> { self.iter_mut() } } impl Deref for Row { type Target = Vec; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Row { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Index for Row { type Output = T; #[inline] fn index(&self, index: index::Column) -> &T { &self.0[index.0] } } impl IndexMut for Row { #[inline] fn index_mut(&mut self, index: index::Column) -> &mut T { &mut self.0[index.0] } } macro_rules! row_index_range { ($range:ty) => { impl Index<$range> for Row { type Output = [T]; #[inline] fn index(&self, index: $range) -> &[T] { &self.0[index] } } impl IndexMut<$range> for Row { #[inline] fn index_mut(&mut self, index: $range) -> &mut [T] { &mut self.0[index] } } } } row_index_range!(Range); row_index_range!(RangeTo); row_index_range!(RangeFrom); row_index_range!(RangeFull); // ----------------------------------------------------------------------------- // Column ranges for Row // ----------------------------------------------------------------------------- impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: Range) -> &[T] { &self.0[(index.start.0)..(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: Range) -> &mut [T] { &mut self.0[(index.start.0)..(index.end.0)] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeTo) -> &[T] { &self.0[..(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeTo) -> &mut [T] { &mut self.0[..(index.end.0)] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeFrom) -> &[T] { &self.0[(index.start.0)..] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeFrom) -> &mut [T] { &mut self.0[(index.start.0)..] } } 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); #[cfg(test)] mod tests { use super::{Grid, BidirectionalIterator}; use index::{Point, Line, Column}; #[test] fn grid_swap_lines_ok() { let mut grid = Grid::new(Line(10), Column(1), &0); info!(""); // swap test ends grid[Line(0)][Column(0)] = 1; grid[Line(9)][Column(0)] = 2; assert_eq!(grid[Line(0)][Column(0)], 1); assert_eq!(grid[Line(9)][Column(0)], 2); grid.swap_lines(Line(0), Line(9)); assert_eq!(grid[Line(0)][Column(0)], 2); assert_eq!(grid[Line(9)][Column(0)], 1); // swap test mid grid[Line(4)][Column(0)] = 1; grid[Line(5)][Column(0)] = 2; info!("grid: {:?}", grid); assert_eq!(grid[Line(4)][Column(0)], 1); assert_eq!(grid[Line(5)][Column(0)], 2); grid.swap_lines(Line(4), Line(5)); info!("grid: {:?}", grid); assert_eq!(grid[Line(4)][Column(0)], 2); assert_eq!(grid[Line(5)][Column(0)], 1); } #[test] #[should_panic] fn grid_swap_lines_oob1() { let mut grid = Grid::new(Line(10), Column(1), &0); grid.swap_lines(Line(0), Line(10)); } #[test] #[should_panic] fn grid_swap_lines_oob2() { let mut grid = Grid::new(Line(10), Column(1), &0); grid.swap_lines(Line(10), Line(0)); } #[test] #[should_panic] fn grid_swap_lines_oob3() { let mut grid = Grid::new(Line(10), Column(1), &0); grid.swap_lines(Line(10), Line(10)); } // Scroll up moves lines upwards #[test] fn scroll_up() { info!(""); let mut grid = Grid::new(Line(10), Column(1), &0); for i in 0..10 { grid[Line(i)][Column(0)] = i; } info!("grid: {:?}", grid); grid.scroll_up(&(Line(0)..Line(10)), Line(2)); info!("grid: {:?}", grid); let mut other = Grid::new(Line(10), Column(1), &9); other[Line(0)][Column(0)] = 2; other[Line(1)][Column(0)] = 3; other[Line(2)][Column(0)] = 4; other[Line(3)][Column(0)] = 5; other[Line(4)][Column(0)] = 6; other[Line(5)][Column(0)] = 7; other[Line(6)][Column(0)] = 8; other[Line(7)][Column(0)] = 9; other[Line(8)][Column(0)] = 0; other[Line(9)][Column(0)] = 1; for i in 0..10 { assert_eq!(grid[Line(i)][Column(0)], other[Line(i)][Column(0)]); } } // Scroll down moves lines downwards #[test] fn scroll_down() { info!(""); let mut grid = Grid::new(Line(10), Column(1), &0); for i in 0..10 { grid[Line(i)][Column(0)] = i; } info!("grid: {:?}", grid); grid.scroll_down(&(Line(0)..Line(10)), Line(2)); info!("grid: {:?}", grid); let mut other = Grid::new(Line(10), Column(1), &9); other[Line(0)][Column(0)] = 8; other[Line(1)][Column(0)] = 9; other[Line(2)][Column(0)] = 0; other[Line(3)][Column(0)] = 1; other[Line(4)][Column(0)] = 2; other[Line(5)][Column(0)] = 3; other[Line(6)][Column(0)] = 4; other[Line(7)][Column(0)] = 5; other[Line(8)][Column(0)] = 6; other[Line(9)][Column(0)] = 7; for i in 0..10 { assert_eq!(grid[Line(i)][Column(0)], other[Line(i)][Column(0)]); } } // Test that GridIterator works #[test] fn test_iter() { info!(""); let mut grid = Grid::new(Line(5), Column(5), &0); for i in 0..5 { for j in 0..5 { grid[Line(i)][Column(j)] = i*5 + j; } } info!("grid: {:?}", grid); let mut iter = grid.iter_from(Point { line: Line(0), col: Column(0), }); assert_eq!(None, iter.prev()); assert_eq!(Some(&1), iter.next()); assert_eq!(Column(1), iter.cur.col); assert_eq!(Line(0), iter.cur.line); assert_eq!(Some(&2), iter.next()); assert_eq!(Some(&3), iter.next()); assert_eq!(Some(&4), iter.next()); // test linewrapping assert_eq!(Some(&5), iter.next()); assert_eq!(Column(0), iter.cur.col); assert_eq!(Line(1), iter.cur.line); assert_eq!(Some(&4), iter.prev()); assert_eq!(Column(4), iter.cur.col); assert_eq!(Line(0), iter.cur.line); // test that iter ends at end of grid let mut final_iter = grid.iter_from(Point { line: Line(4), col: Column(4), }); assert_eq!(None, final_iter.next()); assert_eq!(Some(&23), final_iter.prev()); } }