2016-06-29 23:56:12 -04:00
|
|
|
// 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.
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
//! 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.
|
|
|
|
|
2016-08-22 11:37:50 -04:00
|
|
|
use std::borrow::ToOwned;
|
2016-06-29 13:21:02 -04:00
|
|
|
use std::cmp::Ordering;
|
2016-07-02 11:25:21 -04:00
|
|
|
use std::iter::IntoIterator;
|
2016-08-22 11:37:50 -04:00
|
|
|
use std::ops::{Deref, DerefMut, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut};
|
|
|
|
use std::slice::{self, Iter, IterMut};
|
2016-06-08 13:39:49 -04:00
|
|
|
|
2017-01-06 19:26:31 -05:00
|
|
|
use index::{self, Point, IndexRange, RangeInclusive};
|
2016-07-03 20:00:00 -04:00
|
|
|
|
2016-12-22 13:43:06 -05:00
|
|
|
/// Convert a type to a linear index range.
|
|
|
|
pub trait ToRange {
|
|
|
|
fn to_range(&self, columns: index::Column) -> RangeInclusive<index::Linear>;
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
/// Represents the terminal display contents
|
2016-11-19 19:16:20 -05:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
2016-07-03 20:00:00 -04:00
|
|
|
pub struct Grid<T> {
|
|
|
|
/// Lines in the grid. Each row holds a list of cells corresponding to the
|
|
|
|
/// columns in that row.
|
|
|
|
raw: Vec<Row<T>>,
|
|
|
|
|
|
|
|
/// Number of columns
|
|
|
|
cols: index::Column,
|
|
|
|
|
|
|
|
/// Number of lines.
|
|
|
|
///
|
|
|
|
/// Invariant: lines is equivalent to raw.len()
|
|
|
|
lines: index::Line,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> Grid<T> {
|
|
|
|
pub fn new(lines: index::Line, cols: index::Column, template: &T) -> Grid<T> {
|
|
|
|
let mut raw = Vec::with_capacity(*lines);
|
2017-01-06 18:25:04 -05:00
|
|
|
for _ in IndexRange(index::Line(0)..lines) {
|
2016-07-03 20:00:00 -04:00
|
|
|
raw.push(Row::new(cols, template));
|
|
|
|
}
|
|
|
|
|
|
|
|
Grid {
|
|
|
|
raw: raw,
|
|
|
|
cols: cols,
|
|
|
|
lines: lines,
|
|
|
|
}
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
pub fn resize(&mut self, lines: index::Line, cols: index::Column, template: &T) {
|
2016-06-29 13:21:02 -04:00
|
|
|
// Check that there's actually work to do and return early if not
|
2016-07-03 20:00:00 -04:00
|
|
|
if lines == self.lines && cols == self.cols {
|
2016-06-29 13:21:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
match self.lines.cmp(&lines) {
|
|
|
|
Ordering::Less => self.grow_lines(lines, template),
|
|
|
|
Ordering::Greater => self.shrink_lines(lines),
|
2016-06-29 13:21:02 -04:00
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.cols.cmp(&cols) {
|
2016-07-03 20:00:00 -04:00
|
|
|
Ordering::Less => self.grow_cols(cols, template),
|
2016-06-29 13:21:02 -04:00
|
|
|
Ordering::Greater => self.shrink_cols(cols),
|
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn grow_lines(&mut self, lines: index::Line, template: &T) {
|
2017-01-06 18:25:04 -05:00
|
|
|
for _ in IndexRange(self.num_lines()..lines) {
|
2016-07-03 20:00:00 -04:00
|
|
|
self.raw.push(Row::new(self.cols, template));
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
self.lines = lines;
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn grow_cols(&mut self, cols: index::Column, template: &T) {
|
|
|
|
for row in self.lines_mut() {
|
|
|
|
row.grow(cols, template);
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
self.cols = cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
pub fn lines(&self) -> Iter<Row<T>> {
|
|
|
|
self.raw.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn lines_mut(&mut self) -> IterMut<Row<T>> {
|
|
|
|
self.raw.iter_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn num_lines(&self) -> index::Line {
|
2016-07-29 21:04:26 -04:00
|
|
|
self.lines
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn num_cols(&self) -> index::Column {
|
2016-07-29 21:04:26 -04:00
|
|
|
self.cols
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-08-22 11:37:50 -04:00
|
|
|
pub fn scroll_down(&mut self, region: Range<index::Line>, positions: index::Line) {
|
2017-01-06 18:25:04 -05:00
|
|
|
for line in IndexRange(region).rev() {
|
2016-08-22 11:37:50 -04:00
|
|
|
let src = line;
|
|
|
|
let dst = line - positions;
|
|
|
|
self.swap_lines(src, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn scroll_up(&mut self, region: Range<index::Line>, positions: index::Line) {
|
2017-01-06 18:25:04 -05:00
|
|
|
for line in IndexRange(region) {
|
2016-08-22 11:37:50 -04:00
|
|
|
let src = line;
|
|
|
|
let dst = line + positions;
|
|
|
|
self.swap_lines(src, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:16:58 -04:00
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
pub fn contains(&self, point: &Point) -> bool {
|
|
|
|
self.lines > point.line && self.cols > point.col
|
2016-09-16 20:12:53 -04:00
|
|
|
}
|
|
|
|
|
2016-08-22 11:37:50 -04:00
|
|
|
/// 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) {
|
2017-01-06 19:32:29 -05:00
|
|
|
use util::unlikely;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
unsafe {
|
2016-09-18 21:17:33 -04:00
|
|
|
// check that src/dst are in bounds. Since index::Line newtypes usize,
|
|
|
|
// we can assume values are positive.
|
|
|
|
if unlikely(src >= self.lines) {
|
|
|
|
panic!("swap_lines src out of bounds; len={}, src={}", self.raw.len(), src);
|
|
|
|
}
|
|
|
|
|
|
|
|
if unlikely(dst >= self.lines) {
|
|
|
|
panic!("swap_lines dst out of bounds; len={}, dst={}", self.raw.len(), dst);
|
|
|
|
}
|
|
|
|
|
2016-08-22 11:37:50 -04:00
|
|
|
let src: *mut _ = self.raw.get_unchecked_mut(src.0);
|
|
|
|
let dst: *mut _ = self.raw.get_unchecked_mut(dst.0);
|
|
|
|
|
|
|
|
::std::ptr::swap(src, dst);
|
|
|
|
}
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn shrink_lines(&mut self, lines: index::Line) {
|
|
|
|
while index::Line(self.raw.len()) != lines {
|
|
|
|
self.raw.pop();
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
self.lines = lines;
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn shrink_cols(&mut self, cols: index::Column) {
|
|
|
|
for row in self.lines_mut() {
|
2016-06-29 13:21:02 -04:00
|
|
|
row.shrink(cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cols = cols;
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Index<index::Line> for Grid<T> {
|
|
|
|
type Output = Row<T>;
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index(&self, index: index::Line) -> &Row<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
&self.raw[index.0]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> IndexMut<index::Line> for Grid<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index_mut(&mut self, index: index::Line) -> &mut Row<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
&mut self.raw[index.0]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:09:29 -05:00
|
|
|
impl<'point, T> Index<&'point Point> for Grid<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
type Output = T;
|
2016-05-30 23:44:37 -04:00
|
|
|
|
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
fn index<'a>(&'a self, point: &Point) -> &'a T {
|
|
|
|
&self.raw[point.line.0][point.col]
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:09:29 -05:00
|
|
|
impl<'point, T> IndexMut<&'point Point> for Grid<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
fn index_mut<'a, 'b>(&'a mut self, point: &'b Point) -> &'a mut T {
|
|
|
|
&mut self.raw[point.line.0][point.col]
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:19:39 -04:00
|
|
|
/// A row in the grid
|
2016-11-19 19:16:20 -05:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
2016-07-03 20:00:00 -04:00
|
|
|
pub struct Row<T>(Vec<T>);
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T: Clone> Row<T> {
|
|
|
|
pub fn new(columns: index::Column, template: &T) -> Row<T> {
|
|
|
|
Row(vec![template.to_owned(); *columns])
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
pub fn grow(&mut self, cols: index::Column, template: &T) {
|
|
|
|
while self.len() != *cols {
|
|
|
|
self.push(template.to_owned());
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Row<T> {
|
|
|
|
pub fn shrink(&mut self, cols: index::Column) {
|
|
|
|
while self.len() != *cols {
|
2016-06-29 13:21:02 -04:00
|
|
|
self.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn cells(&self) -> Iter<T> {
|
2016-06-04 22:40:30 -04:00
|
|
|
self.0.iter()
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn cells_mut(&mut self) -> IterMut<T> {
|
2016-06-04 22:40:30 -04:00
|
|
|
self.0.iter_mut()
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<'a, T> IntoIterator for &'a Row<T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = slice::Iter<'a, T>;
|
2016-07-02 11:25:21 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
#[inline]
|
|
|
|
fn into_iter(self) -> slice::Iter<'a, T> {
|
2016-07-02 11:25:21 -04:00
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<'a, T> IntoIterator for &'a mut Row<T> {
|
|
|
|
type Item = &'a mut T;
|
|
|
|
type IntoIter = slice::IterMut<'a, T>;
|
2016-07-02 11:25:21 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
#[inline]
|
|
|
|
fn into_iter(mut self) -> slice::IterMut<'a, T> {
|
2016-07-02 11:25:21 -04:00
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Deref for Row<T> {
|
|
|
|
type Target = Vec<T>;
|
|
|
|
|
|
|
|
#[inline]
|
2016-05-30 23:44:37 -04:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> DerefMut for Row<T> {
|
|
|
|
#[inline]
|
2016-05-30 23:44:37 -04:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Index<index::Column> for Row<T> {
|
|
|
|
type Output = T;
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index(&self, index: index::Column) -> &T {
|
2016-07-03 20:00:00 -04:00
|
|
|
&self.0[index.0]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> IndexMut<index::Column> for Row<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index_mut(&mut self, index: index::Column) -> &mut T {
|
2016-07-03 20:00:00 -04:00
|
|
|
&mut self.0[index.0]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-08 13:39:49 -04:00
|
|
|
|
2016-07-02 00:13:09 -04:00
|
|
|
macro_rules! row_index_range {
|
|
|
|
($range:ty) => {
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Index<$range> for Row<T> {
|
|
|
|
type Output = [T];
|
|
|
|
|
2016-07-02 00:13:09 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index(&self, index: $range) -> &[T] {
|
2016-07-02 00:13:09 -04:00
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 13:39:49 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> IndexMut<$range> for Row<T> {
|
2016-07-02 00:13:09 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index_mut(&mut self, index: $range) -> &mut [T] {
|
2016-07-02 00:13:09 -04:00
|
|
|
&mut self.0[index]
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 13:39:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-02 00:13:09 -04:00
|
|
|
row_index_range!(Range<usize>);
|
|
|
|
row_index_range!(RangeTo<usize>);
|
|
|
|
row_index_range!(RangeFrom<usize>);
|
|
|
|
row_index_range!(RangeFull);
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2016-12-17 01:48:04 -05:00
|
|
|
// -----------------------------------------------------------------------------
|
2016-07-03 20:00:00 -04:00
|
|
|
// Row ranges for Grid
|
2016-12-17 01:48:04 -05:00
|
|
|
// -----------------------------------------------------------------------------
|
2016-07-03 20:00:00 -04:00
|
|
|
|
|
|
|
impl<T> Index<Range<index::Line>> for Grid<T> {
|
|
|
|
type Output = [Row<T>];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: Range<index::Line>) -> &[Row<T>] {
|
|
|
|
&self.raw[(index.start.0)..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<Range<index::Line>> for Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: Range<index::Line>) -> &mut [Row<T>] {
|
|
|
|
&mut self.raw[(index.start.0)..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Index<RangeTo<index::Line>> for Grid<T> {
|
|
|
|
type Output = [Row<T>];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: RangeTo<index::Line>) -> &[Row<T>] {
|
|
|
|
&self.raw[..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<RangeTo<index::Line>> for Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: RangeTo<index::Line>) -> &mut [Row<T>] {
|
|
|
|
&mut self.raw[..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Index<RangeFrom<index::Line>> for Grid<T> {
|
|
|
|
type Output = [Row<T>];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: RangeFrom<index::Line>) -> &[Row<T>] {
|
|
|
|
&self.raw[(index.start.0)..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<RangeFrom<index::Line>> for Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: RangeFrom<index::Line>) -> &mut [Row<T>] {
|
|
|
|
&mut self.raw[(index.start.0)..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 01:48:04 -05:00
|
|
|
// -----------------------------------------------------------------------------
|
2016-07-03 20:00:00 -04:00
|
|
|
// Column ranges for Row
|
2016-12-17 01:48:04 -05:00
|
|
|
// -----------------------------------------------------------------------------
|
2016-07-03 20:00:00 -04:00
|
|
|
|
|
|
|
impl<T> Index<Range<index::Column>> for Row<T> {
|
|
|
|
type Output = [T];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: Range<index::Column>) -> &[T] {
|
|
|
|
&self.0[(index.start.0)..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<Range<index::Column>> for Row<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: Range<index::Column>) -> &mut [T] {
|
|
|
|
&mut self.0[(index.start.0)..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Index<RangeTo<index::Column>> for Row<T> {
|
|
|
|
type Output = [T];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: RangeTo<index::Column>) -> &[T] {
|
|
|
|
&self.0[..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<RangeTo<index::Column>> for Row<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: RangeTo<index::Column>) -> &mut [T] {
|
|
|
|
&mut self.0[..(index.end.0)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Index<RangeFrom<index::Column>> for Row<T> {
|
|
|
|
type Output = [T];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: RangeFrom<index::Column>) -> &[T] {
|
|
|
|
&self.0[(index.start.0)..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<RangeFrom<index::Column>> for Row<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: RangeFrom<index::Column>) -> &mut [T] {
|
|
|
|
&mut self.0[(index.start.0)..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ClearRegion<R, T> {
|
|
|
|
fn clear_region<F: Fn(&mut T)>(&mut self, region: R, func: F);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! clear_region_impl {
|
|
|
|
($range:ty) => {
|
2016-07-03 20:00:00 -04:00
|
|
|
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].iter_mut() {
|
2016-07-02 11:25:21 -04:00
|
|
|
for cell in row {
|
2016-07-03 20:00:00 -04:00
|
|
|
func(cell);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
clear_region_impl!(Range<index::Line>);
|
|
|
|
clear_region_impl!(RangeTo<index::Line>);
|
|
|
|
clear_region_impl!(RangeFrom<index::Line>);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Grid;
|
|
|
|
use index::{Line, Column};
|
|
|
|
#[test]
|
|
|
|
fn grid_swap_lines_ok() {
|
|
|
|
let mut grid = Grid::new(Line(10), Column(1), &0);
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("");
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("grid: {:?}", grid);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
assert_eq!(grid[Line(4)][Column(0)], 1);
|
|
|
|
assert_eq!(grid[Line(5)][Column(0)], 2);
|
|
|
|
|
|
|
|
grid.swap_lines(Line(4), Line(5));
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("grid: {:?}", grid);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
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() {
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("");
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
let mut grid = Grid::new(Line(10), Column(1), &0);
|
|
|
|
for i in 0..10 {
|
|
|
|
grid[Line(i)][Column(0)] = i;
|
|
|
|
}
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("grid: {:?}", grid);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
grid.scroll_up(Line(0)..Line(8), Line(2));
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("grid: {:?}", grid);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
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() {
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("");
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
let mut grid = Grid::new(Line(10), Column(1), &0);
|
|
|
|
for i in 0..10 {
|
|
|
|
grid[Line(i)][Column(0)] = i;
|
|
|
|
}
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("grid: {:?}", grid);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
grid.scroll_down(Line(2)..Line(10), Line(2));
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("grid: {:?}", grid);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
|
|
|
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)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|