From 75ce0fc822529626dafae8df4f62beb51d05bbeb Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 28 Oct 2020 22:50:50 -0700 Subject: [PATCH] term: improve cursor/viewport positioning on resize This seems to make our resize behavior a bit nicer and more consistent with eg: xterm. Previously we'd consider the previously existing scrollback as immutable and prefer to add excess blank lines of padding when making the window taller. In practice that meant that content would scroll back when making the window taller, which is annoying. This commit removes that constraint and instead will prefer to maintain the cursor position relative to the top of the viewport when the size changes. refs: #138 --- term/src/screen.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/term/src/screen.rs b/term/src/screen.rs index 3a7be8f8e..ca2e907f9 100644 --- a/term/src/screen.rs +++ b/term/src/screen.rs @@ -195,28 +195,18 @@ impl Screen { self.lines.push_back(Line::with_width(physical_cols)); } - let vis_cursor_y = cursor - .y - .saturating_add(cursor_y as i64) - .saturating_sub(cursor_phys as i64) - .max(0); - - // We need to ensure that the bottom of the screen has sufficient lines; - // we use simple subtraction of physical_rows from the bottom of the lines - // array to define the visible region. Our resize operation may have - // temporarily violated that, which can result in the cursor unintentionally - // moving up into the scrollback and damaging the output - let required_num_rows_after_cursor = physical_rows.saturating_sub(vis_cursor_y as usize); - let actual_num_rows_after_cursor = self.lines.len().saturating_sub(cursor_y); - for _ in actual_num_rows_after_cursor..required_num_rows_after_cursor { - self.lines.push_back(Line::with_width(physical_cols)); - } + // Compute the new cursor location; this is logically the inverse + // of the phys_row() function, but given the revised cursor_y + // (the rewrap adjusted physical row of the cursor). This + // computes its new VisibleRowIndex given the new viewport size. + let new_cursor_y = cursor_y as VisibleRowIndex + - (self.lines.len() as VisibleRowIndex - physical_rows as VisibleRowIndex); self.physical_rows = physical_rows; self.physical_cols = physical_cols; CursorPosition { x: cursor_x, - y: vis_cursor_y, + y: new_cursor_y, shape: cursor.shape, visibility: cursor.visibility, }