2020-05-05 22:50:23 +00:00
|
|
|
//! A specialized 2D grid implementation optimized for use in a terminal.
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
use std::cmp::{max, min};
|
2021-01-24 21:45:36 +00:00
|
|
|
use std::iter::{Map, TakeWhile};
|
|
|
|
use std::ops::{Bound, Deref, Index, IndexMut, Range, RangeBounds, RangeInclusive};
|
2016-06-08 17:39:49 +00:00
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
use crate::ansi::{CharsetIndex, StandardCharset};
|
2020-01-09 23:06:41 +00:00
|
|
|
use crate::index::{Column, IndexRange, Line, Point};
|
2020-11-05 04:45:14 +00:00
|
|
|
use crate::term::cell::{Flags, ResetDiscriminant};
|
2016-07-04 00:00:00 +00:00
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
pub mod resize;
|
2017-10-13 03:01:28 +00:00
|
|
|
mod row;
|
2020-05-30 20:45:44 +00:00
|
|
|
mod storage;
|
2017-10-13 03:01:28 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
pub use self::row::Row;
|
2018-01-15 05:51:56 +00:00
|
|
|
use self::storage::Storage;
|
|
|
|
|
2020-11-05 04:45:14 +00:00
|
|
|
pub trait GridCell: Sized {
|
|
|
|
/// Check if the cell contains any content.
|
2019-03-13 18:55:18 +00:00
|
|
|
fn is_empty(&self) -> bool;
|
2020-11-05 04:45:14 +00:00
|
|
|
|
|
|
|
/// Perform an opinionated cell reset based on a template cell.
|
|
|
|
fn reset(&mut self, template: &Self);
|
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
fn flags(&self) -> &Flags;
|
|
|
|
fn flags_mut(&mut self) -> &mut Flags;
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 04:45:14 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Cursor<T> {
|
2020-05-30 20:45:44 +00:00
|
|
|
/// The location of this cursor.
|
|
|
|
pub point: Point,
|
|
|
|
|
|
|
|
/// Template cell when using this cursor.
|
2020-11-05 04:45:14 +00:00
|
|
|
pub template: T,
|
2020-05-30 20:45:44 +00:00
|
|
|
|
|
|
|
/// Currently configured graphic character sets.
|
|
|
|
pub charsets: Charsets,
|
2020-07-06 05:08:36 +00:00
|
|
|
|
|
|
|
/// Tracks if the next call to input will need to first handle wrapping.
|
|
|
|
///
|
|
|
|
/// This is true after the last column is set with the input function. Any function that
|
|
|
|
/// implicitly sets the line or column needs to set this to false to avoid wrapping twice.
|
|
|
|
///
|
|
|
|
/// Tracking `input_needs_wrap` makes it possible to not store a cursor position that exceeds
|
|
|
|
/// the number of columns, which would lead to index out of bounds when interacting with arrays
|
|
|
|
/// without sanitization.
|
|
|
|
pub input_needs_wrap: bool,
|
2020-05-30 20:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum Scroll {
|
|
|
|
Delta(isize),
|
|
|
|
PageUp,
|
|
|
|
PageDown,
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
/// Grid based terminal content storage.
|
2019-11-18 21:15:25 +00:00
|
|
|
///
|
|
|
|
/// ```notrust
|
|
|
|
/// ┌─────────────────────────┐ <-- max_scroll_limit + lines
|
|
|
|
/// │ │
|
|
|
|
/// │ UNINITIALIZED │
|
|
|
|
/// │ │
|
2020-01-26 13:49:58 +00:00
|
|
|
/// ├─────────────────────────┤ <-- self.raw.inner.len()
|
2019-11-18 21:15:25 +00:00
|
|
|
/// │ │
|
|
|
|
/// │ RESIZE BUFFER │
|
|
|
|
/// │ │
|
2020-01-26 13:49:58 +00:00
|
|
|
/// ├─────────────────────────┤ <-- self.history_size() + lines
|
2019-11-18 21:15:25 +00:00
|
|
|
/// │ │
|
|
|
|
/// │ SCROLLUP REGION │
|
|
|
|
/// │ │
|
|
|
|
/// ├─────────────────────────┤v lines
|
|
|
|
/// │ │|
|
|
|
|
/// │ VISIBLE REGION │|
|
|
|
|
/// │ │|
|
|
|
|
/// ├─────────────────────────┤^ <-- display_offset
|
|
|
|
/// │ │
|
|
|
|
/// │ SCROLLDOWN REGION │
|
|
|
|
/// │ │
|
|
|
|
/// └─────────────────────────┘ <-- zero
|
|
|
|
/// ^
|
|
|
|
/// cols
|
|
|
|
/// ```
|
2020-12-31 05:52:45 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2016-07-04 00:00:00 +00:00
|
|
|
pub struct Grid<T> {
|
2020-05-30 20:45:44 +00:00
|
|
|
/// Current cursor for writing data.
|
|
|
|
#[serde(skip)]
|
2020-11-05 04:45:14 +00:00
|
|
|
pub cursor: Cursor<T>,
|
2020-05-30 20:45:44 +00:00
|
|
|
|
|
|
|
/// Last saved cursor.
|
|
|
|
#[serde(skip)]
|
2020-11-05 04:45:14 +00:00
|
|
|
pub saved_cursor: Cursor<T>,
|
2020-05-30 20:45:44 +00:00
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
/// Lines in the grid. Each row holds a list of cells corresponding to the
|
|
|
|
/// columns in that row.
|
2018-05-30 04:33:13 +00:00
|
|
|
raw: Storage<T>,
|
2016-07-04 00:00:00 +00:00
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
/// Number of columns.
|
2020-01-09 23:06:41 +00:00
|
|
|
cols: Column,
|
2016-07-04 00:00:00 +00:00
|
|
|
|
2019-11-18 21:15:25 +00:00
|
|
|
/// Number of visible lines.
|
2020-01-09 23:06:41 +00:00
|
|
|
lines: Line,
|
2018-01-14 18:17:08 +00:00
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
/// Offset of displayed area.
|
2018-02-12 05:25:33 +00:00
|
|
|
///
|
|
|
|
/// If the displayed region isn't at the bottom of the screen, it stays
|
|
|
|
/// stationary while more text is emitted. The scrolling implementation
|
|
|
|
/// updates this offset accordingly.
|
|
|
|
display_offset: usize,
|
2018-02-16 03:06:39 +00:00
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
/// Maximum number of lines in history.
|
2018-09-24 18:40:09 +00:00
|
|
|
max_scroll_limit: usize,
|
2016-07-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 04:45:14 +00:00
|
|
|
impl<T: GridCell + Default + PartialEq + Clone> Grid<T> {
|
|
|
|
pub fn new(lines: Line, cols: Column, max_scroll_limit: usize) -> Grid<T> {
|
2020-05-30 20:45:44 +00:00
|
|
|
Grid {
|
2020-11-05 04:45:14 +00:00
|
|
|
raw: Storage::with_capacity(lines, cols),
|
2020-05-30 20:45:44 +00:00
|
|
|
max_scroll_limit,
|
|
|
|
display_offset: 0,
|
|
|
|
saved_cursor: Cursor::default(),
|
|
|
|
cursor: Cursor::default(),
|
|
|
|
lines,
|
|
|
|
cols,
|
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Update the size of the scrollback history.
|
2020-01-26 13:49:58 +00:00
|
|
|
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);
|
2019-03-02 18:32:48 +00:00
|
|
|
self.max_scroll_limit = history_size;
|
2018-05-30 08:20:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 12:01:06 +00:00
|
|
|
pub fn scroll_display(&mut self, scroll: Scroll) {
|
2020-07-09 21:45:22 +00:00
|
|
|
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,
|
|
|
|
};
|
2018-03-10 19:24:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 04:45:14 +00:00
|
|
|
fn increase_scroll_limit(&mut self, count: usize) {
|
2020-01-26 13:49:58 +00:00
|
|
|
let count = min(count, self.max_scroll_limit - self.history_size());
|
|
|
|
if count != 0 {
|
2020-11-05 04:45:14 +00:00
|
|
|
self.raw.initialize(count, self.cols);
|
2018-09-24 18:40:09 +00:00
|
|
|
}
|
2018-02-16 03:06:39 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 02:35:54 +00:00
|
|
|
fn decrease_scroll_limit(&mut self, count: usize) {
|
2020-01-26 13:49:58 +00:00
|
|
|
let count = min(count, self.history_size());
|
|
|
|
if count != 0 {
|
|
|
|
self.raw.shrink_lines(min(count, self.history_size()));
|
2020-05-30 20:45:44 +00:00
|
|
|
self.display_offset = min(self.display_offset, self.history_size());
|
2020-01-26 13:49:58 +00:00
|
|
|
}
|
2018-02-17 02:35:54 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
#[inline]
|
2020-11-05 04:45:14 +00:00
|
|
|
pub fn scroll_down<D>(&mut self, region: &Range<Line>, positions: Line)
|
|
|
|
where
|
|
|
|
T: ResetDiscriminant<D>,
|
|
|
|
D: PartialEq,
|
|
|
|
{
|
2020-12-10 02:10:24 +00:00
|
|
|
let screen_lines = self.screen_lines().0;
|
|
|
|
|
|
|
|
// When rotating the entire region, just reset everything.
|
|
|
|
if positions >= region.end - region.start {
|
|
|
|
for i in region.start.0..region.end.0 {
|
|
|
|
let index = screen_lines - i - 1;
|
|
|
|
self.raw[index].reset(&self.cursor.template);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Which implementation we can use depends on the existence of a scrollback history.
|
2018-01-15 05:51:56 +00:00
|
|
|
//
|
2020-12-10 02:10:24 +00:00
|
|
|
// Since a scrollback history prevents us from rotating the entire buffer downwards, we
|
|
|
|
// instead have to rely on a slower, swap-based implementation.
|
|
|
|
if self.max_scroll_limit == 0 {
|
|
|
|
// Swap the lines fixed at the bottom to their target positions after rotation.
|
|
|
|
//
|
|
|
|
// Since we've made sure that the rotation will never rotate away the entire region, we
|
|
|
|
// know that the position of the fixed lines before the rotation must already be
|
|
|
|
// visible.
|
|
|
|
//
|
|
|
|
// We need to start from the top, to make sure the fixed lines aren't swapped with each
|
|
|
|
// other.
|
|
|
|
let fixed_lines = screen_lines - region.end.0;
|
|
|
|
for i in (0..fixed_lines).rev() {
|
|
|
|
self.raw.swap(i, i + positions.0);
|
2018-01-15 05:51:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// Rotate the entire line buffer downward.
|
|
|
|
self.raw.rotate_down(*positions);
|
|
|
|
|
|
|
|
// Ensure all new lines are fully cleared.
|
|
|
|
for i in 0..positions.0 {
|
|
|
|
let index = screen_lines - i - 1;
|
|
|
|
self.raw[index].reset(&self.cursor.template);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the fixed lines at the top back into position.
|
|
|
|
for i in 0..region.start.0 {
|
|
|
|
let index = screen_lines - i - 1;
|
|
|
|
self.raw.swap(index, index - positions.0);
|
2017-10-13 02:07:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Subregion rotation.
|
2017-10-13 02:07:49 +00:00
|
|
|
for line in IndexRange((region.start + positions)..region.end).rev() {
|
2018-02-16 02:35:49 +00:00
|
|
|
self.raw.swap_lines(line, line - positions);
|
2017-10-13 02:07:49 +00:00
|
|
|
}
|
2018-01-14 18:17:08 +00:00
|
|
|
|
2019-03-30 16:48:36 +00:00
|
|
|
for line in IndexRange(region.start..(region.start + positions)) {
|
2020-11-05 04:45:14 +00:00
|
|
|
self.raw[line].reset(&self.cursor.template);
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:27:32 +00:00
|
|
|
/// Move lines at the bottom toward the top.
|
2018-02-11 18:07:33 +00:00
|
|
|
///
|
|
|
|
/// This is the performance-sensitive part of scrolling.
|
2020-11-05 04:45:14 +00:00
|
|
|
pub fn scroll_up<D>(&mut self, region: &Range<Line>, positions: Line)
|
|
|
|
where
|
|
|
|
T: ResetDiscriminant<D>,
|
|
|
|
D: PartialEq,
|
|
|
|
{
|
2020-12-10 02:10:24 +00:00
|
|
|
let screen_lines = self.screen_lines().0;
|
2020-01-24 22:57:22 +00:00
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// When rotating the entire region with fixed lines at the top, just reset everything.
|
|
|
|
if positions >= region.end - region.start && region.start != Line(0) {
|
|
|
|
for i in region.start.0..region.end.0 {
|
|
|
|
let index = screen_lines - i - 1;
|
|
|
|
self.raw[index].reset(&self.cursor.template);
|
2018-02-12 05:25:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-16 03:06:39 +00:00
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// Update display offset when not pinned to active area.
|
|
|
|
if self.display_offset != 0 {
|
|
|
|
self.display_offset = min(self.display_offset + *positions, self.max_scroll_limit);
|
|
|
|
}
|
2018-01-15 05:51:56 +00:00
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// Create scrollback for the new lines.
|
|
|
|
self.increase_scroll_limit(*positions);
|
2018-05-19 21:47:16 +00:00
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// Swap the lines fixed at the top to their target positions after rotation.
|
|
|
|
//
|
|
|
|
// Since we've made sure that the rotation will never rotate away the entire region, we
|
|
|
|
// know that the position of the fixed lines before the rotation must already be
|
|
|
|
// visible.
|
|
|
|
//
|
|
|
|
// We need to start from the bottom, to make sure the fixed lines aren't swapped with each
|
|
|
|
// other.
|
|
|
|
for i in (0..region.start.0).rev() {
|
|
|
|
let index = screen_lines - i - 1;
|
|
|
|
self.raw.swap(index, index - positions.0);
|
|
|
|
}
|
2018-01-15 05:51:56 +00:00
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// Rotate the entire line buffer upward.
|
|
|
|
self.raw.rotate(-(positions.0 as isize));
|
2018-01-14 18:17:08 +00:00
|
|
|
|
2020-12-10 02:10:24 +00:00
|
|
|
// Ensure all new lines are fully cleared.
|
|
|
|
for i in 0..positions.0 {
|
|
|
|
self.raw[i].reset(&self.cursor.template);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the fixed lines at the bottom back into position.
|
|
|
|
let fixed_lines = screen_lines - region.end.0;
|
|
|
|
for i in 0..fixed_lines {
|
|
|
|
self.raw.swap(i, i + positions.0);
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 21:30:29 +00:00
|
|
|
|
2020-11-05 04:45:14 +00:00
|
|
|
pub fn clear_viewport<D>(&mut self)
|
|
|
|
where
|
|
|
|
T: ResetDiscriminant<D>,
|
|
|
|
D: PartialEq,
|
|
|
|
{
|
2019-11-18 21:15:25 +00:00
|
|
|
// Determine how many lines to scroll up by.
|
2021-01-24 21:45:36 +00:00
|
|
|
let end = Point { line: 0, column: self.cols() };
|
2019-11-18 21:15:25 +00:00
|
|
|
let mut iter = self.iter_from(end);
|
|
|
|
while let Some(cell) = iter.prev() {
|
2021-01-24 21:45:36 +00:00
|
|
|
if !cell.is_empty() || cell.point.line >= *self.lines {
|
2019-11-18 21:15:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 21:45:36 +00:00
|
|
|
debug_assert!(iter.point.line <= *self.lines);
|
|
|
|
let positions = self.lines - iter.point.line;
|
2020-07-09 21:45:22 +00:00
|
|
|
let region = Line(0)..self.screen_lines();
|
2019-11-18 21:15:25 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Reset display offset.
|
2019-12-22 11:05:19 +00:00
|
|
|
self.display_offset = 0;
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Clear the viewport.
|
2020-11-05 04:45:14 +00:00
|
|
|
self.scroll_up(®ion, positions);
|
2019-11-18 21:15:25 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Reset rotated lines.
|
2019-11-18 21:15:25 +00:00
|
|
|
for i in positions.0..self.lines.0 {
|
2020-11-05 04:45:14 +00:00
|
|
|
self.raw[i].reset(&self.cursor.template);
|
2019-11-18 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Completely reset the grid state.
|
2020-11-05 04:45:14 +00:00
|
|
|
pub fn reset<D>(&mut self)
|
|
|
|
where
|
|
|
|
T: ResetDiscriminant<D>,
|
|
|
|
D: PartialEq,
|
|
|
|
{
|
2019-03-02 21:30:29 +00:00
|
|
|
self.clear_history();
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
self.saved_cursor = Cursor::default();
|
|
|
|
self.cursor = Cursor::default();
|
2019-03-02 21:30:29 +00:00
|
|
|
self.display_offset = 0;
|
2020-11-05 04:45:14 +00:00
|
|
|
|
|
|
|
// Reset all visible lines.
|
|
|
|
for row in 0..self.raw.len() {
|
|
|
|
self.raw[row].reset(&self.cursor.template);
|
|
|
|
}
|
2019-03-02 21:30:29 +00:00
|
|
|
}
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Grid<T> {
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Reset a visible region within the grid.
|
|
|
|
pub fn reset_region<D, R: RangeBounds<Line>>(&mut self, bounds: R)
|
|
|
|
where
|
|
|
|
T: ResetDiscriminant<D> + GridCell + Clone + Default,
|
|
|
|
D: PartialEq,
|
|
|
|
{
|
|
|
|
let start = match bounds.start_bound() {
|
|
|
|
Bound::Included(line) => *line,
|
|
|
|
Bound::Excluded(line) => *line + 1,
|
|
|
|
Bound::Unbounded => Line(0),
|
|
|
|
};
|
|
|
|
|
|
|
|
let end = match bounds.end_bound() {
|
|
|
|
Bound::Included(line) => *line + 1,
|
|
|
|
Bound::Excluded(line) => *line,
|
|
|
|
Bound::Unbounded => self.screen_lines(),
|
|
|
|
};
|
|
|
|
|
|
|
|
debug_assert!(start < self.screen_lines());
|
|
|
|
debug_assert!(end <= self.screen_lines());
|
|
|
|
|
|
|
|
for row in start.0..end.0 {
|
|
|
|
self.raw[Line(row)].reset(&self.cursor.template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:45:22 +00:00
|
|
|
/// Clamp a buffer point to the visible region.
|
|
|
|
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 {
|
|
|
|
// Since edgecases are handled, conversion is identical as visible to buffer.
|
|
|
|
self.visible_to_buffer(point.into()).into()
|
|
|
|
}
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 05:40:09 +00:00
|
|
|
// Clamp a buffer point based range to the viewport.
|
|
|
|
//
|
|
|
|
// This will make sure the content within the range is visible and return `None` whenever the
|
|
|
|
// entire range is outside the visible region.
|
|
|
|
pub fn clamp_buffer_range_to_visible(
|
|
|
|
&self,
|
|
|
|
range: &RangeInclusive<Point<usize>>,
|
|
|
|
) -> Option<RangeInclusive<Point>> {
|
|
|
|
let start = range.start();
|
|
|
|
let end = range.end();
|
|
|
|
|
|
|
|
// Check if the range is completely offscreen
|
|
|
|
let viewport_end = self.display_offset;
|
|
|
|
let viewport_start = viewport_end + self.lines.0 - 1;
|
|
|
|
if end.line > viewport_start || start.line < viewport_end {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = self.clamp_buffer_to_visible(*start);
|
|
|
|
let end = self.clamp_buffer_to_visible(*end);
|
|
|
|
|
|
|
|
Some(start..=end)
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:45:22 +00:00
|
|
|
/// Convert viewport relative point to global buffer indexing.
|
2020-01-26 13:49:58 +00:00
|
|
|
#[inline]
|
2020-07-09 21:45:22 +00:00
|
|
|
pub fn visible_to_buffer(&self, point: Point) -> Point<usize> {
|
2021-01-24 21:45:36 +00:00
|
|
|
Point { line: self.lines.0 + self.display_offset - point.line.0 - 1, column: point.column }
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
#[inline]
|
2018-08-03 22:12:23 +00:00
|
|
|
pub fn clear_history(&mut self) {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Explicitly purge all lines from history.
|
2020-01-26 13:49:58 +00:00
|
|
|
self.raw.shrink_lines(self.history_size());
|
2018-08-15 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// This is used only for initializing after loading ref-tests.
|
2020-01-26 13:49:58 +00:00
|
|
|
#[inline]
|
2020-11-05 04:45:14 +00:00
|
|
|
pub fn initialize_all(&mut self)
|
2018-12-08 01:50:01 +00:00
|
|
|
where
|
2020-11-05 04:45:14 +00:00
|
|
|
T: GridCell + Clone + Default,
|
2018-12-08 01:50:01 +00:00
|
|
|
{
|
2020-05-05 22:50:23 +00:00
|
|
|
// Remove all cached lines to clear them of any content.
|
2020-01-26 13:49:58 +00:00
|
|
|
self.truncate();
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Initialize everything with empty new lines.
|
2020-11-05 04:45:14 +00:00
|
|
|
self.raw.initialize(self.max_scroll_limit - self.history_size(), self.cols);
|
2018-12-08 01:50:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// This is used only for truncating before saving ref-tests.
|
2020-01-26 13:49:58 +00:00
|
|
|
#[inline]
|
2018-05-15 20:36:14 +00:00
|
|
|
pub fn truncate(&mut self) {
|
|
|
|
self.raw.truncate();
|
|
|
|
}
|
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
#[inline]
|
2018-12-10 17:53:56 +00:00
|
|
|
pub fn iter_from(&self, point: Point<usize>) -> GridIterator<'_, T> {
|
2021-01-24 21:45:36 +00:00
|
|
|
GridIterator { grid: self, point }
|
2019-02-07 00:55:18 +00:00
|
|
|
}
|
2020-05-30 20:45:44 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Iterator over all visible cells.
|
2020-05-30 20:45:44 +00:00
|
|
|
#[inline]
|
2021-01-24 21:45:36 +00:00
|
|
|
pub fn display_iter(&self) -> DisplayIter<'_, T> {
|
|
|
|
let start = Point::new(self.display_offset + self.lines.0, self.cols() - 1);
|
|
|
|
let end = Point::new(self.display_offset, self.cols());
|
2020-07-09 21:45:22 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
let iter = GridIterator { grid: self, point: start };
|
2020-07-09 21:45:22 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
let display_offset = self.display_offset;
|
|
|
|
let lines = self.lines.0;
|
2020-07-09 21:45:22 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
let take_while: DisplayIterTakeFun<'_, T> =
|
|
|
|
Box::new(move |indexed: &Indexed<&T>| indexed.point <= end);
|
|
|
|
let map: DisplayIterMapFun<'_, T> = Box::new(move |indexed: Indexed<&T>| {
|
|
|
|
let line = Line(lines + display_offset - indexed.point.line - 1);
|
|
|
|
Indexed { point: Point::new(line, indexed.point.column), cell: indexed.cell }
|
|
|
|
});
|
|
|
|
iter.take_while(take_while).map(map)
|
2020-07-09 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-01-24 21:45:36 +00:00
|
|
|
pub fn display_offset(&self) -> usize {
|
|
|
|
self.display_offset
|
2020-07-09 21:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-01-24 21:45:36 +00:00
|
|
|
pub fn cursor_cell(&mut self) -> &mut T {
|
|
|
|
let point = self.cursor.point;
|
|
|
|
&mut self[point.line][point.column]
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<T: PartialEq> 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)
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
impl<T> Index<Line> for Grid<T> {
|
2016-07-04 00:00:00 +00:00
|
|
|
type Output = Row<T>;
|
2016-04-10 23:19:39 +00:00
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2020-01-09 23:06:41 +00:00
|
|
|
fn index(&self, index: Line) -> &Row<T> {
|
2018-02-16 02:35:49 +00:00
|
|
|
&self.raw[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
impl<T> IndexMut<Line> for Grid<T> {
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2020-01-09 23:06:41 +00:00
|
|
|
fn index_mut(&mut self, index: Line) -> &mut Row<T> {
|
2018-02-12 05:25:33 +00:00
|
|
|
&mut self.raw[index]
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<T> Index<usize> for Grid<T> {
|
|
|
|
type Output = Row<T>;
|
2016-05-31 03:44:37 +00:00
|
|
|
|
|
|
|
#[inline]
|
2021-01-24 21:45:36 +00:00
|
|
|
fn index(&self, index: usize) -> &Row<T> {
|
|
|
|
&self.raw[index]
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<T> IndexMut<usize> for Grid<T> {
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2021-01-24 21:45:36 +00:00
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Row<T> {
|
|
|
|
&mut self.raw[index]
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:45:22 +00:00
|
|
|
impl<T> Index<Point<usize>> for Grid<T> {
|
|
|
|
type Output = T;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, point: Point<usize>) -> &T {
|
2021-01-24 21:45:36 +00:00
|
|
|
&self[point.line][point.column]
|
2020-07-09 21:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<Point<usize>> for Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, point: Point<usize>) -> &mut T {
|
2021-01-24 21:45:36 +00:00
|
|
|
&mut self[point.line][point.column]
|
2020-07-09 21:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<T> Index<Point> for Grid<T> {
|
|
|
|
type Output = T;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[inline]
|
|
|
|
fn index(&self, point: Point) -> &T {
|
|
|
|
&self[point.line][point.column]
|
|
|
|
}
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<T> IndexMut<Point> for Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, point: Point) -> &mut T {
|
|
|
|
&mut self[point.line][point.column]
|
2017-10-13 03:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Grid dimensions.
|
|
|
|
pub trait Dimensions {
|
|
|
|
/// Total number of lines in the buffer, this includes scrollback and visible lines.
|
|
|
|
fn total_lines(&self) -> usize;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Height of the viewport in lines.
|
|
|
|
fn screen_lines(&self) -> Line;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Width of the terminal in columns.
|
|
|
|
fn cols(&self) -> Column;
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Number of invisible lines part of the scrollback history.
|
|
|
|
#[inline]
|
|
|
|
fn history_size(&self) -> usize {
|
|
|
|
self.total_lines() - self.screen_lines().0
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<G> Dimensions for Grid<G> {
|
|
|
|
#[inline]
|
|
|
|
fn total_lines(&self) -> usize {
|
|
|
|
self.raw.len()
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[inline]
|
|
|
|
fn screen_lines(&self) -> Line {
|
|
|
|
self.lines
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[inline]
|
|
|
|
fn cols(&self) -> Column {
|
|
|
|
self.cols
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
impl Dimensions for (Line, Column) {
|
|
|
|
fn total_lines(&self) -> usize {
|
|
|
|
*self.0
|
2017-10-13 03:12:29 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
fn screen_lines(&self) -> Line {
|
|
|
|
self.0
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
fn cols(&self) -> Column {
|
|
|
|
self.1
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct Indexed<T, L = usize> {
|
|
|
|
pub point: Point<L>,
|
|
|
|
pub cell: T,
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2017-01-15 01:53:48 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<T, L> Deref for Indexed<T, L> {
|
|
|
|
type Target = T;
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.cell
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Grid cell iterator.
|
|
|
|
pub struct GridIterator<'a, T> {
|
|
|
|
/// Immutable grid reference.
|
2018-02-16 02:35:49 +00:00
|
|
|
grid: &'a Grid<T>,
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Current position of the iterator within the grid.
|
|
|
|
point: Point<usize>,
|
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<'a, T> GridIterator<'a, T> {
|
|
|
|
/// Current iteratior position.
|
|
|
|
pub fn point(&self) -> Point<usize> {
|
|
|
|
self.point
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
/// Cell at the current iteratior position.
|
|
|
|
pub fn cell(&self) -> &'a T {
|
|
|
|
&self.grid[self.point]
|
2018-02-16 03:34:09 +00:00
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<'a, T> Iterator for GridIterator<'a, T> {
|
2020-11-05 04:45:14 +00:00
|
|
|
type Item = Indexed<&'a T>;
|
2018-02-16 02:35:49 +00:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2021-01-24 21:45:36 +00:00
|
|
|
let last_col = self.grid.cols() - 1;
|
|
|
|
match self.point {
|
|
|
|
Point { line, column: col } if line == 0 && col == last_col => return None,
|
|
|
|
Point { column: col, .. } if (col == last_col) => {
|
|
|
|
self.point.line -= 1;
|
|
|
|
self.point.column = Column(0);
|
|
|
|
},
|
|
|
|
_ => self.point.column += Column(1),
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
Some(Indexed { cell: &self.grid[self.point], point: self.point })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bidirectional iterator.
|
|
|
|
pub trait BidirectionalIterator: Iterator {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item>;
|
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
impl<'a, T> BidirectionalIterator for GridIterator<'a, T> {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item> {
|
|
|
|
let last_col = self.grid.cols() - 1;
|
2018-04-21 11:45:43 +00:00
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
match self.point {
|
|
|
|
Point { line, column: Column(0) } if line == self.grid.total_lines() - 1 => {
|
|
|
|
return None
|
|
|
|
},
|
|
|
|
Point { column: Column(0), .. } => {
|
|
|
|
self.point.line += 1;
|
|
|
|
self.point.column = last_col;
|
|
|
|
},
|
|
|
|
_ => self.point.column -= Column(1),
|
2018-04-21 11:45:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:36 +00:00
|
|
|
Some(Indexed { cell: &self.grid[self.point], point: self.point })
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-24 21:45:36 +00:00
|
|
|
|
|
|
|
pub type DisplayIter<'a, T> =
|
|
|
|
Map<TakeWhile<GridIterator<'a, T>, DisplayIterTakeFun<'a, T>>, DisplayIterMapFun<'a, T>>;
|
|
|
|
type DisplayIterTakeFun<'a, T> = Box<dyn Fn(&Indexed<&'a T>) -> bool>;
|
|
|
|
type DisplayIterMapFun<'a, T> = Box<dyn FnMut(Indexed<&'a T>) -> Indexed<&'a T, Line>>;
|