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};
|
2019-11-03 20:59:28 +00:00
|
|
|
use std::ops::{Deref, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo};
|
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-05-30 20:45:44 +00:00
|
|
|
use crate::term::cell::{Cell, Flags};
|
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-05-05 22:50:23 +00:00
|
|
|
/// Bidirectional iterator.
|
2017-01-15 01:53:48 +00:00
|
|
|
pub trait BidirectionalIterator: Iterator {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item>;
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
/// An item in the grid along with its Line and Column.
|
2017-02-11 20:49:40 +00:00
|
|
|
pub struct Indexed<T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
pub inner: T,
|
2017-02-11 20:49:40 +00:00
|
|
|
pub line: Line,
|
|
|
|
pub column: Column,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Indexed<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:57:34 +00:00
|
|
|
impl<T: PartialEq> ::std::cmp::PartialEq for Grid<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Compare struct fields and check result of grid comparison.
|
2019-03-30 16:48:36 +00:00
|
|
|
self.raw.eq(&other.raw)
|
|
|
|
&& self.cols.eq(&other.cols)
|
|
|
|
&& self.lines.eq(&other.lines)
|
|
|
|
&& self.display_offset.eq(&other.display_offset)
|
2018-03-05 17:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:55:18 +00:00
|
|
|
pub trait GridCell {
|
|
|
|
fn is_empty(&self) -> bool;
|
2020-01-09 23:06:41 +00:00
|
|
|
fn flags(&self) -> &Flags;
|
|
|
|
fn flags_mut(&mut self) -> &mut Flags;
|
2019-12-09 23:35:13 +00:00
|
|
|
|
|
|
|
/// Fast equality approximation.
|
|
|
|
///
|
|
|
|
/// This is a faster alternative to [`PartialEq`],
|
2020-05-05 22:50:23 +00:00
|
|
|
/// but might report unequal cells as equal.
|
2019-12-09 23:35:13 +00:00
|
|
|
fn fast_eq(&self, other: Self) -> bool;
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Cursor {
|
|
|
|
/// The location of this cursor.
|
|
|
|
pub point: Point,
|
|
|
|
|
|
|
|
/// Template cell when using this cursor.
|
|
|
|
pub template: Cell,
|
|
|
|
|
|
|
|
/// 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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
/// ```
|
2018-03-05 17:57:34 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
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)]
|
|
|
|
pub cursor: Cursor,
|
|
|
|
|
|
|
|
/// Last saved cursor.
|
|
|
|
#[serde(skip)]
|
|
|
|
pub saved_cursor: Cursor,
|
|
|
|
|
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-06-18 01:02:56 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2018-03-11 12:01:06 +00:00
|
|
|
pub enum Scroll {
|
|
|
|
Lines(isize),
|
|
|
|
PageUp,
|
|
|
|
PageDown,
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
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,
|
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 22:17:38 +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 {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Since edge-cases are handled, conversion is identical as visible to buffer.
|
2020-03-07 22:17:38 +00:00
|
|
|
self.visible_to_buffer(point.into()).into()
|
2018-03-10 13:53:54 +00:00
|
|
|
}
|
2018-03-07 04:57:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 22:17:38 +00:00
|
|
|
/// Convert viewport relative point to global buffer indexing.
|
2019-08-01 15:37:01 +00:00
|
|
|
pub fn visible_to_buffer(&self, point: Point) -> Point<usize> {
|
2020-03-07 22:17:38 +00:00
|
|
|
Point { line: self.lines.0 + self.display_offset - point.line.0 - 1, col: point.col }
|
2018-03-07 04:57:40 +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) {
|
|
|
|
match scroll {
|
|
|
|
Scroll::Lines(count) => {
|
|
|
|
self.display_offset = min(
|
|
|
|
max((self.display_offset as isize) + count, 0isize) as usize,
|
2020-01-26 13:49:58 +00:00
|
|
|
self.history_size(),
|
2018-03-11 12:01:06 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
Scroll::PageUp => {
|
2020-01-26 13:49:58 +00:00
|
|
|
self.display_offset = min(self.display_offset + self.lines.0, self.history_size());
|
2018-03-11 12:01:06 +00:00
|
|
|
},
|
|
|
|
Scroll::PageDown => {
|
2019-03-30 16:48:36 +00:00
|
|
|
self.display_offset -= min(self.display_offset, self.lines.0);
|
2018-03-11 12:01:06 +00:00
|
|
|
},
|
2020-01-26 13:49:58 +00:00
|
|
|
Scroll::Top => self.display_offset = self.history_size(),
|
2018-03-11 12:01:06 +00:00
|
|
|
Scroll::Bottom => self.display_offset = 0,
|
2018-03-10 19:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
fn increase_scroll_limit(&mut self, count: usize, template: T) {
|
2020-01-26 13:49:58 +00:00
|
|
|
let count = min(count, self.max_scroll_limit - self.history_size());
|
|
|
|
if count != 0 {
|
|
|
|
self.raw.initialize(count, template, 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-05-30 20:45:44 +00:00
|
|
|
pub fn scroll_down(&mut self, region: &Range<Line>, positions: Line, template: T) {
|
2018-01-15 05:51:56 +00:00
|
|
|
// Whether or not there is a scrolling region active, as long as it
|
|
|
|
// starts at the top, we can do a full rotation which just involves
|
|
|
|
// changing the start index.
|
|
|
|
//
|
2019-04-28 21:42:43 +00:00
|
|
|
// To accommodate scroll regions, rows are reordered at the end.
|
2020-06-25 09:50:17 +00:00
|
|
|
if region.start == Line(0) && self.max_scroll_limit == 0 {
|
2018-01-15 05:51:56 +00:00
|
|
|
// Rotate the entire line buffer. If there's a scrolling region
|
|
|
|
// active, the bottom lines are restored in the next step.
|
2018-02-16 02:35:49 +00:00
|
|
|
self.raw.rotate_up(*positions);
|
2018-01-15 05:51:56 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Now, restore any scroll region lines.
|
2018-02-16 03:34:23 +00:00
|
|
|
let lines = self.lines;
|
2019-03-30 16:48:36 +00:00
|
|
|
for i in IndexRange(region.end..lines) {
|
2018-02-16 02:35:49 +00:00
|
|
|
self.raw.swap_lines(i, i + positions);
|
2018-01-15 05:51:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Finally, reset recycled lines.
|
2018-02-17 02:35:54 +00:00
|
|
|
for i in IndexRange(Line(0)..positions) {
|
2020-05-30 20:45:44 +00:00
|
|
|
self.raw[i].reset(template);
|
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-05-30 20:45:44 +00:00
|
|
|
self.raw[line].reset(template);
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Move lines at the bottom towards the top.
|
2018-02-11 18:07:33 +00:00
|
|
|
///
|
|
|
|
/// This is the performance-sensitive part of scrolling.
|
2020-05-30 20:45:44 +00:00
|
|
|
pub fn scroll_up(&mut self, region: &Range<Line>, positions: Line, template: T) {
|
2020-01-24 22:57:22 +00:00
|
|
|
let num_lines = self.num_lines().0;
|
|
|
|
|
2018-01-15 05:51:56 +00:00
|
|
|
if region.start == Line(0) {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Update display offset when not pinned to active area.
|
2018-02-12 05:25:33 +00:00
|
|
|
if self.display_offset != 0 {
|
2020-02-12 20:24:24 +00:00
|
|
|
self.display_offset = min(self.display_offset + *positions, self.max_scroll_limit);
|
2018-02-12 05:25:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:40:09 +00:00
|
|
|
self.increase_scroll_limit(*positions, template);
|
2018-02-16 03:06:39 +00:00
|
|
|
|
2018-01-15 05:51:56 +00:00
|
|
|
// Rotate the entire line buffer. If there's a scrolling region
|
|
|
|
// active, the bottom lines are restored in the next step.
|
2018-02-16 02:35:49 +00:00
|
|
|
self.raw.rotate(-(*positions as isize));
|
2018-01-15 05:51:56 +00:00
|
|
|
|
2019-11-18 21:15:25 +00:00
|
|
|
// This next loop swaps "fixed" lines outside of a scroll region
|
|
|
|
// back into place after the rotation. The work is done in buffer-
|
|
|
|
// space rather than terminal-space to avoid redundant
|
|
|
|
// transformations.
|
2020-01-24 22:57:22 +00:00
|
|
|
let fixed_lines = num_lines - *region.end;
|
2018-05-19 21:47:16 +00:00
|
|
|
|
|
|
|
for i in 0..fixed_lines {
|
|
|
|
self.raw.swap(i, i + *positions);
|
2018-01-15 05:51:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Finally, reset recycled lines.
|
2018-01-15 05:51:56 +00:00
|
|
|
//
|
|
|
|
// Recycled lines are just above the end of the scrolling region.
|
|
|
|
for i in 0..*positions {
|
2020-05-30 20:45:44 +00:00
|
|
|
self.raw[i + fixed_lines].reset(template);
|
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..(region.end - positions)) {
|
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
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Clear reused lines.
|
2019-03-30 16:48:36 +00:00
|
|
|
for line in IndexRange((region.end - positions)..region.end) {
|
2020-05-30 20:45:44 +00:00
|
|
|
self.raw[line].reset(template);
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 21:30:29 +00:00
|
|
|
|
2020-05-30 20:45:44 +00:00
|
|
|
pub fn clear_viewport(&mut self, template: T) {
|
2019-11-18 21:15:25 +00:00
|
|
|
// Determine how many lines to scroll up by.
|
|
|
|
let end = Point { line: 0, col: self.num_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.num_lines();
|
|
|
|
|
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.
|
2019-11-18 21:15:25 +00:00
|
|
|
self.scroll_up(®ion, positions, template);
|
|
|
|
|
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-05-30 20:45:44 +00:00
|
|
|
self.raw[i].reset(template);
|
2019-11-18 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Completely reset the grid state.
|
2020-05-30 20:45:44 +00:00
|
|
|
pub fn reset(&mut self, template: T) {
|
2019-03-02 21:30:29 +00:00
|
|
|
self.clear_history();
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Reset all visible lines.
|
2019-03-02 21:30:29 +00:00
|
|
|
for row in 0..self.raw.len() {
|
|
|
|
self.raw[row].reset(template);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-01-14 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:58:11 +00:00
|
|
|
#[allow(clippy::len_without_is_empty)]
|
2018-01-14 18:17:08 +00:00
|
|
|
impl<T> Grid<T> {
|
|
|
|
#[inline]
|
2020-01-09 23:06:41 +00:00
|
|
|
pub fn num_lines(&self) -> Line {
|
2018-01-14 18:17:08 +00:00
|
|
|
self.lines
|
|
|
|
}
|
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
#[inline]
|
2018-12-10 17:53:56 +00:00
|
|
|
pub fn display_iter(&self) -> DisplayIter<'_, T> {
|
2018-02-16 02:35:49 +00:00
|
|
|
DisplayIter::new(self)
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:17:08 +00:00
|
|
|
#[inline]
|
2020-01-09 23:06:41 +00:00
|
|
|
pub fn num_cols(&self) -> Column {
|
2018-01-14 18:17:08 +00:00
|
|
|
self.cols
|
|
|
|
}
|
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
|
|
|
/// Total number of lines in the buffer, this includes scrollback + visible lines.
|
2018-04-28 14:15:21 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.raw.len()
|
|
|
|
}
|
|
|
|
|
2018-12-08 01:50:01 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn history_size(&self) -> usize {
|
2020-01-26 13:49:58 +00:00
|
|
|
self.raw.len() - *self.lines
|
2018-12-08 01:50:01 +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-05-30 20:45:44 +00:00
|
|
|
pub fn initialize_all(&mut self, template: T)
|
2018-12-08 01:50:01 +00:00
|
|
|
where
|
2019-07-10 21:17:20 +00:00
|
|
|
T: Copy + GridCell,
|
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-01-26 13:49:58 +00:00
|
|
|
self.raw.initialize(self.max_scroll_limit - self.history_size(), template, 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> {
|
2019-03-30 16:48:36 +00:00
|
|
|
GridIterator { grid: self, cur: point }
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 00:55:18 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn display_offset(&self) -> usize {
|
|
|
|
self.display_offset
|
|
|
|
}
|
2020-05-30 20:45:44 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn cursor_cell(&mut self) -> &mut T {
|
|
|
|
let point = self.cursor.point;
|
|
|
|
&mut self[&point]
|
|
|
|
}
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:14:17 +00:00
|
|
|
pub struct GridIterator<'a, T> {
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Immutable grid reference.
|
2019-03-19 19:14:17 +00:00
|
|
|
grid: &'a Grid<T>,
|
|
|
|
|
2019-03-30 09:23:48 +00:00
|
|
|
/// Current position of the iterator within the grid.
|
2019-08-01 15:37:01 +00:00
|
|
|
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.line][self.cur.col]
|
|
|
|
}
|
2019-03-19 19:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 01:53:48 +00:00
|
|
|
impl<'a, T> Iterator for GridIterator<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2019-03-30 09:23:48 +00:00
|
|
|
let last_col = self.grid.num_cols() - Column(1);
|
|
|
|
match self.cur {
|
|
|
|
Point { line, col } if line == 0 && col == last_col => None,
|
2019-03-30 16:48:36 +00:00
|
|
|
Point { col, .. } if (col == last_col) => {
|
2019-03-30 09:23:48 +00:00
|
|
|
self.cur.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])
|
2019-03-30 16:48:36 +00:00
|
|
|
},
|
2019-03-30 09:23:48 +00:00
|
|
|
}
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> BidirectionalIterator for GridIterator<'a, T> {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item> {
|
2019-03-30 09:23:48 +00:00
|
|
|
let num_cols = self.grid.num_cols();
|
|
|
|
|
|
|
|
match self.cur {
|
|
|
|
Point { line, col: Column(0) } if line == self.grid.len() - 1 => None,
|
|
|
|
Point { col: Column(0), .. } => {
|
|
|
|
self.cur.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])
|
2019-03-30 16:48:36 +00:00
|
|
|
},
|
2019-03-30 09:23:48 +00:00
|
|
|
}
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Index active region by line.
|
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-05-05 22:50:23 +00:00
|
|
|
/// Index with buffer offset.
|
2018-02-16 02:35:49 +00:00
|
|
|
impl<T> Index<usize> for Grid<T> {
|
|
|
|
type Output = Row<T>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &Row<T> {
|
2018-02-12 05:25:33 +00:00
|
|
|
&self.raw[index]
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:14:17 +00:00
|
|
|
impl<T> IndexMut<usize> for Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Row<T> {
|
|
|
|
&mut self.raw[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 16:09:29 +00:00
|
|
|
impl<'point, T> Index<&'point Point> for Grid<T> {
|
2016-07-04 00:00:00 +00:00
|
|
|
type Output = T;
|
2016-05-31 03:44:37 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-12-29 16:09:29 +00:00
|
|
|
fn index<'a>(&'a self, point: &Point) -> &'a T {
|
2018-02-12 05:25:33 +00:00
|
|
|
&self[point.line][point.col]
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 16:09:29 +00:00
|
|
|
impl<'point, T> IndexMut<&'point Point> for Grid<T> {
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2016-12-29 16:09:29 +00:00
|
|
|
fn index_mut<'a, 'b>(&'a mut self, point: &'b Point) -> &'a mut T {
|
2018-02-12 05:25:33 +00:00
|
|
|
&mut self[point.line][point.col]
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// A subset of lines in the grid.
|
2017-10-13 03:01:28 +00:00
|
|
|
///
|
2020-05-05 22:50:23 +00:00
|
|
|
/// May be constructed using Grid::region(..).
|
2018-12-10 17:53:56 +00:00
|
|
|
pub struct Region<'a, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
start: Line,
|
|
|
|
end: Line,
|
2018-05-30 04:33:13 +00:00
|
|
|
raw: &'a Storage<T>,
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// A mutable subset of lines in the grid.
|
2017-10-13 03:01:28 +00:00
|
|
|
///
|
2020-05-05 22:50:23 +00:00
|
|
|
/// May be constructed using Grid::region_mut(..).
|
2018-12-10 17:53:56 +00:00
|
|
|
pub struct RegionMut<'a, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
start: Line,
|
|
|
|
end: Line,
|
2018-05-30 04:33:13 +00:00
|
|
|
raw: &'a mut Storage<T>,
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:12:29 +00:00
|
|
|
impl<'a, T> RegionMut<'a, T> {
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Call the provided function for every item in this region.
|
2017-10-13 03:12:29 +00:00
|
|
|
pub fn each<F: Fn(&mut T)>(self, func: F) {
|
|
|
|
for row in self {
|
|
|
|
for item in row {
|
|
|
|
func(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
pub trait IndexRegion<I, T> {
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get an immutable region of Self.
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region(&self, _: I) -> Region<'_, T>;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get a mutable region of Self.
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region_mut(&mut self, _: I) -> RegionMut<'_, T>;
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<T> IndexRegion<Range<Line>, T> for Grid<T> {
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region(&self, index: Range<Line>) -> Region<'_, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
assert!(index.start <= index.end);
|
2019-03-30 16:48:36 +00:00
|
|
|
Region { start: index.start, end: index.end, raw: &self.raw }
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region_mut(&mut self, index: Range<Line>) -> RegionMut<'_, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
assert!(index.start <= index.end);
|
2019-03-30 16:48:36 +00:00
|
|
|
RegionMut { start: index.start, end: index.end, raw: &mut self.raw }
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<T> IndexRegion<RangeTo<Line>, T> for Grid<T> {
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region(&self, index: RangeTo<Line>) -> Region<'_, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
assert!(index.end <= self.num_lines());
|
2019-03-30 16:48:36 +00:00
|
|
|
Region { start: Line(0), end: index.end, raw: &self.raw }
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<'_, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
assert!(index.end <= self.num_lines());
|
2019-03-30 16:48:36 +00:00
|
|
|
RegionMut { start: Line(0), end: index.end, raw: &mut self.raw }
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> {
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region(&self, index: RangeFrom<Line>) -> Region<'_, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
assert!(index.start < self.num_lines());
|
2019-03-30 16:48:36 +00:00
|
|
|
Region { start: index.start, end: self.num_lines(), raw: &self.raw }
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<'_, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
assert!(index.start < self.num_lines());
|
2019-03-30 16:48:36 +00:00
|
|
|
RegionMut { start: index.start, end: self.num_lines(), raw: &mut self.raw }
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:12:29 +00:00
|
|
|
impl<T> IndexRegion<RangeFull, T> for Grid<T> {
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region(&self, _: RangeFull) -> Region<'_, T> {
|
2019-03-30 16:48:36 +00:00
|
|
|
Region { start: Line(0), end: self.num_lines(), raw: &self.raw }
|
2017-10-13 03:12:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 17:53:56 +00:00
|
|
|
fn region_mut(&mut self, _: RangeFull) -> RegionMut<'_, T> {
|
2019-03-30 16:48:36 +00:00
|
|
|
RegionMut { start: Line(0), end: self.num_lines(), raw: &mut self.raw }
|
2017-10-13 03:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:53:56 +00:00
|
|
|
pub struct RegionIter<'a, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
end: Line,
|
|
|
|
cur: Line,
|
2018-05-30 04:33:13 +00:00
|
|
|
raw: &'a Storage<T>,
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2018-12-10 17:53:56 +00:00
|
|
|
pub struct RegionIterMut<'a, T> {
|
2017-10-13 03:01:28 +00:00
|
|
|
end: Line,
|
|
|
|
cur: Line,
|
2018-05-30 04:33:13 +00:00
|
|
|
raw: &'a mut Storage<T>,
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<'a, T> IntoIterator for Region<'a, T> {
|
|
|
|
type IntoIter = RegionIter<'a, T>;
|
2019-03-30 16:48:36 +00:00
|
|
|
type Item = &'a Row<T>;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2019-03-30 16:48:36 +00:00
|
|
|
RegionIter { end: self.end, cur: self.start, raw: self.raw }
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<'a, T> IntoIterator for RegionMut<'a, T> {
|
|
|
|
type IntoIter = RegionIterMut<'a, T>;
|
2019-03-30 16:48:36 +00:00
|
|
|
type Item = &'a mut Row<T>;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2019-03-30 16:48:36 +00:00
|
|
|
RegionIterMut { end: self.end, cur: self.start, raw: self.raw }
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<'a, T> Iterator for RegionIter<'a, T> {
|
|
|
|
type Item = &'a Row<T>;
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.cur < self.end {
|
|
|
|
let index = self.cur;
|
|
|
|
self.cur += 1;
|
2018-02-16 02:35:49 +00:00
|
|
|
Some(&self.raw[index])
|
2017-10-13 03:01:28 +00:00
|
|
|
} else {
|
|
|
|
None
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 03:01:28 +00:00
|
|
|
}
|
2017-01-15 01:53:48 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
impl<'a, T> Iterator for RegionIterMut<'a, T> {
|
|
|
|
type Item = &'a mut Row<T>;
|
2019-03-30 16:48:36 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.cur < self.end {
|
|
|
|
let index = self.cur;
|
|
|
|
self.cur += 1;
|
2019-03-30 16:48:36 +00:00
|
|
|
unsafe { Some(&mut *(&mut self.raw[index] as *mut _)) }
|
2017-10-13 03:01:28 +00:00
|
|
|
} else {
|
|
|
|
None
|
2017-01-15 01:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Iterates over the visible area accounting for buffer transform.
|
2018-12-10 17:53:56 +00:00
|
|
|
pub struct DisplayIter<'a, T> {
|
2018-02-16 02:35:49 +00:00
|
|
|
grid: &'a Grid<T>,
|
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
|
|
|
col: Column,
|
2018-02-16 03:34:09 +00:00
|
|
|
line: Line,
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: 'a> DisplayIter<'a, T> {
|
|
|
|
pub fn new(grid: &'a Grid<T>) -> DisplayIter<'a, T> {
|
|
|
|
let offset = grid.display_offset + *grid.num_lines() - 1;
|
2019-03-30 16:48:36 +00:00
|
|
|
let limit = grid.display_offset;
|
2018-02-16 02:35:49 +00:00
|
|
|
let col = Column(0);
|
2018-02-16 03:34:09 +00:00
|
|
|
let line = Line(0);
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2018-02-16 03:34:09 +00:00
|
|
|
DisplayIter { grid, offset, col, limit, line }
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn offset(&self) -> usize {
|
|
|
|
self.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn column(&self) -> Column {
|
|
|
|
self.col
|
|
|
|
}
|
2018-02-16 03:34:09 +00:00
|
|
|
|
|
|
|
pub fn line(&self) -> Line {
|
|
|
|
self.line
|
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Copy + 'a> Iterator for DisplayIter<'a, T> {
|
|
|
|
type Item = Indexed<T>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2018-04-21 11:45:43 +00:00
|
|
|
// Return None if we've reached the end.
|
|
|
|
if self.offset == self.limit && self.grid.num_cols() == self.col {
|
|
|
|
return None;
|
2018-02-16 02:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 11:45:43 +00:00
|
|
|
// Get the next item.
|
2018-02-16 02:35:49 +00:00
|
|
|
let item = Some(Indexed {
|
|
|
|
inner: self.grid.raw[self.offset][self.col],
|
2018-02-16 03:34:09 +00:00
|
|
|
line: self.line,
|
2019-03-30 16:48:36 +00:00
|
|
|
column: self.col,
|
2018-02-16 02:35:49 +00:00
|
|
|
});
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Update line/col to point to next item.
|
2018-04-21 11:37:16 +00:00
|
|
|
self.col += 1;
|
2018-07-21 17:17:41 +00:00
|
|
|
if self.col == self.grid.num_cols() && self.offset != self.limit {
|
|
|
|
self.offset -= 1;
|
2018-04-21 11:45:43 +00:00
|
|
|
|
2018-07-21 17:17:41 +00:00
|
|
|
self.col = Column(0);
|
|
|
|
self.line = Line(*self.grid.lines - 1 - (self.offset - self.limit));
|
2018-04-21 11:45:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 02:35:49 +00:00
|
|
|
item
|
|
|
|
}
|
|
|
|
}
|