Fix crash when vi cursor in history during clear

Fixes #5544.
This commit is contained in:
a5ob7r 2021-12-03 16:45:05 +09:00 committed by GitHub
parent 4c6a763850
commit 7e736c00f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 0 deletions

View File

@ -30,6 +30,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Vi mode search starting in the line below the vi cursor
- Invisible cursor with matching foreground/background colors
- Crash when hovering over a match emptied by post-processing
- Crash when the vi cursor is on the scrollback and viewport clear is invoked
- Freeze when the vi cursor is on the scrollback and scrollback clear is invoked
### Removed

View File

@ -376,6 +376,9 @@ impl<T> Grid<T> {
pub fn clear_history(&mut self) {
// Explicitly purge all lines from history.
self.raw.shrink_lines(self.history_size());
// Reset display offset.
self.display_offset = 0;
}
/// This is used only for initializing after loading ref-tests.

View File

@ -1424,6 +1424,9 @@ impl<T: EventListener> Handler for Term<T> {
self.grid.reset_region(..);
} else {
self.grid.clear_viewport();
self.vi_mode_cursor.point.line =
self.vi_mode_cursor.point.line.grid_clamp(self, Boundary::Cursor);
}
self.selection = None;
@ -1431,6 +1434,9 @@ impl<T: EventListener> Handler for Term<T> {
ansi::ClearMode::Saved if self.history_size() > 0 => {
self.grid.clear_history();
self.vi_mode_cursor.point.line =
self.vi_mode_cursor.point.line.grid_clamp(self, Boundary::Cursor);
self.selection = self.selection.take().filter(|s| !s.intersects_range(..Line(0)));
},
// We have no history to clear.
@ -2118,6 +2124,50 @@ mod tests {
assert_eq!(term.grid()[cursor].c, '▒');
}
#[test]
fn clear_viewport_set_vi_cursor_into_viewport() {
let size = SizeInfo::new(10.0, 20.0, 1.0, 1.0, 0.0, 0.0, false);
let mut term = Term::new(&Config::default(), size, ());
// Create 10 lines of scrollback.
for _ in 0..29 {
term.newline();
}
// Change the display area and the vi cursor position.
term.scroll_display(Scroll::Top);
term.vi_mode_cursor.point = Point::new(Line(-5), Column(3));
// Clear the viewport.
term.clear_screen(ansi::ClearMode::All);
assert_eq!(term.grid.display_offset(), 0);
assert_eq!(term.vi_mode_cursor.point.line.0, 0);
assert_eq!(term.vi_mode_cursor.point.column.0, 3);
}
#[test]
fn clear_scrollback_set_vi_cursor_into_viewport() {
let size = SizeInfo::new(10.0, 20.0, 1.0, 1.0, 0.0, 0.0, false);
let mut term = Term::new(&Config::default(), size, ());
// Create 10 lines of scrollback.
for _ in 0..29 {
term.newline();
}
// Change the display area and the vi cursor position.
term.scroll_display(Scroll::Top);
term.vi_mode_cursor.point = Point::new(Line(-5), Column(3));
// Clear the scrollback buffer.
term.clear_screen(ansi::ClearMode::Saved);
assert_eq!(term.grid.display_offset(), 0);
assert_eq!(term.vi_mode_cursor.point.line.0, 0);
assert_eq!(term.vi_mode_cursor.point.column.0, 3);
}
#[test]
fn clear_saved_lines() {
let size = SizeInfo::new(21.0, 51.0, 3.0, 3.0, 0.0, 0.0, false);