diff --git a/term/src/config.rs b/term/src/config.rs index d31b49316..5c2db2c7b 100644 --- a/term/src/config.rs +++ b/term/src/config.rs @@ -40,4 +40,32 @@ pub trait TerminalConfiguration: std::fmt::Debug { /// color palette for a terminal instance at runtime, but this method /// defines the initial palette. fn color_palette(&self) -> ColorPalette; + + /// Return true if a resize operation should consider rows that have + /// made it to scrollback as being immutable. + /// When immutable, the resize operation will pad out the screen height + /// with additional blank rows and due to implementation details means + /// that the user will need to scroll back the scrollbar post-resize + /// than they would otherwise. + /// + /// When mutable, resizing the window taller won't add extra rows; + /// instead the resize will tend to have "bottom gravity" meaning that + /// making the window taller will reveal more history than in the other + /// mode. + /// + /// mutable is generally speaking a nicer experience. + /// + /// On Windows, the PTY layer doesn't play well with a mutable scrollback, + /// frequently moving the cursor up to high and erasing portions of the + /// screen. + /// + /// This behavior only happens with the windows pty layer; it doesn't + /// manifest when using eg: ssh directly to a remote unix system. + /// + /// Ideally we'd have this return `true` only for the native windows + /// pty layer, but for the sake of simplicity, we make this conditional + /// on being a windows build. + fn resize_preserves_scrollback(&self) -> bool { + cfg!(windows) + } } diff --git a/term/src/screen.rs b/term/src/screen.rs index ca2e907f9..3ce128868 100644 --- a/term/src/screen.rs +++ b/term/src/screen.rs @@ -195,12 +195,34 @@ impl Screen { 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); + let new_cursor_y; + + if self.config.resize_preserves_scrollback() { + new_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(new_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)); + } + } else { + // 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. + 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;