From 44359ce5f1d4f13df5426175a5cbff45448c0c08 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 9 Nov 2019 12:34:39 -0500 Subject: optionally save scrollback rows --- src/grid.rs | 23 +++++++++++++++++++++-- src/parser.rs | 10 +++++----- src/screen.rs | 11 +++++++---- 3 files changed, 33 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/grid.rs b/src/grid.rs index 287e5e4..5a53c6e 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -12,10 +12,12 @@ pub struct Grid { scroll_bottom: u16, origin_mode: bool, saved_origin_mode: bool, + scrollback: std::collections::VecDeque<(usize, crate::row::Row)>, + scrollback_len: usize, } impl Grid { - pub fn new(size: Size) -> Self { + pub fn new(size: Size, scrollback_len: usize) -> Self { Self { size, pos: Pos::default(), @@ -25,6 +27,8 @@ impl Grid { scroll_bottom: size.rows - 1, origin_mode: false, saved_origin_mode: false, + scrollback: std::collections::VecDeque::new(), + scrollback_len, } } @@ -127,6 +131,10 @@ impl Grid { .expect("cursor not pointing to a cell") } + pub fn scrollback_len(&self) -> usize { + self.scrollback_len + } + pub fn write_contents(&self, contents: &mut String) { for row in self.rows() { row.write_contents(contents, 0, self.size.cols); @@ -309,7 +317,14 @@ impl Grid { for _ in 0..(count.min(self.size.rows - self.scroll_top)) { self.rows .insert(self.scroll_bottom as usize + 1, self.new_row()); - self.rows.remove(self.scroll_top as usize); + let removed = self.rows.remove(self.scroll_top as usize); + if self.scrollback_len > 0 && !self.scroll_region_active() { + let idx = self.scrollback.back().map_or(0, |r| r.0 + 1); + self.scrollback.push_back((idx, removed)); + while self.scrollback.len() > self.scrollback_len { + self.scrollback.pop_front(); + } + } } } @@ -337,6 +352,10 @@ impl Grid { self.pos.row >= self.scroll_top && self.pos.row <= self.scroll_bottom } + fn scroll_region_active(&self) -> bool { + self.scroll_top != 0 || self.scroll_bottom != self.size.rows - 1 + } + pub fn set_origin_mode(&mut self, mode: bool) { self.origin_mode = mode; self.set_pos(Pos { row: 0, col: 0 }) diff --git a/src/parser.rs b/src/parser.rs index 77373bd..0cabb1a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -7,13 +7,13 @@ pub struct Parser { impl Parser { /// Creates a new terminal parser of the given size. - pub fn new(rows: u16, cols: u16) -> Self { + pub fn new(rows: u16, cols: u16, scrollback_len: usize) -> Self { Self { parser: vte::Parser::new(), - screen: crate::screen::Screen::new(crate::grid::Size { - rows, - cols, - }), + screen: crate::screen::Screen::new( + crate::grid::Size { rows, cols }, + scrollback_len, + ), } } diff --git a/src/screen.rs b/src/screen.rs index 05b8935..f3fdf6a 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -91,10 +91,13 @@ pub struct Screen { } impl Screen { - pub(crate) fn new(size: crate::grid::Size) -> Self { + pub(crate) fn new( + size: crate::grid::Size, + scrollback_len: usize, + ) -> Self { Self { - grid: crate::grid::Grid::new(size), - alternate_grid: crate::grid::Grid::new(size), + grid: crate::grid::Grid::new(size, scrollback_len), + alternate_grid: crate::grid::Grid::new(size, 0), attrs: crate::attrs::Attrs::default(), saved_attrs: crate::attrs::Attrs::default(), @@ -551,7 +554,7 @@ impl Screen { let title = self.title.clone(); let icon_name = self.icon_name.clone(); - *self = Self::new(self.grid().size()); + *self = Self::new(self.grid.size(), self.grid.scrollback_len()); self.outputs = outputs; self.title = title; -- cgit v1.2.3-54-g00ecf