Keep the TUI transcript selection cursor visible (#63056)

## Why

Moving the selection cursor below the viewport could jump the transcript so
the cursor landed at the top. Resizing the viewport could also leave the
cursor offscreen.

## What changed

Scroll just enough to reveal the cursor, accounting for the row occupied by
a pinned prompt header. Recheck visibility when the viewport changes size
or text rewraps. When rendering an inactive pane, preserve its saved scroll
position and recheck cursor visibility when it becomes active again.

## Testing

Extend transcript tests to cover scrolling past the bottom edge, shrinking
and narrowing the viewport, reactivating a pane, and moving the cursor
around a pinned prompt header.

GitOrigin-RevId: 8772a79316fb31e758986d1f71a0d776fc5ff2b5
This commit is contained in:
Eric Traut
2026-11-21 23:25:56 +00:00
committed by copyberry
parent 17cb2e3a7f
commit f12fa0fbc5
7 changed files with 195 additions and 18 deletions
+1 -1
View File
@@ -4715,7 +4715,7 @@ async fn handle_start_side_seeds_navigation_before_thread_started() -> Result<()
app.transcript_view.end_select_mode();
let reopened = side_frame(&mut app, &mut tui, size)?;
assert!(
reopened.contains(child_row) && reopened.contains("child draft!"),
reopened.contains("child-29") && reopened.contains("child draft!"),
"{reopened}"
);
assert_eq!(
@@ -24,17 +24,17 @@ parent-29
100 columns, reopened
parent-24 │ Side chat × +
│child-24
parent-25 │
│child-25
parent-26 │
│child-26
parent-27 │
│child-27
parent-28 │
│child-28
parent-29 │
│child-29
│
parent-25 │child-25
│
parent-26 │child-26
│
parent-27 │child-27
│
parent-28 │child-28
│
parent-29 │child-29
│
│
│
› parent draft- │› child draft!
@@ -44,7 +44,6 @@ parent-29 │
99 columns, reopened
Side chat × +
child-24
child-25
@@ -57,6 +56,7 @@ child-28
child-29
› child draft!
GPT-6.2-Sol default · Agent
+23
View File
@@ -119,6 +119,7 @@ pub(crate) struct TranscriptView {
visible: Vec<VisibleRow>,
selection: Option<Selection>,
select_mode: Option<SelectMode>,
select_reveal_pending: bool,
copy_mode: Option<copy_mode::CopyMode>,
held_reading: Option<ViewSnapshot>,
search: Search,
@@ -163,6 +164,7 @@ impl Default for TranscriptView {
visible: Vec::new(),
selection: None,
select_mode: None,
select_reveal_pending: false,
copy_mode: None,
held_reading: None,
search: Search::default(),
@@ -212,6 +214,8 @@ impl TranscriptView {
}
fn paint_rows(&mut self, area: Rect, buf: &mut Buffer, cells: &[Arc<dyn HistoryCell>]) {
let previous_viewport_height =
self.area.height + u16::from(self.prompt_header_target.is_some());
self.composer_tip = None;
self.prompt_header_target = None;
self.cache.begin_frame();
@@ -223,6 +227,10 @@ impl TranscriptView {
Clear.render(area, buf);
let previous_height = self.area.height;
let resized = self.area.width != area.width || self.area.bottom() != area.bottom();
let select_width_changed = self
.visible
.first()
.is_some_and(|row| row.layout.width != area.width);
self.prepare_width(area.width);
self.area = area;
self.normalize_selection(cells);
@@ -234,6 +242,15 @@ impl TranscriptView {
self.tail_visible = false;
return;
}
let select_reveal_pending = std::mem::take(&mut self.select_reveal_pending);
if (resized
|| select_width_changed
|| previous_viewport_height != area.height
|| select_reveal_pending)
&& self.is_select_mode()
{
self.reveal_select_cursor(cells);
}
if let Some(key) = self.disclosure.anchor_control.take()
&& let Position::Reading(mut anchor) = self.position
&& anchor.key == key
@@ -536,7 +553,13 @@ impl TranscriptView {
})
});
}
let position = self.position;
self.render(area, buffer, cells);
if self.is_select_mode() && self.position != position {
self.position = position;
// Reveal again if this pane becomes active without another resize.
self.select_reveal_pending = true;
}
self.unseen_activity = unseen_activity;
}
@@ -219,6 +219,31 @@ fn paused_header_reservation_survives_copy_until_scroll_or_resize() {
user("Second question"),
cell("done 10:16 AM"),
];
let mut view = TranscriptView::default();
let before = render(&mut view, &cells, /*width*/ 40, /*height*/ 7);
assert!(view.begin_select_mode(&cells));
view.scroll(&cells, /*rows*/ 100);
render(&mut view, &cells, /*width*/ 40, /*height*/ 7);
for _ in 0..7 {
if view.selection.as_ref().unwrap().start.key == EntryKey::cell(&cells[1]) {
break;
}
assert!(matches!(
view.handle_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE), &cells),
Some(ViewAction::Changed)
));
assert_eq!(
text(&render(
&mut view, &cells, /*width*/ 40, /*height*/ 7
)),
text(&before)
);
}
assert_eq!(
view.selection.as_ref().unwrap().start.key,
EntryKey::cell(&cells[1])
);
assert_eq!(view.position, Position::Latest);
for scroll in [false, true] {
let mut view = TranscriptView::default();
let before = render(&mut view, &cells, /*width*/ 40, /*height*/ 7);
@@ -248,8 +273,16 @@ fn paused_header_reservation_survives_copy_until_scroll_or_resize() {
render(&mut view, &cells, /*width*/ 40, /*height*/ 7),
view.is_following(),
),
(before, false)
(before.clone(), false)
);
assert!(view.begin_select_mode(&cells));
assert_eq!(
text(&render(
&mut view, &cells, /*width*/ 40, /*height*/ 7
)),
text(&before)
);
view.end_select_mode();
let height = if scroll {
view.scroll(&cells, /*rows*/ -1);
7
@@ -425,18 +425,51 @@ impl TranscriptView {
return;
};
let cursor = mode.cursor(selection);
let snapshot = self.snapshot_cells();
let cells = snapshot.as_deref().unwrap_or(cells);
let index = self.resolve(cells, cursor);
let row = self.layout(cells, index).map_or(/*default*/ 0, |layout| {
layout
.row_for_offset(cursor.offset)
.saturating_add_signed(cursor.row_bias.saturating_neg())
});
if !self
let start = self.start(cells);
let height = self.area.height + u16::from(self.prompt_header_target.is_some());
let width = self.area.width;
let detailed = self.detailed;
let has_header =
|index| height >= 4 && prompt_header::line(cells, index, width, detailed).is_some();
let painted_start = self
.visible
.iter()
.any(|visible| visible.index == index && visible.row == row)
.first()
.is_some_and(|first| (first.index, first.row) == start);
let end = self
.visible
.last()
.filter(|_| painted_start)
.map(|last| (last.index, last.row))
.unwrap_or_else(|| {
let last_row = height.saturating_sub(1 + u16::from(has_header(start.0))) as isize;
self.move_rows(cells, start.0, start.1, last_row)
});
if (index, row) < start
|| ((index, row) == start
&& matches!(self.position, Position::Latest)
&& !painted_start
&& self.prompt_header_target.is_none()
&& has_header(start.0))
{
self.position = Position::Reading(Anchor { index, ..cursor });
} else if (index, row) > end {
let context = height.saturating_sub(1) as isize;
let (mut index, mut row) = self.move_rows(cells, index, row, -context);
// A newly pinned prompt consumes one of the rows available to the cursor.
if has_header(index) {
(index, row) = self.move_rows(cells, index, row, /*rows*/ 1);
}
if let Some(layout) = self.layout(cells, index) {
self.read_from_row(cells, index, row, &layout);
}
}
}
@@ -282,6 +282,7 @@ fn configured_pager_page_motion_moves_the_select_cursor() {
render(&mut view, &cells);
assert!(view.begin_select_mode(&cells));
press(&mut view, &cells, KeyCode::Char('g'));
render(&mut view, &cells);
press(&mut view, &cells, KeyCode::Char('v'));
assert!(view.select_mode.is_some_and(|mode| mode.mark.is_some()));
@@ -298,6 +299,93 @@ fn configured_pager_page_motion_moves_the_select_cursor() {
.saturating_add_signed(cursor.row_bias.saturating_neg()),
5
);
render(&mut view, &cells);
press(&mut view, &cells, KeyCode::Down);
let buffer = render(&mut view, &cells);
insta::assert_snapshot!(super::super::tests::text(&buffer), @"
line 1
line 2
line 3
line 4
line 5
line 6
");
let area = Rect::new(
/*x*/ 0, /*y*/ 1, /*width*/ 32, /*height*/ 5,
);
let mut shrunk = Buffer::empty(area);
view.render(area, &mut shrunk, &cells);
assert_eq!(
super::super::tests::text(&shrunk),
"line 2\nline 3\nline 4\nline 5\nline 6"
);
let saved_position = view.position;
let parked_area = Rect::new(
/*x*/ 0, /*y*/ 1, /*width*/ 32, /*height*/ 3,
);
let mut parked = Buffer::empty(parked_area);
view.render_parked(
parked_area,
&mut parked,
&cells,
/*active*/ None,
/*auxiliary*/ &[],
/*collapse_turn_details*/ false,
);
assert_eq!(view.position, saved_position);
assert!(super::super::tests::text(&parked).contains("line 6"));
let mut active = Buffer::empty(parked_area);
view.render(parked_area, &mut active, &cells);
assert_eq!(active, parked);
let area = Rect::new(
/*x*/ 0, /*y*/ 1, /*width*/ 5, /*height*/ 3,
);
view.prepare_width(area.width);
let mut narrowed = Buffer::empty(area);
view.render(area, &mut narrowed, &cells);
let cursor = view
.select_mode
.unwrap()
.cursor(view.selection.as_ref().unwrap());
assert!(view.visible.iter().any(|visible| visible.key == cursor.key
&& visible.row == visible.layout.row_for_offset(cursor.offset)));
let mut cells = cells;
cells.insert(
0,
Arc::new(crate::history_cell::new_user_prompt(
"question".into(),
Vec::new(),
Vec::new(),
Vec::new(),
)),
);
let mut view = TranscriptView::default();
view.jump_to_entry(&cells, /*index*/ 0);
render(&mut view, &cells);
assert!(view.begin_select_mode(&cells));
press(&mut view, &cells, KeyCode::PageDown);
press(&mut view, &cells, KeyCode::Char('j'));
render(&mut view, &cells);
let cursor = view.selection.as_ref().unwrap().start;
assert!(view.visible.iter().any(|visible| visible.key == cursor.key
&& visible.row == visible.layout.row_for_offset(cursor.offset)));
let mut view = TranscriptView::default();
view.jump_to_entry(&cells, /*index*/ 0);
render(&mut view, &cells);
assert!(view.begin_select_mode(&cells));
press(&mut view, &cells, KeyCode::PageDown);
view.scroll(&cells, /*rows*/ 100);
assert_eq!(view.position, Position::Latest);
for _ in 0..2 {
press(&mut view, &cells, KeyCode::Up);
}
render(&mut view, &cells);
let cursor = view.selection.as_ref().unwrap().start;
assert!(view.visible.iter().any(|visible| visible.key == cursor.key
&& visible.row == visible.layout.row_for_offset(cursor.offset)));
}
#[test]
+1 -1
View File
@@ -54,7 +54,7 @@ pub(crate) struct TextLayout {
logical: Vec<LogicalLine>,
text: String,
rows: Vec<TextRow>,
width: u16,
pub(super) width: u16,
pub(super) separated: bool,
pub(super) disclosure: bool,
leading_blank_hidden: bool,