Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

term: anchor the alt screen to its top when it shrinks

RoversXfea50cbab0a1
diff --git a/term/src/screen.rs b/term/src/screen.rs
index fd71fc73..a9ff9f37 100644
--- a/term/src/screen.rs
+++ b/term/src/screen.rs
@@ -263,6 +263,22 @@ impl Screen {
self.lines.push_back(Line::new(seqno));
}
+ // The alt screen has no scrollback: it is a fixed grid that
+ // full-screen apps redraw in place. The shared "visible region is
+ // the last N lines" rule below gives a shrink bottom gravity,
+ // which scrolls the whole grid up until the app's SIGWINCH redraw
+ // lands. Dragging a split can take many rows between two redraws,
+ // so the top of the pane briefly shows rows from the middle of
+ // the old grid — visible as flicker in any TUI. Anchor the alt
+ // screen to its top instead: crop the bottom and clamp the cursor
+ // into the window; the app repaints the lot on SIGWINCH anyway.
+ let cursor_y = if !self.allow_scrollback && self.lines.len() > physical_rows {
+ self.lines.truncate(physical_rows);
+ cursor_y.min(physical_rows.saturating_sub(1))
+ } else {
+ cursor_y
+ };
+
let new_cursor_y;
// true if a resize operation should consider rows that have
diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs
index d7d43770..da6f5da8 100644
--- a/term/src/test/mod.rs
+++ b/term/src/test/mod.rs
@@ -894,6 +894,34 @@ fn test_resize_2162_by_2_then_up_1() {
term.assert_cursor_pos(19, 0, None, Some(8));
}
+/// The alt screen has no scrollback, so shrinking it must not scroll the
+/// grid up to chase the cursor: full-screen apps redraw on SIGWINCH, and
+/// the bottom-gravity slice briefly showed rows from the middle of the
+/// old grid at the top of the pane (visible as flicker while dragging a
+/// split). The alt screen is anchored to its top: the bottom is cropped
+/// and the cursor clamped into the window.
+#[test]
+fn test_alt_screen_shrink_is_top_anchored() {
+ let mut term = TestTerm::new(6, 10, 100);
+ term.print("\x1b[?1049h");
+ term.print("aaa\r\nbbb\r\nccc\r\nddd\r\neee\r\nfff");
+ assert_visible_contents(
+ &term,
+ file!(),
+ line!(),
+ &["aaa", "bbb", "ccc", "ddd", "eee", "fff"],
+ );
+ term.resize(TerminalSize {
+ rows: 4,
+ cols: 10,
+ pixel_width: 0,
+ pixel_height: 0,
+ dpi: 0,
+ });
+ assert_visible_contents(&term, file!(), line!(), &["aaa", "bbb", "ccc", "ddd"]);
+ term.assert_cursor_pos(3, 3, None, None);
+}
+
/// This test skips over an edge case with cursor positioning,
/// so it passes even ahead of a fix for issue 2162.
#[test]