// 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::ops::{Deref, DerefMut, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut}; use std::cmp::Ordering; use std::slice::{self, Iter, IterMut}; use std::iter::IntoIterator; use std::borrow::ToOwned; use util::Rotate; use index::{self, Cursor}; /// Represents the terminal display contents #[derive(Clone)] pub struct Grid { /// Lines in the grid. Each row holds a list of cells corresponding to the /// columns in that row. raw: Vec>, /// Number of columns cols: index::Column, /// Number of lines. /// /// Invariant: lines is equivalent to raw.len() lines: index::Line, } impl Grid { pub fn new(lines: index::Line, cols: index::Column, template: &T) -> Grid { let mut raw = Vec::with_capacity(*lines); for _ in index::Line(0)..lines { raw.push(Row::new(cols, template)); } Grid { raw: raw, cols: cols, lines: 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 self.num_lines()..lines { self.raw.push(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; } } impl Grid { #[inline] pub fn lines(&self) -> Iter> { self.raw.iter() } #[inline] pub fn lines_mut(&mut self) -> IterMut> { self.raw.iter_mut() } #[inline] pub fn num_lines(&self) -> index::Line { index::Line(self.raw.len()) } #[inline] pub fn num_cols(&self) -> index::Column { index::Column(self.raw[0].len()) } #[inline] pub fn scroll(&mut self, region: Range, positions: isize) { self[region].rotate(positions) } #[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(); } self.lines = lines; } fn shrink_cols(&mut self, cols: index::Column) { for row in self.lines_mut() { row.shrink(cols); } self.cols = cols; } } impl Index for Grid { type Output = Row; #[inline] fn index<'a>(&'a self, index: index::Line) -> &'a Row { &self.raw[index.0] } } impl IndexMut for Grid { #[inline] fn index_mut<'a>(&'a mut self, index: index::Line) -> &'a mut Row { &mut self.raw[index.0] } } impl<'cursor, T> Index<&'cursor Cursor> for Grid { type Output = T; #[inline] fn index<'a, 'b>(&'a self, cursor: &'b Cursor) -> &'a T { &self.raw[cursor.line.0][cursor.col] } } impl<'cursor, T> IndexMut<&'cursor Cursor> for Grid { #[inline] fn index_mut<'a, 'b>(&'a mut self, cursor: &'b Cursor) -> &'a mut T { &mut self.raw[cursor.line.0][cursor.col] } } /// A row in the grid #[derive(Clone)] 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) -> Iter { self.0.iter() } #[inline] pub fn cells_mut(&mut self) -> IterMut { self.0.iter_mut() } } 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(mut 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<'a>(&'a self, index: index::Column) -> &'a T { &self.0[index.0] } } impl IndexMut for Row { #[inline] fn index_mut<'a>(&'a mut self, index: index::Column) -> &'a 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<'a>(&'a self, index: $range) -> &'a [T] { &self.0[index] } } impl IndexMut<$range> for Row { #[inline] fn index_mut<'a>(&'a mut self, index: $range) -> &'a mut [T] { &mut self.0[index] } } } } row_index_range!(Range); row_index_range!(RangeTo); row_index_range!(RangeFrom); row_index_range!(RangeFull); // ------------------------------------------------------------------------------------------------- // Row ranges for Grid // ------------------------------------------------------------------------------------------------- impl Index> for Grid { type Output = [Row]; #[inline] fn index(&self, index: Range) -> &[Row] { &self.raw[(index.start.0)..(index.end.0)] } } impl IndexMut> for Grid { #[inline] fn index_mut(&mut self, index: Range) -> &mut [Row] { &mut self.raw[(index.start.0)..(index.end.0)] } } impl Index> for Grid { type Output = [Row]; #[inline] fn index(&self, index: RangeTo) -> &[Row] { &self.raw[..(index.end.0)] } } impl IndexMut> for Grid { #[inline] fn index_mut(&mut self, index: RangeTo) -> &mut [Row] { &mut self.raw[..(index.end.0)] } } impl Index> for Grid { type Output = [Row]; #[inline] fn index(&self, index: RangeFrom) -> &[Row] { &self.raw[(index.start.0)..] } } impl IndexMut> for Grid { #[inline] fn index_mut(&mut self, index: RangeFrom) -> &mut [Row] { &mut self.raw[(index.start.0)..] } } // ------------------------------------------------------------------------------------------------- // 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].iter_mut() { for cell in row { func(cell); } } } } } } clear_region_impl!(Range); clear_region_impl!(RangeTo); clear_region_impl!(RangeFrom);