use std::cmp::{max, min};
use std::ops::{Deref, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo};
use serde::{Deserialize, Serialize};
use crate::ansi::{CharsetIndex, StandardCharset};
use crate::index::{Column, IndexRange, Line, Point};
use crate::term::cell::{Cell, Flags};
pub mod resize;
mod row;
mod storage;
#[cfg(test)]
mod tests;
pub use self::row::Row;
use self::storage::Storage;
pub trait BidirectionalIterator: Iterator {
fn prev(&mut self) -> Option<Self::Item>;
}
pub struct Indexed<T> {
pub inner: T,
pub line: Line,
pub column: Column,
}
impl<T> Deref for Indexed<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.inner
}
}
impl<T: PartialEq> ::std::cmp::PartialEq for Grid<T> {
fn eq(&self, other: &Self) -> bool {
self.raw.eq(&other.raw)
&& self.cols.eq(&other.cols)
&& self.lines.eq(&other.lines)
&& self.display_offset.eq(&other.display_offset)
}
}
pub trait GridCell {
fn is_empty(&self) -> bool;
fn flags(&self) -> &Flags;
fn flags_mut(&mut self) -> &mut Flags;
fn fast_eq(&self, other: Self) -> bool;
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub struct Cursor {
pub point: Point,
pub template: Cell,
pub charsets: Charsets,
pub input_needs_wrap: bool,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub struct Charsets([StandardCharset; 4]);
impl Index<CharsetIndex> for Charsets {
type Output = StandardCharset;
fn index(&self, index: CharsetIndex) -> &StandardCharset {
&self.0[index as usize]
}
}
impl IndexMut<CharsetIndex> for Charsets {
fn index_mut(&mut self, index: CharsetIndex) -> &mut StandardCharset {
&mut self.0[index as usize]
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Grid<T> {
#[serde(skip)]
pub cursor: Cursor,
#[serde(skip)]
pub saved_cursor: Cursor,
raw: Storage<T>,
cols: Column,
lines: Line,
display_offset: usize,
max_scroll_limit: usize,
}
#[derive(Debug, Copy, Clone)]
pub enum Scroll {
Delta(isize),
PageUp,
PageDown,
Top,
Bottom,
}
impl<T: GridCell + Default + PartialEq + Copy> Grid<T> {
pub fn new(lines: Line, cols: Column, max_scroll_limit: usize, template: T) -> Grid<T> {
Grid {
raw: Storage::with_capacity(lines, Row::new(cols, template)),
max_scroll_limit,
display_offset: 0,
saved_cursor: Cursor::default(),
cursor: Cursor::default(),
lines,
cols,
}
}
pub fn update_history(&mut self, history_size: usize) {
let current_history_size = self.history_size();
if current_history_size > history_size {
self.raw.shrink_lines(current_history_size - history_size);
}
self.display_offset = min(self.display_offset, history_size);
self.max_scroll_limit = history_size;
}
pub fn scroll_display(&mut self, scroll: Scroll) {
self.display_offset = match scroll {
Scroll::Delta(count) => min(
max((self.display_offset as isize) + count, 0isize) as usize,
self.history_size(),
),
Scroll::PageUp => min(self.display_offset + self.lines.0, self.history_size()),
Scroll::PageDown => self.display_offset.saturating_sub(self.lines.0),
Scroll::Top => self.history_size(),
Scroll::Bottom => 0,
};
}
fn increase_scroll_limit(&mut self, count: usize, template: T) {
let count = min(count, self.max_scroll_limit - self.history_size());
if count != 0 {
self.raw.initialize(count, template, self.cols);
}
}
fn decrease_scroll_limit(&mut self, count: usize) {
let count = min(count, self.history_size());
if count != 0 {
self.raw.shrink_lines(min(count, self.history_size()));
self.display_offset = min(self.display_offset, self.history_size());
}
}
#[inline]
pub fn scroll_down(&mut self, region: &Range<Line>, positions: Line, template: T) {
if region.start == Line(0) && self.max_scroll_limit == 0 {
self.raw.rotate_up(*positions);
let lines = self.lines;
for i in IndexRange(region.end..lines) {
self.raw.swap_lines(i, i + positions);
}
for i in IndexRange(Line(0)..positions) {
self.raw[i].reset(template);
}
} else {
for line in IndexRange((region.start + positions)..region.end).rev() {
self.raw.swap_lines(line, line - positions);
}
for line in IndexRange(region.start..(region.start + positions)) {
self.raw[line].reset(template);
}
}
}
pub fn scroll_up(&mut self, region: &Range<Line>, positions: Line, template: T) {
let num_lines = self.screen_lines().0;
if region.start == Line(0) {
if self.display_offset != 0 {
self.display_offset = min(self.display_offset + *positions, self.max_scroll_limit);
}
self.increase_scroll_limit(*positions, template);
self.raw.rotate(-(*positions as isize));
let fixed_lines = num_lines - *region.end;
for i in 0..fixed_lines {
self.raw.swap(i, i + *positions);
}
for i in 0..*positions {
self.raw[i + fixed_lines].reset(template);
}
} else {
for line in IndexRange(region.start..(region.end - positions)) {
self.raw.swap_lines(line, line + positions);
}
for line in IndexRange((region.end - positions)..region.end) {
self.raw[line].reset(template);
}
}
}
pub fn clear_viewport(&mut self, template: T) {
let end = Point { line: 0, col: self.cols() };
let mut iter = self.iter_from(end);
while let Some(cell) = iter.prev() {
if !cell.is_empty() || iter.cur.line >= *self.lines {
break;
}
}
debug_assert!(iter.cur.line <= *self.lines);
let positions = self.lines - iter.cur.line;
let region = Line(0)..self.screen_lines();
self.display_offset = 0;
self.scroll_up(®ion, positions, template);
for i in positions.0..self.lines.0 {
self.raw[i].reset(template);
}
}
pub fn reset(&mut self, template: T) {
self.clear_history();
for row in 0..self.raw.len() {
self.raw[row].reset(template);
}
self.saved_cursor = Cursor::default();
self.cursor = Cursor::default();
self.display_offset = 0;
}
}
#[allow(clippy::len_without_is_empty)]
impl<T> Grid<T> {
pub fn clamp_buffer_to_visible(&self, point: Point<usize>) -> Point {
if point.line < self.display_offset {
Point::new(self.lines - 1, self.cols - 1)
} else if point.line >= self.display_offset + self.lines.0 {
Point::new(Line(0), Column(0))
} else {
self.visible_to_buffer(point.into()).into()
}
}
#[inline]
pub fn visible_to_buffer(&self, point: Point) -> Point<usize> {
Point { line: self.lines.0 + self.display_offset - point.line.0 - 1, col: point.col }
}
#[inline]
pub fn display_iter(&self) -> DisplayIter<'_, T> {
DisplayIter::new(self)
}
#[inline]
pub fn clear_history(&mut self) {
self.raw.shrink_lines(self.history_size());
}
#[inline]
pub fn initialize_all(&mut self, template: T)
where
T: Copy + GridCell,
{
self.truncate();
self.raw.initialize(self.max_scroll_limit - self.history_size(), template, self.cols);
}
#[inline]
pub fn truncate(&mut self) {
self.raw.truncate();
}
#[inline]
pub fn iter_from(&self, point: Point<usize>) -> GridIterator<'_, T> {
GridIterator { grid: self, cur: point }
}
#[inline]
pub fn display_offset(&self) -> usize {
self.display_offset
}
#[inline]
pub fn cursor_cell(&mut self) -> &mut T {
let point = self.cursor.point;
&mut self[&point]
}
}
pub trait Dimensions {
fn total_lines(&self) -> usize;
fn screen_lines(&self) -> Line;
fn cols(&self) -> Column;
#[inline]
fn history_size(&self) -> usize {
self.total_lines() - self.screen_lines().0
}
}
impl<G> Dimensions for Grid<G> {
#[inline]
fn total_lines(&self) -> usize {
self.raw.len()
}
#[inline]
fn screen_lines(&self) -> Line {
self.lines
}
#[inline]
fn cols(&self) -> Column {
self.cols
}
}
#[cfg(test)]
impl Dimensions for (Line, Column) {
fn total_lines(&self) -> usize {
*self.0
}
fn screen_lines(&self) -> Line {
self.0
}
fn cols(&self) -> Column {
self.1
}
}
pub struct GridIterator<'a, T> {
grid: &'a Grid<T>,
cur: Point<usize>,
}
impl<'a, T> GridIterator<'a, T> {
pub fn point(&self) -> Point<usize> {
self.cur
}
pub fn cell(&self) -> &'a T {
&self.grid[self.cur]
}
}
impl<'a, T> Iterator for GridIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
let last_col = self.grid.cols() - 1;
match self.cur {
Point { line, col } if line == 0 && col == last_col => return None,
Point { col, .. } if (col == last_col) => {
self.cur.line -= 1;
self.cur.col = Column(0);
},
_ => self.cur.col += Column(1),
}
Some(&self.grid[self.cur])
}
}
impl<'a, T> BidirectionalIterator for GridIterator<'a, T> {
fn prev(&mut self) -> Option<Self::Item> {
let last_col = self.grid.cols() - 1;
match self.cur {
Point { line, col: Column(0) } if line == self.grid.total_lines() - 1 => return None,
Point { col: Column(0), .. } => {
self.cur.line += 1;
self.cur.col = last_col;
},
_ => self.cur.col -= Column(1),
}
Some(&self.grid[self.cur])
}
}
impl<T> Index<Line> for Grid<T> {
type Output = Row<T>;
#[inline]
fn index(&self, index: Line) -> &Row<T> {
&self.raw[index]
}
}
impl<T> Index<usize> for Grid<T> {
type Output = Row<T>;
#[inline]
fn index(&self, index: usize) -> &Row<T> {
&self.raw[index]
}
}
impl<T> IndexMut<Line> for Grid<T> {
#[inline]
fn index_mut(&mut self, index: Line) -> &mut Row<T> {
&mut self.raw[index]
}
}
impl<T> IndexMut<usize> for Grid<T> {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Row<T> {
&mut self.raw[index]
}
}
impl<'point, T> Index<&'point Point> for Grid<T> {
type Output = T;
#[inline]
fn index<'a>(&'a self, point: &Point) -> &'a T {
&self[point.line][point.col]
}
}
impl<'point, T> IndexMut<&'point Point> for Grid<T> {
#[inline]
fn index_mut<'a, 'b>(&'a mut self, point: &'b Point) -> &'a mut T {
&mut self[point.line][point.col]
}
}
impl<T> Index<Point<usize>> for Grid<T> {
type Output = T;
#[inline]
fn index(&self, point: Point<usize>) -> &T {
&self[point.line][point.col]
}
}
impl<T> IndexMut<Point<usize>> for Grid<T> {
#[inline]
fn index_mut(&mut self, point: Point<usize>) -> &mut T {
&mut self[point.line][point.col]
}
}
pub struct Region<'a, T> {
start: Line,
end: Line,
raw: &'a Storage<T>,
}
pub struct RegionMut<'a, T> {
start: Line,
end: Line,
raw: &'a mut Storage<T>,
}
impl<'a, T> RegionMut<'a, T> {
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> {
fn region(&self, _: I) -> Region<'_, T>;
fn region_mut(&mut self, _: I) -> RegionMut<'_, T>;
}
impl<T> IndexRegion<Range<Line>, T> for Grid<T> {
fn region(&self, index: Range<Line>) -> Region<'_, T> {
assert!(index.start < self.screen_lines());
assert!(index.end <= self.screen_lines());
assert!(index.start <= index.end);
Region { start: index.start, end: index.end, raw: &self.raw }
}
fn region_mut(&mut self, index: Range<Line>) -> RegionMut<'_, T> {
assert!(index.start < self.screen_lines());
assert!(index.end <= self.screen_lines());
assert!(index.start <= index.end);
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.screen_lines());
Region { start: Line(0), end: index.end, raw: &self.raw }
}
fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<'_, T> {
assert!(index.end <= self.screen_lines());
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.screen_lines());
Region { start: index.start, end: self.screen_lines(), raw: &self.raw }
}
fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<'_, T> {
assert!(index.start < self.screen_lines());
RegionMut { start: index.start, end: self.screen_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.screen_lines(), raw: &self.raw }
}
fn region_mut(&mut self, _: RangeFull) -> RegionMut<'_, T> {
RegionMut { start: Line(0), end: self.screen_lines(), raw: &mut self.raw }
}
}
pub struct RegionIter<'a, T> {
end: Line,
cur: Line,
raw: &'a Storage<T>,
}
pub struct RegionIterMut<'a, T> {
end: Line,
cur: Line,
raw: &'a mut Storage<T>,
}
impl<'a, T> IntoIterator for Region<'a, 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 }
}
}
impl<'a, T> IntoIterator for RegionMut<'a, 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 }
}
}
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;
self.cur += 1;
Some(&self.raw[index])
} else {
None
}
}
}
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 _)) }
} else {
None
}
}
}
pub struct DisplayIter<'a, T> {
grid: &'a Grid<T>,
offset: usize,
limit: usize,
col: Column,
line: Line,
}
impl<'a, T: 'a> DisplayIter<'a, T> {
pub fn new(grid: &'a Grid<T>) -> DisplayIter<'a, T> {
let offset = grid.display_offset + *grid.screen_lines() - 1;
let limit = grid.display_offset;
let col = Column(0);
let line = Line(0);
DisplayIter { grid, offset, col, limit, line }
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn column(&self) -> Column {
self.col
}
pub fn line(&self) -> Line {
self.line
}
}
impl<'a, T: Copy + 'a> Iterator for DisplayIter<'a, T> {
type Item = Indexed<T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.offset == self.limit && self.grid.cols() == self.col {
return None;
}
let item = Some(Indexed {
inner: self.grid.raw[self.offset][self.col],
line: self.line,
column: self.col,
});
self.col += 1;
if self.col == self.grid.cols() && self.offset != self.limit {
self.offset -= 1;
self.col = Column(0);
self.line = Line(*self.grid.lines - 1 - (self.offset - self.limit));
}
item
}
}