Keep the TUI composer caret anchored while typing (#62174)

## Why

When the composer footer gains or loses a hint row, typing the first
character or deleting the last one can move the caret vertically.

## What changed

Reserve a row for passive footer hints when the status line is visible
and there is room after laying out the input, slash-command suggestions,
and previews. The row can stay blank until a hint appears.

## Testing

Add a regression test that checks the caret row before typing, after
typing one character, and after pressing Backspace. Extend coverage to
check that slash-command suggestions and a short status line fit together
when space is tight.

GitOrigin-RevId: 25df697826cebc4b7cb4aab4460c432b7454d5ea
This commit is contained in:
Eric Traut
2026-11-18 03:08:53 +00:00
committed by copyberry
parent f09fb197f5
commit 3dbc6f8657
13 changed files with 55 additions and 26 deletions
@@ -62,10 +62,7 @@ async fn follow_control_click_preserves_draft_caret_and_composer_geometry() -> R
tui.screen_size_for_event(&TuiEvent::Resize(size))?;
app.render_owned_transcript(&mut tui, size)?;
let scrolled_cursor = tui.terminal.last_known_cursor_pos;
assert_eq!(
(scrolled_cursor.x, scrolled_cursor.y + u16::from(!running)),
(cursor.x, cursor.y)
);
assert_eq!(scrolled_cursor, cursor);
let buffer = crate::custom_terminal::test_support::last_rendered_buffer(&tui.terminal);
let scrolled = super::tests::buffer_text(buffer);
let first_row = scrolled.lines().next().expect("visible transcript row");
@@ -3,12 +3,12 @@ source: tui/src/app/owned_transcript_input_tests.rs
expression: "format!(\"before\\n{before}\\n\\nafter\\n{}\", screen(&tui))"
---
before
row 13
row 14
row 15
row 16
row 17
row 18
row 19
New activity · ↓ Back to bottom · esc
› draft preserved
@@ -17,7 +17,6 @@ row 18
New activity · esc latest
after
row 25
row 26
row 27
row 28
@@ -49,7 +49,6 @@ After paste
› [Pasted Content 2084 chars]
GPT-5.6-Sol default · /tmp/project
@@ -68,7 +68,6 @@ hello world
Copied 5 chars to host clipboard
› hello world
@@ -3,7 +3,6 @@ source: tui/src/app/owned_transcript_tests.rs
expression: "snapshots.join(\"\\n\\n\")"
---
Latest
Transcript row 34
Transcript row 35
Transcript row 36
Transcript row 37
@@ -16,13 +15,14 @@ Transcript row 40
gpt-test default · /tmp/project
Reading
Transcript row 28
Transcript row 29
Transcript row 30
Transcript row 31
Transcript row 32
Transcript row 33
Transcript row 34
Transcript row 35
↓ Back to bottom · esc
› draft
@@ -31,10 +31,10 @@ Transcript row 33
esc latest
Resized multiline composer
Transcript row 28
Transcript row 29
Transcript row 30
Transcript row 31
Transcript row 32
Transcript row 33
↓ Back to bottom · esc
› draft
@@ -2,7 +2,6 @@
source: tui/src/app/warnings_tests.rs
expression: "crate::chatwidget::tests::helpers::normalize_snapshot_paths(screen)"
---
Earlier transcript row 11
Earlier transcript row 12
Earlier transcript row 13
Earlier transcript row 14
@@ -12,6 +11,7 @@ Earlier transcript row 17
Earlier transcript row 18
Earlier transcript row 19
Earlier transcript row 20
Earlier transcript row 21
Warnings · 1 of 1 · Warning
@@ -2,7 +2,6 @@
source: tui/src/app/warnings_tests.rs
expression: "crate::chatwidget::tests::helpers::normalize_snapshot_paths(screen)"
---
Earlier transcript row 11
Earlier transcript row 12
Earlier transcript row 13
Earlier transcript row 14
@@ -20,6 +19,7 @@ Earlier transcript row 25
Earlier transcript row 26
Earlier transcript row 27
Earlier transcript row 28
Earlier transcript row 29
↓ Back to bottom · esc
› draft
@@ -1,6 +1,6 @@
---
source: tui/src/app/tests/background_task_defaults_tests.rs
expression: "snapshots.join(\"\\n\\n\")"
expression: "crate::chatwidget::tests::helpers::normalize_snapshot_paths(snapshots.join(\"\\n\\n\"))"
---
owned=false
startup:
@@ -11,11 +11,11 @@ startup:
attached:
› draft during startup
GPT-6.2-Sol default · /tmp/project
owned=true
startup:
@@ -25,7 +25,6 @@ startup:
attached:
› draft during startup
GPT-6.2-Sol default · /tmp/project
+2 -2
View File
@@ -53,10 +53,10 @@ async fn warning_notice_keeps_details_in_transcript_and_preserves_draft() -> Res
let after = app.render_owned_transcript(&mut tui, size)?;
let cursor = tui.terminal.last_known_cursor_pos;
assert_eq!(
(after.bottom(), after.height, cursor.x, cursor.y + 1),
(after.bottom(), after.height, cursor.x, cursor.y),
(
before.bottom(),
before.height + 1,
before.height,
original_cursor.x,
original_cursor.y
)
@@ -297,7 +297,7 @@ impl ChatComposer {
let find_active = options
.footer
.is_some_and(|footer| footer.cursor_column.is_some());
let footer = FooterLayout::new(
let mut footer = FooterLayout::new(
workspace_height,
self.footer_hint_height(width, options),
/*priority_hint_rows*/ if find_active { 2 } else { 1 },
@@ -315,6 +315,16 @@ impl ChatComposer {
} else {
thumbnails = 0;
}
if self.footer.status_line_enabled
&& footer.wanted_hints == 0
&& wanted_status > 0
&& wanted_status < footer.cap.min(available)
&& self.attachments.is_empty()
&& !options.is_dialog_backdrop
&& self.passive_hints(options).is_some()
{
footer.wanted_hints = 1;
}
ComposerRows {
input: input_height,
above_input,
@@ -3,7 +3,7 @@ source: tui/src/bottom_pane/chat_composer/status_surface_tests.rs
expression: "format!(\"{buf:?}\")"
---
Buffer {
area: Rect { x: 0, y: 0, width: 80, height: 9 },
area: Rect { x: 0, y: 0, width: 80, height: 10 },
content: [
" /model choose what model and reasoning effort to use ",
"› /memories configure memory use and generation ",
@@ -14,6 +14,7 @@ Buffer {
"› /m ",
" ",
" model · high · fast ",
" ",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
@@ -127,6 +127,26 @@ fn frame(buffer: &Buffer) -> String {
.join("\n")
}
#[test]
fn status_line_keeps_the_caret_anchored_while_typing() {
let mut composer = composer();
let options = ComposerRenderOptions {
separate_status_line: true,
max_height: Some(5),
..Default::default()
};
let mut rows = Vec::new();
for key in [None, Some(KeyCode::Char('x')), Some(KeyCode::Backspace)] {
if let Some(key) = key {
composer.handle_key_event(KeyEvent::from(key));
}
let height = composer.desired_height_with_options(/*width*/ 48, options);
let area = Rect::new(0, 5 - height, 48, height);
rows.push(composer.cursor_pos_with_options(area, options).unwrap().1);
}
assert_eq!(rows, [1, 1, 1]);
}
#[test]
fn extension_uses_the_full_width_after_resize() {
let mut composer = composer();
@@ -1134,6 +1154,11 @@ fn slash_suggestions_take_space_before_status_expands() {
let (buf, rows) = render_status(&composer, area, options);
assert_eq!(rows, ["one…"]);
insta::assert_snapshot!(frame(&buf));
composer.set_status_line(Some("OK".into()));
let (buf, rows) = render_status(&composer, Rect::new(0, 0, 80, 5), options);
assert_eq!(rows, ["OK"]);
assert!(frame(&buf).contains("/model"));
}
#[test]
@@ -37,10 +37,10 @@ parent-29 │
│child-29
│
│
› parent draft- │
│› child draft!
GPT-6.2-Sol default │
│ GPT-6.2-Sol default · Agent
› parent draft- │› child draft!
│
GPT-6.2-Sol default │ GPT-6.2-Sol default · Agent
│
99 columns, reopened
Side chat × +