2016-06-29 23:56:12 -04:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
//! A specialized 2d grid implementation optimized for use in a terminal.
|
|
|
|
|
2018-02-15 22:06:39 -05:00
|
|
|
use std::cmp::{min, max, Ordering};
|
2018-02-15 21:35:49 -05:00
|
|
|
use std::ops::{Deref, Range, Index, IndexMut, RangeTo, RangeFrom, RangeFull};
|
2016-06-08 13:39:49 -04:00
|
|
|
|
2018-03-06 23:57:40 -05:00
|
|
|
use index::{self, Point, Line, Column, IndexRange};
|
2018-03-05 12:57:34 -05:00
|
|
|
use selection::Selection;
|
2016-07-03 20:00:00 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
mod row;
|
|
|
|
pub use self::row::Row;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2018-01-15 00:51:56 -05:00
|
|
|
mod storage;
|
|
|
|
use self::storage::Storage;
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
/// Bidirection iterator
|
|
|
|
pub trait BidirectionalIterator: Iterator {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item>;
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// An item in the grid along with its Line and Column.
|
2017-02-11 15:49:40 -05:00
|
|
|
pub struct Indexed<T> {
|
2017-10-12 23:01:28 -04:00
|
|
|
pub inner: T,
|
2017-02-11 15:49:40 -05: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 12:57:34 -05:00
|
|
|
impl<T: PartialEq> ::std::cmp::PartialEq for Grid<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.cols.eq(&other.cols) &&
|
|
|
|
self.lines.eq(&other.lines) &&
|
|
|
|
self.raw.eq(&other.raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
/// Represents the terminal display contents
|
2018-03-05 12:57:34 -05:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2016-07-03 20:00:00 -04:00
|
|
|
pub struct Grid<T> {
|
|
|
|
/// Lines in the grid. Each row holds a list of cells corresponding to the
|
|
|
|
/// columns in that row.
|
2018-01-15 00:51:56 -05:00
|
|
|
raw: Storage<Row<T>>,
|
2016-07-03 20:00:00 -04:00
|
|
|
|
|
|
|
/// Number of columns
|
|
|
|
cols: index::Column,
|
|
|
|
|
|
|
|
/// Number of lines.
|
|
|
|
///
|
|
|
|
/// Invariant: lines is equivalent to raw.len()
|
|
|
|
lines: index::Line,
|
2018-01-14 13:17:08 -05:00
|
|
|
|
2018-02-12 00:25:33 -05:00
|
|
|
/// Offset of displayed area
|
|
|
|
///
|
|
|
|
/// 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.
|
2018-03-09 16:49:47 -05:00
|
|
|
#[serde(default)]
|
2018-02-12 00:25:33 -05:00
|
|
|
display_offset: usize,
|
2018-02-15 22:06:39 -05:00
|
|
|
|
|
|
|
/// An limit on how far back it's possible to scroll
|
2018-03-09 16:49:47 -05:00
|
|
|
#[serde(default)]
|
2018-02-15 22:06:39 -05:00
|
|
|
scroll_limit: usize,
|
2018-03-05 12:57:34 -05:00
|
|
|
|
|
|
|
/// Selected region
|
|
|
|
#[serde(skip)]
|
|
|
|
pub selection: Option<Selection>,
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
pub struct GridIterator<'a, T: 'a> {
|
2018-03-06 23:57:40 -05:00
|
|
|
/// Immutable grid reference
|
2017-01-14 20:53:48 -05:00
|
|
|
grid: &'a Grid<T>,
|
2018-03-06 23:57:40 -05:00
|
|
|
|
|
|
|
/// Current position of the iterator within the grid.
|
|
|
|
pub cur: Point<usize>,
|
|
|
|
|
|
|
|
/// Bottom of screen (buffer)
|
|
|
|
bot: usize,
|
|
|
|
|
|
|
|
/// Top of screen (buffer)
|
|
|
|
top: usize,
|
2017-01-14 20:53:48 -05:00
|
|
|
}
|
|
|
|
|
2018-03-11 08:01:06 -04:00
|
|
|
pub enum Scroll {
|
|
|
|
Lines(isize),
|
|
|
|
PageUp,
|
|
|
|
PageDown,
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
}
|
|
|
|
|
2018-01-14 13:17:08 -05:00
|
|
|
impl<T: Copy + Clone> Grid<T> {
|
2018-02-16 20:54:32 -05:00
|
|
|
pub fn new(lines: index::Line, cols: index::Column, scrollback: usize, template: T) -> Grid<T> {
|
|
|
|
let mut raw = Storage::with_capacity(*lines + scrollback, lines);
|
2018-02-12 00:25:33 -05:00
|
|
|
|
|
|
|
// Allocate all lines in the buffer, including scrollback history
|
|
|
|
//
|
|
|
|
// TODO (jwilm) Allocating each line at this point is expensive and
|
|
|
|
// delays startup. A nice solution might be having `Row` delay
|
|
|
|
// allocation until it's actually used.
|
|
|
|
for _ in 0..raw.capacity() {
|
2018-04-02 11:44:54 -04:00
|
|
|
raw.push(Row::new(cols, &template));
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Grid {
|
2018-03-04 17:40:15 -05:00
|
|
|
raw,
|
|
|
|
cols,
|
|
|
|
lines,
|
2018-02-15 21:35:49 -05:00
|
|
|
display_offset: 0,
|
2018-02-15 22:06:39 -05:00
|
|
|
scroll_limit: 0,
|
2018-03-05 12:57:34 -05:00
|
|
|
selection: None,
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
|
2018-03-06 23:57:40 -05:00
|
|
|
pub fn visible_to_buffer(&self, point: Point) -> Point<usize> {
|
|
|
|
Point {
|
|
|
|
line: self.visible_line_to_buffer(point.line),
|
|
|
|
col: point.col
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_to_visible(&self, point: Point<usize>) -> Point {
|
|
|
|
Point {
|
2018-03-10 08:53:54 -05:00
|
|
|
line: self.buffer_line_to_visible(point.line).expect("Line not visible"),
|
2018-03-06 23:57:40 -05:00
|
|
|
col: point.col
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:53:54 -05:00
|
|
|
pub fn buffer_line_to_visible(&self, line: usize) -> Option<Line> {
|
|
|
|
if line >= self.display_offset {
|
|
|
|
self.offset_to_line(line - self.display_offset)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-03-06 23:57:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn visible_line_to_buffer(&self, line: Line) -> usize {
|
|
|
|
self.line_to_offset(line) + self.display_offset
|
|
|
|
}
|
|
|
|
|
2018-03-11 08:01:06 -04: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,
|
|
|
|
self.scroll_limit
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Scroll::PageUp => {
|
|
|
|
self.display_offset = min(
|
|
|
|
self.display_offset + self.lines.0,
|
|
|
|
self.scroll_limit
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Scroll::PageDown => {
|
|
|
|
self.display_offset -= min(
|
|
|
|
self.display_offset,
|
|
|
|
self.lines.0
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Scroll::Top => self.display_offset = self.scroll_limit,
|
|
|
|
Scroll::Bottom => self.display_offset = 0,
|
2018-03-10 14:24:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 11:44:54 -04:00
|
|
|
pub fn resize(
|
|
|
|
&mut self,
|
|
|
|
lines: index::Line,
|
|
|
|
cols: index::Column,
|
|
|
|
template: &T,
|
|
|
|
) {
|
2016-06-29 13:21:02 -04:00
|
|
|
// Check that there's actually work to do and return early if not
|
2016-07-03 20:00:00 -04:00
|
|
|
if lines == self.lines && cols == self.cols {
|
2016-06-29 13:21:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
match self.lines.cmp(&lines) {
|
2018-04-02 11:44:54 -04:00
|
|
|
Ordering::Less => self.grow_lines(lines, template),
|
2016-07-03 20:00:00 -04:00
|
|
|
Ordering::Greater => self.shrink_lines(lines),
|
2016-06-29 13:21:02 -04:00
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.cols.cmp(&cols) {
|
2018-04-02 11:44:54 -04:00
|
|
|
Ordering::Less => self.grow_cols(cols, template),
|
2016-06-29 13:21:02 -04:00
|
|
|
Ordering::Greater => self.shrink_cols(cols),
|
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:06:39 -05:00
|
|
|
fn increase_scroll_limit(&mut self, count: usize) {
|
|
|
|
self.scroll_limit = min(self.scroll_limit + count, self.raw.len() - *self.lines);
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:35:54 -05:00
|
|
|
fn decrease_scroll_limit(&mut self, count: usize) {
|
|
|
|
self.scroll_limit = self.scroll_limit.saturating_sub(count);
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
/// Add lines to the visible area
|
|
|
|
///
|
|
|
|
/// The behavior in Terminal.app and iTerm.app is to keep the cursor at the
|
|
|
|
/// bottom of the screen as long as there is scrollback available. Once
|
|
|
|
/// scrollback is exhausted, new lines are simply added to the bottom of the
|
|
|
|
/// screen.
|
|
|
|
///
|
|
|
|
/// Alacritty takes a different approach. Rather than trying to move with
|
|
|
|
/// the scrollback, we simply pull additional lines from the back of the
|
|
|
|
/// buffer in order to populate the new area.
|
2018-04-02 11:44:54 -04:00
|
|
|
fn grow_lines(
|
|
|
|
&mut self,
|
|
|
|
new_line_count: index::Line,
|
|
|
|
template: &T,
|
|
|
|
) {
|
2018-02-15 22:06:39 -05:00
|
|
|
let previous_scroll_limit = self.scroll_limit;
|
|
|
|
let lines_added = new_line_count - self.lines;
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2018-02-15 22:06:39 -05:00
|
|
|
// Need to "resize" before updating buffer
|
|
|
|
self.raw.set_visible_lines(new_line_count);
|
|
|
|
self.lines = new_line_count;
|
|
|
|
|
|
|
|
// Add new lines to bottom
|
2018-04-02 11:44:54 -04:00
|
|
|
self.scroll_up(&(Line(0)..new_line_count), lines_added, template);
|
2018-02-15 21:35:49 -05:00
|
|
|
|
2018-02-15 22:06:39 -05:00
|
|
|
self.scroll_limit = self.scroll_limit.saturating_sub(*lines_added);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2018-04-02 11:44:54 -04:00
|
|
|
fn grow_cols(&mut self, cols: index::Column, template: &T) {
|
2017-10-12 23:29:35 -04:00
|
|
|
for row in self.raw.iter_mut() {
|
2018-04-02 11:44:54 -04:00
|
|
|
row.grow(cols, template);
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2018-01-14 13:17:08 -05:00
|
|
|
// Update self cols
|
2016-07-03 20:00:00 -04:00
|
|
|
self.cols = cols;
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
/// Remove lines from the visible area
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
fn shrink_lines(&mut self, target: index::Line) {
|
|
|
|
// TODO handle disabled scrollback
|
|
|
|
// while index::Line(self.raw.len()) != lines {
|
|
|
|
// self.raw.pop();
|
|
|
|
// }
|
|
|
|
|
|
|
|
let prev = self.lines;
|
|
|
|
|
2018-03-06 23:57:40 -05:00
|
|
|
self.selection = None;
|
2018-02-15 21:35:49 -05:00
|
|
|
self.raw.rotate(*prev as isize - *target as isize);
|
|
|
|
self.raw.set_visible_lines(target);
|
|
|
|
self.lines = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a Line index (active region) to a buffer offset
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This method will panic if `Line` is larger than the grid dimensions
|
|
|
|
pub fn line_to_offset(&self, line: index::Line) -> usize {
|
|
|
|
assert!(line < self.num_lines());
|
|
|
|
|
|
|
|
*(self.num_lines() - line - 1)
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:53:54 -05:00
|
|
|
pub fn offset_to_line(&self, offset: usize) -> Option<Line> {
|
|
|
|
if offset < *self.num_lines() {
|
|
|
|
Some(self.lines - offset - 1)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-03-06 23:57:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
#[inline]
|
2018-04-02 11:44:54 -04:00
|
|
|
pub fn scroll_down(
|
|
|
|
&mut self,
|
|
|
|
region: &Range<index::Line>,
|
|
|
|
positions: index::Line,
|
|
|
|
template: &T,
|
|
|
|
) {
|
2018-01-15 00:51:56 -05: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.
|
|
|
|
//
|
|
|
|
// To accomodate scroll regions, rows are reordered at the end.
|
|
|
|
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-15 21:35:49 -05:00
|
|
|
self.raw.rotate_up(*positions);
|
2018-03-06 23:57:40 -05:00
|
|
|
if let Some(ref mut selection) = self.selection {
|
|
|
|
selection.rotate(-(*positions as isize));
|
|
|
|
}
|
2018-01-15 00:51:56 -05:00
|
|
|
|
2018-02-16 21:35:54 -05:00
|
|
|
self.decrease_scroll_limit(*positions);
|
|
|
|
|
2018-01-15 00:51:56 -05:00
|
|
|
// Now, restore any scroll region lines
|
2018-02-15 22:34:23 -05:00
|
|
|
let lines = self.lines;
|
|
|
|
for i in IndexRange(region.end .. lines) {
|
2018-02-15 21:35:49 -05:00
|
|
|
self.raw.swap_lines(i, i + positions);
|
2018-01-15 00:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, reset recycled lines
|
2018-02-16 21:35:54 -05:00
|
|
|
for i in IndexRange(Line(0)..positions) {
|
2018-04-02 11:44:54 -04:00
|
|
|
self.raw[i].reset(&template);
|
2017-10-12 22:07:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Subregion rotation
|
|
|
|
for line in IndexRange((region.start + positions)..region.end).rev() {
|
2018-02-15 21:35:49 -05:00
|
|
|
self.raw.swap_lines(line, line - positions);
|
2017-10-12 22:07:49 -04:00
|
|
|
}
|
2018-01-14 13:17:08 -05:00
|
|
|
|
2018-02-11 13:07:33 -05:00
|
|
|
for line in IndexRange(region.start .. (region.start + positions)) {
|
2018-04-02 11:44:54 -04:00
|
|
|
self.raw[line].reset(&template);
|
2018-01-14 13:17:08 -05:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 13:07:33 -05:00
|
|
|
/// scroll_up moves lines at the bottom towards the top
|
|
|
|
///
|
|
|
|
/// This is the performance-sensitive part of scrolling.
|
2016-08-22 11:37:50 -04:00
|
|
|
#[inline]
|
2018-04-02 11:44:54 -04:00
|
|
|
pub fn scroll_up(
|
|
|
|
&mut self,
|
|
|
|
region: &Range<index::Line>,
|
|
|
|
positions: index::Line,
|
|
|
|
template: &T
|
|
|
|
) {
|
2018-01-15 00:51:56 -05:00
|
|
|
if region.start == Line(0) {
|
2018-02-12 00:25:33 -05:00
|
|
|
// Update display offset when not pinned to active area
|
|
|
|
if self.display_offset != 0 {
|
|
|
|
self.display_offset += *positions;
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:06:39 -05:00
|
|
|
self.increase_scroll_limit(*positions);
|
|
|
|
|
2018-01-15 00:51:56 -05:00
|
|
|
// Rotate the entire line buffer. If there's a scrolling region
|
|
|
|
// active, the bottom lines are restored in the next step.
|
2018-02-15 21:35:49 -05:00
|
|
|
self.raw.rotate(-(*positions as isize));
|
2018-03-06 23:57:40 -05:00
|
|
|
if let Some(ref mut selection) = self.selection {
|
|
|
|
selection.rotate(*positions as isize);
|
|
|
|
}
|
2018-01-15 00:51:56 -05:00
|
|
|
|
|
|
|
// Now, restore any lines outside the scroll region
|
2018-02-11 13:07:33 -05:00
|
|
|
for idx in (*region.end .. *self.num_lines()).rev() {
|
2018-01-15 00:51:56 -05:00
|
|
|
// First do the swap
|
2018-02-15 21:35:49 -05:00
|
|
|
self.raw.swap_lines(Line(idx), Line(idx) - positions);
|
2018-01-15 00:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, reset recycled lines
|
|
|
|
//
|
|
|
|
// Recycled lines are just above the end of the scrolling region.
|
|
|
|
for i in 0..*positions {
|
2018-04-02 11:44:54 -04:00
|
|
|
self.raw[region.end - i - 1].reset(&template);
|
2017-10-12 22:07:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Subregion rotation
|
|
|
|
for line in IndexRange(region.start..(region.end - positions)) {
|
2018-02-15 21:35:49 -05:00
|
|
|
self.raw.swap_lines(line, line + positions);
|
2017-10-12 22:07:49 -04:00
|
|
|
}
|
2018-01-14 13:17:08 -05:00
|
|
|
|
|
|
|
// Clear reused lines
|
2018-02-11 13:07:33 -05:00
|
|
|
for line in IndexRange((region.end - positions) .. region.end) {
|
2018-04-02 11:44:54 -04:00
|
|
|
self.raw[line].reset(&template);
|
2018-01-14 13:17:08 -05:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 13:17:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Grid<T> {
|
|
|
|
#[inline]
|
|
|
|
pub fn num_lines(&self) -> index::Line {
|
|
|
|
self.lines
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
pub fn display_iter(&self) -> DisplayIter<T> {
|
|
|
|
DisplayIter::new(self)
|
|
|
|
}
|
|
|
|
|
2018-01-14 13:17:08 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn num_cols(&self) -> index::Column {
|
|
|
|
self.cols
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2018-03-06 23:57:40 -05:00
|
|
|
pub fn iter_from(&self, point: Point<usize>) -> GridIterator<T> {
|
2017-01-14 20:53:48 -05:00
|
|
|
GridIterator {
|
|
|
|
grid: self,
|
|
|
|
cur: point,
|
2018-03-06 23:57:40 -05:00
|
|
|
bot: self.display_offset,
|
|
|
|
top: self.display_offset + *self.num_lines() - 1,
|
2017-01-14 20:53:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:16:58 -04:00
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
pub fn contains(&self, point: &Point) -> bool {
|
|
|
|
self.lines > point.line && self.cols > point.col
|
2016-09-16 20:12:53 -04:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
// /// Swap two lines in the grid
|
|
|
|
// ///
|
|
|
|
// /// This could have used slice::swap internally, but we are able to have
|
|
|
|
// /// better error messages by doing the bounds checking ourselves.
|
|
|
|
// #[inline]
|
|
|
|
// pub fn swap_lines(&mut self, src: index::Line, dst: index::Line) {
|
|
|
|
// self.raw.swap(*src, *dst);
|
|
|
|
// }
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn shrink_cols(&mut self, cols: index::Column) {
|
2017-10-12 23:29:35 -04:00
|
|
|
for row in self.raw.iter_mut() {
|
2016-06-29 13:21:02 -04:00
|
|
|
row.shrink(cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cols = cols;
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
impl<'a, T> Iterator for GridIterator<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let last_col = self.grid.num_cols() - Column(1);
|
|
|
|
match self.cur {
|
2018-03-06 23:57:40 -05:00
|
|
|
Point { line, col } if (line == self.bot) && (col == last_col) => None,
|
2017-01-14 20:53:48 -05:00
|
|
|
Point { col, .. } if
|
|
|
|
(col == last_col) => {
|
2018-03-06 23:57:40 -05:00
|
|
|
self.cur.line -= 1;
|
2017-01-14 20:53:48 -05:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> BidirectionalIterator for GridIterator<'a, T> {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item> {
|
|
|
|
let num_cols = self.grid.num_cols();
|
|
|
|
|
|
|
|
match self.cur {
|
2018-03-06 23:57:40 -05:00
|
|
|
Point { line, col: Column(0) } if line == self.top => None,
|
2017-01-14 20:53:48 -05:00
|
|
|
Point { col: Column(0), .. } => {
|
2018-03-06 23:57:40 -05:00
|
|
|
self.cur.line += 1;
|
2017-01-14 20:53:48 -05:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
/// Index active region by line
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Index<index::Line> for Grid<T> {
|
|
|
|
type Output = Row<T>;
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index(&self, index: index::Line) -> &Row<T> {
|
2018-02-15 21:35:49 -05:00
|
|
|
&self.raw[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Index with buffer offset
|
|
|
|
impl<T> Index<usize> for Grid<T> {
|
|
|
|
type Output = Row<T>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &Row<T> {
|
2018-02-12 00:25:33 -05:00
|
|
|
&self.raw[index]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> IndexMut<index::Line> for Grid<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index_mut(&mut self, index: index::Line) -> &mut Row<T> {
|
2018-02-12 00:25:33 -05:00
|
|
|
&mut self.raw[index]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:09:29 -05:00
|
|
|
impl<'point, T> Index<&'point Point> for Grid<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
type Output = T;
|
2016-05-30 23:44:37 -04:00
|
|
|
|
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
fn index<'a>(&'a self, point: &Point) -> &'a T {
|
2018-02-12 00:25:33 -05:00
|
|
|
&self[point.line][point.col]
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:09:29 -05:00
|
|
|
impl<'point, T> IndexMut<&'point Point> for Grid<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
fn index_mut<'a, 'b>(&'a mut self, point: &'b Point) -> &'a mut T {
|
2018-02-12 00:25:33 -05:00
|
|
|
&mut self[point.line][point.col]
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:35:49 -05:00
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
// REGIONS
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// A subset of lines in the grid
|
|
|
|
///
|
|
|
|
/// May be constructed using Grid::region(..)
|
|
|
|
pub struct Region<'a, T: 'a> {
|
|
|
|
start: Line,
|
|
|
|
end: Line,
|
2018-01-15 00:51:56 -05:00
|
|
|
raw: &'a Storage<Row<T>>,
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// A mutable subset of lines in the grid
|
|
|
|
///
|
|
|
|
/// May be constructed using Grid::region_mut(..)
|
|
|
|
pub struct RegionMut<'a, T: 'a> {
|
|
|
|
start: Line,
|
|
|
|
end: Line,
|
2018-01-15 00:51:56 -05:00
|
|
|
raw: &'a mut Storage<Row<T>>,
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:12:29 -04:00
|
|
|
impl<'a, T> RegionMut<'a, T> {
|
|
|
|
/// Call the provided function for every item in this region
|
|
|
|
pub fn each<F: Fn(&mut T)>(self, func: F) {
|
|
|
|
for row in self {
|
|
|
|
for item in row {
|
|
|
|
func(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
pub trait IndexRegion<I, T> {
|
|
|
|
/// Get an immutable region of Self
|
|
|
|
fn region<'a>(&'a self, _: I) -> Region<'a, T>;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// Get a mutable region of Self
|
|
|
|
fn region_mut<'a>(&'a mut self, _: I) -> RegionMut<'a, T>;
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<T> IndexRegion<Range<Line>, T> for Grid<T> {
|
|
|
|
fn region(&self, index: Range<Line>) -> Region<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
assert!(index.start <= index.end);
|
|
|
|
Region {
|
|
|
|
start: index.start,
|
|
|
|
end: index.end,
|
|
|
|
raw: &self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
fn region_mut(&mut self, index: Range<Line>) -> RegionMut<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
assert!(index.start <= index.end);
|
|
|
|
RegionMut {
|
|
|
|
start: index.start,
|
|
|
|
end: index.end,
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<T> IndexRegion<RangeTo<Line>, T> for Grid<T> {
|
|
|
|
fn region(&self, index: RangeTo<Line>) -> Region<T> {
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
Region {
|
|
|
|
start: Line(0),
|
|
|
|
end: index.end,
|
|
|
|
raw: &self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<T> {
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
RegionMut {
|
|
|
|
start: Line(0),
|
|
|
|
end: index.end,
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> {
|
|
|
|
fn region(&self, index: RangeFrom<Line>) -> Region<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
Region {
|
|
|
|
start: index.start,
|
|
|
|
end: self.num_lines(),
|
|
|
|
raw: &self.raw
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
|
|
|
fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
RegionMut {
|
|
|
|
start: index.start,
|
|
|
|
end: self.num_lines(),
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:12:29 -04:00
|
|
|
impl<T> IndexRegion<RangeFull, T> for Grid<T> {
|
|
|
|
fn region(&self, _: RangeFull) -> Region<T> {
|
|
|
|
Region {
|
|
|
|
start: Line(0),
|
|
|
|
end: self.num_lines(),
|
|
|
|
raw: &self.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn region_mut(&mut self, _: RangeFull) -> RegionMut<T> {
|
|
|
|
RegionMut {
|
|
|
|
start: Line(0),
|
|
|
|
end: self.num_lines(),
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
pub struct RegionIter<'a, T: 'a> {
|
|
|
|
end: Line,
|
|
|
|
cur: Line,
|
2018-01-15 00:51:56 -05:00
|
|
|
raw: &'a Storage<Row<T>>,
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
pub struct RegionIterMut<'a, T: 'a> {
|
|
|
|
end: Line,
|
|
|
|
cur: Line,
|
2018-01-15 00:51:56 -05:00
|
|
|
raw: &'a mut Storage<Row<T>>,
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> IntoIterator for Region<'a, T> {
|
|
|
|
type Item = &'a Row<T>;
|
|
|
|
type IntoIter = RegionIter<'a, T>;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
RegionIter {
|
|
|
|
end: self.end,
|
|
|
|
cur: self.start,
|
|
|
|
raw: self.raw
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> IntoIterator for RegionMut<'a, T> {
|
|
|
|
type Item = &'a mut Row<T>;
|
|
|
|
type IntoIter = RegionIterMut<'a, T>;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
RegionIterMut {
|
|
|
|
end: self.end,
|
|
|
|
cur: self.start,
|
|
|
|
raw: self.raw
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> Iterator for RegionIter<'a, T> {
|
|
|
|
type Item = &'a Row<T>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.cur < self.end {
|
|
|
|
let index = self.cur;
|
|
|
|
self.cur += 1;
|
2018-02-15 21:35:49 -05:00
|
|
|
Some(&self.raw[index])
|
2017-10-12 23:01:28 -04:00
|
|
|
} else {
|
|
|
|
None
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2017-01-14 20:53:48 -05:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> Iterator for RegionIterMut<'a, T> {
|
|
|
|
type Item = &'a mut Row<T>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.cur < self.end {
|
|
|
|
let index = self.cur;
|
|
|
|
self.cur += 1;
|
|
|
|
unsafe {
|
2018-02-15 21:35:49 -05:00
|
|
|
Some(&mut *(&mut self.raw[index] as *mut _))
|
2017-01-14 20:53:48 -05:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
} else {
|
|
|
|
None
|
2017-01-14 20:53:48 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2018-02-15 21:35:49 -05:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
// DISPLAY ITERATOR
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Iterates over the visible area accounting for buffer transform
|
|
|
|
pub struct DisplayIter<'a, T: 'a> {
|
|
|
|
grid: &'a Grid<T>,
|
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
|
|
|
col: Column,
|
2018-02-15 22:34:09 -05:00
|
|
|
line: Line,
|
2018-02-15 21:35:49 -05: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;
|
|
|
|
let limit = grid.display_offset;
|
|
|
|
let col = Column(0);
|
2018-02-15 22:34:09 -05:00
|
|
|
let line = Line(0);
|
2018-02-15 21:35:49 -05:00
|
|
|
|
2018-02-15 22:34:09 -05:00
|
|
|
DisplayIter { grid, offset, col, limit, line }
|
2018-02-15 21:35:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn offset(&self) -> usize {
|
|
|
|
self.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn column(&self) -> Column {
|
|
|
|
self.col
|
|
|
|
}
|
2018-02-15 22:34:09 -05:00
|
|
|
|
|
|
|
pub fn line(&self) -> Line {
|
|
|
|
self.line
|
|
|
|
}
|
2018-02-15 21:35:49 -05: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-02 15:12:04 -04:00
|
|
|
// Check if the end of the line was reached
|
2018-03-24 09:42:05 -04:00
|
|
|
let next_line = self.col == self.grid.num_cols();
|
|
|
|
if next_line {
|
2018-04-02 15:12:04 -04:00
|
|
|
// Return `None` if we've reached the end of the last line
|
2018-02-15 21:35:49 -05:00
|
|
|
if self.offset == self.limit {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:12:04 -04:00
|
|
|
// Switch to the next line
|
2018-02-15 21:35:49 -05:00
|
|
|
self.col = Column(0);
|
|
|
|
self.offset -= 1;
|
2018-02-15 22:34:09 -05:00
|
|
|
self.line = Line(*self.grid.lines - 1 - (self.offset - self.limit));
|
2018-02-15 21:35:49 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 15:12:04 -04:00
|
|
|
// Return the next item
|
2018-02-15 21:35:49 -05:00
|
|
|
let item = Some(Indexed {
|
|
|
|
inner: self.grid.raw[self.offset][self.col],
|
2018-02-15 22:34:09 -05:00
|
|
|
line: self.line,
|
2018-02-15 21:35:49 -05:00
|
|
|
column: self.col
|
|
|
|
});
|
|
|
|
|
2018-04-02 15:12:04 -04:00
|
|
|
// Only increment column if the line hasn't changed
|
2018-03-24 09:42:05 -04:00
|
|
|
if !next_line {
|
|
|
|
self.col += 1;
|
|
|
|
}
|
2018-02-15 21:35:49 -05:00
|
|
|
item
|
|
|
|
}
|
|
|
|
}
|