Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic for reflowing cursor when growing columns, after shrinking columns #7873

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions alacritty_terminal/src/grid/resize.rs
Expand Up @@ -167,6 +167,14 @@ impl<T: GridCell + Default + PartialEq + Clone> Grid<T> {
let cursor_buffer_line = self.lines - self.cursor.point.line.0 as usize - 1;

if i == cursor_buffer_line && reflow {
if row.is_clear() {
// Rotate cursor down, if the line can be completely moved up (into history).
// This allows us to correctly complete the subtraction of num_wrapped below
// (and avoid hitting the Cursor boundary at (0, 0)
// incorrectly).
self.cursor.point.line += 1;
}

// Resize cursor's line and reflow the cursor if necessary.
let mut target = self.cursor.point.sub(self, Boundary::Cursor, num_wrapped);

Expand All @@ -182,6 +190,9 @@ impl<T: GridCell + Default + PartialEq + Clone> Grid<T> {
let line_delta = self.cursor.point.line - target.line;

if line_delta != 0 && row.is_clear() {
// We move the cursor up a line, if the current row is being entirely reflowed
// and removed.
self.cursor.point.line -= line_delta;
continue;
}

Expand Down
44 changes: 44 additions & 0 deletions alacritty_terminal/src/grid/tests.rs
Expand Up @@ -2,6 +2,7 @@

use super::*;

use crate::index;
use crate::term::cell::Cell;

impl GridCell for usize {
Expand Down Expand Up @@ -214,6 +215,49 @@ fn shrink_reflow_twice() {
assert_eq!(grid[Line(0)][Column(1)], Cell::default());
}

/// Tests shrinking the Grid and then growing it back to its original size, to confirm we
/// adjust the cursor appropriately.
#[test]
fn shrink_grow_reflow_cursor_position() {
// Create a Grid with 3 rows and 8 columns.
let mut grid = Grid::<Cell>::new(3, 8, 2);
grid[Line(0)][Column(0)] = cell('1');
grid[Line(0)][Column(1)] = cell('2');
grid[Line(0)][Column(2)] = cell('3');
grid[Line(0)][Column(3)] = cell('4');
grid[Line(0)][Column(4)] = cell('5');

// Set the cursor position to (0, 5). Note that NO rows are in scrollback currently.
grid.cursor.point.line = index::Line(0);
grid.cursor.point.column = index::Column(5);

// Confirm the cursor position and scrollback size is correct.
assert_eq!(grid.cursor.point.line, index::Line(0));
assert_eq!(grid.cursor.point.column, index::Column(5));
assert_eq!(grid.history_size(), 0);

// Resize the Grid to have 3 columns, instead of 8 columns. Results in a shrink_columns call.
grid.resize(true, 3, 3);

// Note that 1 row is now in scrollback history. Hence, the cursor should be at (0, 2), since
// it's in the "visible" coordinate system.
assert_eq!(grid.cursor.point.line, index::Line(0));
assert_eq!(grid.cursor.point.column, index::Column(2));
// Scrollback history should have 1 row.
assert_eq!(grid.history_size(), 1);

// Resize the Grid back to its original size of 3 rows and 8 columns. Results in a
// grow_columns call.
grid.resize(true, 3, 8);

// We expect the cursor to be back at (0, 5), as we grew the Grid back to its original size.
assert_eq!(grid.cursor.point.line, index::Line(0));
assert_eq!(grid.cursor.point.column, index::Column(5));
// We expect the scrollback history to be empty since we grew the Grid back to its original size
// and removed the row that was in scrollback history (it's "visible" once again).
assert_eq!(grid.history_size(), 0);
}

#[test]
fn shrink_reflow_empty_cell_inside_line() {
let mut grid = Grid::<Cell>::new(1, 5, 3);
Expand Down