2016-06-30 03:56:12 +00: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 23:19:39 +00:00
|
|
|
|
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
|
|
|
|
2019-03-30 16:48:36 +00:00
|
|
|
use std::cmp::{max, min, Ordering};
|
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-01-09 23:06:41 +00:00
|
|
|
use crate::index::{Column, IndexRange, Line, Point};
|
2018-12-10 17:53:56 +00:00
|
|
|
use crate::selection::Selection;
|
2020-01-09 23:06:41 +00:00
|
|
|
use crate::term::cell::Flags;
|
2016-07-04 00:00:00 +00:00
|
|
|
|
2017-10-13 03:01:28 +00:00
|
|
|
mod row;
|
|
|
|
pub use self::row::Row;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2018-01-15 05:51:56 +00:00
|
|
|
mod storage;
|
|
|
|
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)
|
|
|
|
&& self.selection.eq(&other.selection)
|
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-05 22:50:23 +00:00
|
|
|
/// Represents the terminal display contents.
|
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> {
|
|
|
|
/// 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
|
|
|
/// Selected region.
|
2018-03-05 17:57:34 +00:00
|
|
|
#[serde(skip)]
|
|
|
|
pub selection: Option<Selection>,
|
2018-09-24 18:40:09 +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
|
|
|
}
|
|
|
|
|
2018-07-21 17:17:41 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2018-03-11 12:01:06 +00:00
|
|
|
pub enum Scroll {
|
|
|
|
Lines(isize),
|
|
|
|
PageUp,
|
|
|
|
PageDown,
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
}
|
|
|
|
|
2019-12-09 23:35:13 +00:00
|
|
|
impl<T: GridCell + PartialEq + Copy> Grid<T> {
|
2020-01-09 23:06:41 +00:00
|
|
|
pub fn new(lines: Line, cols: Column, scrollback: usize, template: T) -> Grid<T> {
|
2018-09-24 18:40:09 +00:00
|
|
|
let raw = Storage::with_capacity(lines, Row::new(cols, &template));
|
2020-01-26 13:49:58 +00:00
|
|
|
Grid { raw, cols, lines, display_offset: 0, selection: None, max_scroll_limit: scrollback }
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:44:54 +00:00
|
|
|
pub fn resize(
|
|
|
|
&mut self,
|
2019-06-23 23:29:01 +00:00
|
|
|
reflow: bool,
|
2020-01-09 23:06:41 +00:00
|
|
|
lines: Line,
|
|
|
|
cols: Column,
|
2019-03-13 18:55:18 +00:00
|
|
|
cursor_pos: &mut Point,
|
2018-04-02 15:44:54 +00:00
|
|
|
template: &T,
|
|
|
|
) {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Check that there's actually work to do and return early if not.
|
2016-07-04 00:00:00 +00:00
|
|
|
if lines == self.lines && cols == self.cols {
|
2016-06-29 17:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
match self.lines.cmp(&lines) {
|
2020-03-24 01:29:07 +00:00
|
|
|
Ordering::Less => self.grow_lines(lines, cursor_pos, template),
|
|
|
|
Ordering::Greater => self.shrink_lines(lines, cursor_pos, template),
|
2016-06-29 17:21:02 +00:00
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.cols.cmp(&cols) {
|
2019-06-23 23:29:01 +00:00
|
|
|
Ordering::Less => self.grow_cols(reflow, cols, cursor_pos, template),
|
|
|
|
Ordering::Greater => self.shrink_cols(reflow, cols, template),
|
2016-06-29 17:21:02 +00:00
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 16:48:36 +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()));
|
|
|
|
}
|
2018-02-17 02:35:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Add lines to the visible area.
|
2018-02-16 02:35:49 +00:00
|
|
|
///
|
2018-09-02 00:30:03 +00:00
|
|
|
/// Alacritty keeps the cursor at the bottom of the terminal as long as there
|
|
|
|
/// is scrollback available. Once scrollback is exhausted, new lines are
|
|
|
|
/// simply added to the bottom of the screen.
|
2020-03-24 01:29:07 +00:00
|
|
|
fn grow_lines(&mut self, new_line_count: Line, cursor_pos: &mut Point, template: &T) {
|
2018-02-16 03:06:39 +00:00
|
|
|
let lines_added = new_line_count - self.lines;
|
2016-06-29 17:21:02 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Need to "resize" before updating buffer.
|
2018-04-22 12:39:55 +00:00
|
|
|
self.raw.grow_visible_lines(new_line_count, Row::new(self.cols, template));
|
2018-02-16 03:06:39 +00:00
|
|
|
self.lines = new_line_count;
|
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
let history_size = self.history_size();
|
2020-03-24 01:29:07 +00:00
|
|
|
let from_history = min(history_size, lines_added.0);
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Move cursor down for all lines pulled from history.
|
2020-03-24 01:29:07 +00:00
|
|
|
cursor_pos.line += from_history;
|
|
|
|
|
|
|
|
if from_history != lines_added.0 {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Move existing lines up for every line that couldn't be pulled from history.
|
2020-03-24 01:29:07 +00:00
|
|
|
self.scroll_up(&(Line(0)..new_line_count), lines_added - from_history, template);
|
2018-08-15 20:09:59 +00:00
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
self.decrease_scroll_limit(*lines_added);
|
2019-03-13 18:55:18 +00:00
|
|
|
self.display_offset = self.display_offset.saturating_sub(*lines_added);
|
2016-06-29 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Grow number of columns in each row, reflowing if necessary.
|
2020-01-09 23:06:41 +00:00
|
|
|
fn grow_cols(&mut self, reflow: bool, cols: Column, cursor_pos: &mut Point, template: &T) {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Check if a row needs to be wrapped.
|
2020-01-09 23:06:41 +00:00
|
|
|
let should_reflow = |row: &Row<T>| -> bool {
|
|
|
|
let len = Column(row.len());
|
|
|
|
reflow && len < cols && row[len - 1].flags().contains(Flags::WRAPLINE)
|
|
|
|
};
|
|
|
|
|
2019-06-23 23:29:01 +00:00
|
|
|
let mut new_empty_lines = 0;
|
2020-01-09 23:06:41 +00:00
|
|
|
let mut reversed: Vec<Row<T>> = Vec::with_capacity(self.raw.len());
|
2019-06-23 23:29:01 +00:00
|
|
|
for (i, mut row) in self.raw.drain().enumerate().rev() {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Check if reflowing should be performed.
|
2020-03-18 02:35:08 +00:00
|
|
|
let last_row = match reversed.last_mut() {
|
|
|
|
Some(last_row) if should_reflow(last_row) => last_row,
|
2020-01-09 23:06:41 +00:00
|
|
|
_ => {
|
|
|
|
reversed.push(row);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Remove wrap flag before appending additional cells.
|
2020-01-09 23:06:41 +00:00
|
|
|
if let Some(cell) = last_row.last_mut() {
|
|
|
|
cell.flags_mut().remove(Flags::WRAPLINE);
|
|
|
|
}
|
2019-03-13 18:55:18 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Remove leading spacers when reflowing wide char to the previous line.
|
2020-01-09 23:06:41 +00:00
|
|
|
let last_len = last_row.len();
|
|
|
|
if last_len >= 2
|
|
|
|
&& !last_row[Column(last_len - 2)].flags().contains(Flags::WIDE_CHAR)
|
|
|
|
&& last_row[Column(last_len - 1)].flags().contains(Flags::WIDE_CHAR_SPACER)
|
|
|
|
{
|
|
|
|
last_row.shrink(Column(last_len - 1));
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Append as many cells from the next line as possible.
|
2020-01-09 23:06:41 +00:00
|
|
|
let len = min(row.len(), cols.0 - last_row.len());
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Insert leading spacer when there's not enough room for reflowing wide char.
|
2020-01-09 23:06:41 +00:00
|
|
|
let mut cells = if row[Column(len - 1)].flags().contains(Flags::WIDE_CHAR) {
|
|
|
|
let mut cells = row.front_split_off(len - 1);
|
|
|
|
|
|
|
|
let mut spacer = *template;
|
|
|
|
spacer.flags_mut().insert(Flags::WIDE_CHAR_SPACER);
|
|
|
|
cells.push(spacer);
|
|
|
|
|
|
|
|
cells
|
|
|
|
} else {
|
|
|
|
row.front_split_off(len)
|
|
|
|
};
|
|
|
|
|
|
|
|
last_row.append(&mut cells);
|
|
|
|
|
|
|
|
if row.is_empty() {
|
2020-01-26 13:49:58 +00:00
|
|
|
if i + reversed.len() < self.lines.0 {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Add new line and move lines up if we can't pull from history.
|
2020-01-09 23:06:41 +00:00
|
|
|
cursor_pos.line = Line(cursor_pos.line.saturating_sub(1));
|
|
|
|
new_empty_lines += 1;
|
2020-01-26 13:49:58 +00:00
|
|
|
} else if i < self.display_offset {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Keep viewport in place if line is outside of the visible area.
|
2020-01-26 13:49:58 +00:00
|
|
|
self.display_offset = self.display_offset.saturating_sub(1);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
2020-01-09 23:06:41 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Don't push line into the new buffer.
|
2020-01-09 23:06:41 +00:00
|
|
|
continue;
|
|
|
|
} else if let Some(cell) = last_row.last_mut() {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Set wrap flag if next line still has cells.
|
2020-01-09 23:06:41 +00:00
|
|
|
cell.flags_mut().insert(Flags::WRAPLINE);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
reversed.push(row);
|
2019-06-23 23:29:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Add padding lines.
|
2020-01-09 23:06:41 +00:00
|
|
|
reversed.append(&mut vec![Row::new(cols, template); new_empty_lines]);
|
2019-06-23 23:29:01 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Fill remaining cells and reverse iterator.
|
2020-01-09 23:06:41 +00:00
|
|
|
let mut new_raw = Vec::with_capacity(reversed.len());
|
|
|
|
for mut row in reversed.drain(..).rev() {
|
2019-06-23 23:29:01 +00:00
|
|
|
if row.len() < cols.0 {
|
|
|
|
row.grow(cols, template);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
2020-01-09 23:06:41 +00:00
|
|
|
new_raw.push(row);
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
2016-06-29 17:21:02 +00:00
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
self.raw.replace_inner(new_raw);
|
2019-06-23 23:29:01 +00:00
|
|
|
|
2020-01-26 13:49:58 +00:00
|
|
|
self.display_offset = min(self.display_offset, self.history_size());
|
2016-07-04 00:00:00 +00:00
|
|
|
self.cols = cols;
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Shrink number of columns in each row, reflowing if necessary.
|
2020-01-09 23:06:41 +00:00
|
|
|
fn shrink_cols(&mut self, reflow: bool, cols: Column, template: &T) {
|
2019-06-23 23:29:01 +00:00
|
|
|
let mut new_raw = Vec::with_capacity(self.raw.len());
|
|
|
|
let mut buffered = None;
|
|
|
|
for (i, mut row) in self.raw.drain().enumerate().rev() {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Append lines left over from previous row.
|
2019-06-23 23:29:01 +00:00
|
|
|
if let Some(buffered) = buffered.take() {
|
|
|
|
row.append_front(buffered);
|
|
|
|
}
|
2019-03-13 18:55:18 +00:00
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
loop {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Check if reflowing should be performed.
|
2020-03-18 02:35:08 +00:00
|
|
|
let mut wrapped = match row.shrink(cols) {
|
|
|
|
Some(wrapped) if reflow => wrapped,
|
2020-01-09 23:06:41 +00:00
|
|
|
_ => {
|
|
|
|
new_raw.push(row);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Insert spacer if a wide char would be wrapped into the last column.
|
2020-01-09 23:06:41 +00:00
|
|
|
if row.len() >= cols.0 && row[cols - 1].flags().contains(Flags::WIDE_CHAR) {
|
|
|
|
wrapped.insert(0, row[cols - 1]);
|
|
|
|
|
|
|
|
let mut spacer = *template;
|
|
|
|
spacer.flags_mut().insert(Flags::WIDE_CHAR_SPACER);
|
|
|
|
row[cols - 1] = spacer;
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Remove wide char spacer before shrinking.
|
2020-01-09 23:06:41 +00:00
|
|
|
let len = wrapped.len();
|
|
|
|
if (len == 1 || (len >= 2 && !wrapped[len - 2].flags().contains(Flags::WIDE_CHAR)))
|
|
|
|
&& wrapped[len - 1].flags().contains(Flags::WIDE_CHAR_SPACER)
|
|
|
|
{
|
|
|
|
if len == 1 {
|
|
|
|
row[cols - 1].flags_mut().insert(Flags::WRAPLINE);
|
|
|
|
new_raw.push(row);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
wrapped[len - 2].flags_mut().insert(Flags::WRAPLINE);
|
|
|
|
wrapped.truncate(len - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_raw.push(row);
|
2019-03-13 18:55:18 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Set line as wrapped if cells got removed.
|
2019-06-23 23:29:01 +00:00
|
|
|
if let Some(cell) = new_raw.last_mut().and_then(|r| r.last_mut()) {
|
2020-01-09 23:06:41 +00:00
|
|
|
cell.flags_mut().insert(Flags::WRAPLINE);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 23:06:41 +00:00
|
|
|
if wrapped
|
|
|
|
.last()
|
|
|
|
.map(|c| c.flags().contains(Flags::WRAPLINE) && i >= 1)
|
|
|
|
.unwrap_or(false)
|
|
|
|
&& wrapped.len() < cols.0
|
2019-03-13 18:55:18 +00:00
|
|
|
{
|
2020-05-05 22:50:23 +00:00
|
|
|
// Make sure previous wrap flag doesn't linger around.
|
2020-01-09 23:06:41 +00:00
|
|
|
if let Some(cell) = wrapped.last_mut() {
|
|
|
|
cell.flags_mut().remove(Flags::WRAPLINE);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Add removed cells to start of next row.
|
2020-01-09 23:06:41 +00:00
|
|
|
buffered = Some(wrapped);
|
|
|
|
break;
|
2019-03-13 18:55:18 +00:00
|
|
|
} else {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Make sure viewport doesn't move if line is outside of the visible area.
|
2019-03-13 18:55:18 +00:00
|
|
|
if i < self.display_offset {
|
|
|
|
self.display_offset = min(self.display_offset + 1, self.max_scroll_limit);
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Make sure new row is at least as long as new width.
|
2020-01-09 23:06:41 +00:00
|
|
|
let occ = wrapped.len();
|
2019-03-13 18:55:18 +00:00
|
|
|
if occ < cols.0 {
|
2020-01-09 23:06:41 +00:00
|
|
|
wrapped.append(&mut vec![*template; cols.0 - occ]);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
2020-01-09 23:06:41 +00:00
|
|
|
row = Row::from_vec(wrapped, occ);
|
2019-03-13 18:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-29 18:15:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 23:29:01 +00:00
|
|
|
let mut reversed: Vec<Row<T>> = new_raw.drain(..).rev().collect();
|
|
|
|
reversed.truncate(self.max_scroll_limit + self.lines.0);
|
|
|
|
self.raw.replace_inner(reversed);
|
2018-05-29 18:15:03 +00:00
|
|
|
self.cols = cols;
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Remove lines from the visible area.
|
2018-02-16 02:35:49 +00:00
|
|
|
///
|
|
|
|
/// The behavior in Terminal.app and iTerm.app is to keep the cursor at the
|
|
|
|
/// bottom of the screen. This is achieved by pushing history "out the top"
|
|
|
|
/// of the terminal window.
|
|
|
|
///
|
|
|
|
/// Alacritty takes the same approach.
|
2020-03-24 01:29:07 +00:00
|
|
|
fn shrink_lines(&mut self, target: Line, cursor_pos: &mut Point, template: &T) {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Scroll up to keep cursor inside the window.
|
2020-03-24 01:29:07 +00:00
|
|
|
let required_scrolling = (cursor_pos.line + 1).saturating_sub(target.0);
|
|
|
|
if required_scrolling > 0 {
|
|
|
|
self.scroll_up(&(Line(0)..self.lines), Line(required_scrolling), template);
|
|
|
|
}
|
2018-02-16 02:35:49 +00:00
|
|
|
|
2018-03-07 04:57:40 +00:00
|
|
|
self.selection = None;
|
2020-03-24 01:29:07 +00:00
|
|
|
self.raw.rotate((self.lines - target).0 as isize);
|
2018-04-22 12:39:55 +00:00
|
|
|
self.raw.shrink_visible_lines(target);
|
2018-02-16 02:35:49 +00:00
|
|
|
self.lines = target;
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
#[inline]
|
2020-01-09 23:06:41 +00:00
|
|
|
pub fn scroll_down(&mut self, region: &Range<Line>, positions: Line, template: &T) {
|
2020-01-24 22:57:22 +00:00
|
|
|
let num_lines = self.num_lines().0;
|
|
|
|
let num_cols = self.num_cols().0;
|
|
|
|
|
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.
|
2018-01-15 05:51:56 +00:00
|
|
|
if region.start == Line(0) {
|
|
|
|
// 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);
|
2020-01-24 22:57:22 +00:00
|
|
|
self.selection = self
|
|
|
|
.selection
|
|
|
|
.take()
|
|
|
|
.and_then(|s| s.rotate(num_lines, num_cols, region, -(*positions as isize)));
|
2018-01-15 05:51:56 +00:00
|
|
|
|
2018-02-17 02:35:54 +00:00
|
|
|
self.decrease_scroll_limit(*positions);
|
|
|
|
|
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) {
|
2018-04-02 15:44:54 +00:00
|
|
|
self.raw[i].reset(&template);
|
2017-10-13 02:07:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Rotate selection to track content.
|
2020-01-24 22:57:22 +00:00
|
|
|
self.selection = self
|
|
|
|
.selection
|
|
|
|
.take()
|
|
|
|
.and_then(|s| s.rotate(num_lines, num_cols, region, -(*positions as isize)));
|
|
|
|
|
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)) {
|
2018-04-02 15:44:54 +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-01-09 23:06:41 +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;
|
|
|
|
let num_cols = self.num_cols().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));
|
2020-01-24 22:57:22 +00:00
|
|
|
self.selection = self
|
|
|
|
.selection
|
|
|
|
.take()
|
|
|
|
.and_then(|s| s.rotate(num_lines, num_cols, region, *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 {
|
2018-05-19 21:47:16 +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
|
|
|
// Rotate selection to track content.
|
2020-01-24 22:57:22 +00:00
|
|
|
self.selection = self
|
|
|
|
.selection
|
|
|
|
.take()
|
|
|
|
.and_then(|s| s.rotate(num_lines, num_cols, region, *positions as isize));
|
|
|
|
|
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) {
|
2018-04-02 15:44:54 +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
|
|
|
|
2019-11-18 21:15:25 +00:00
|
|
|
pub fn clear_viewport(&mut self, template: &T) {
|
|
|
|
// 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 {
|
|
|
|
self.raw[i].reset(&template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Completely reset the grid state.
|
2019-03-02 21:30:29 +00:00
|
|
|
pub fn reset(&mut self, template: &T) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.display_offset = 0;
|
|
|
|
self.selection = None;
|
|
|
|
}
|
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]
|
2018-12-08 01:50:01 +00:00
|
|
|
pub fn initialize_all(&mut self, template: &T)
|
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|