Skip to content

Commit cfcddd6

Browse files
author
User
committed
fix(tui): incorrect scroll to the latest message in history view
1 parent 54fd6e7 commit cfcddd6

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/tui/app.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub struct UiState<'a> {
8686
pub focus: FocusedComponent,
8787
pub tree_state: FileTreeState,
8888
pub history_state: ListState,
89+
pub history_follow_last: bool,
8990
pub history_opened_state: HashSet<usize>,
9091
pub throbber_state: throbber_widgets_tui::ThrobberState,
9192
pub widget_areas: HashMap<FocusedComponent, Rect>,
@@ -110,6 +111,7 @@ impl UiState<'_> {
110111
focus: FocusedComponent::Input,
111112
tree_state: FileTreeState::new(workspace),
112113
history_state: ListState::default(),
114+
history_follow_last: false,
113115
history_opened_state: HashSet::default(),
114116
throbber_state: throbber_widgets_tui::ThrobberState::default(),
115117
widget_areas: HashMap::default(),
@@ -152,9 +154,7 @@ impl App<'_> {
152154

153155
pub async fn run(mut self, mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
154156
if !self.model.messages.is_empty() {
155-
self.ui
156-
.history_state
157-
.select(Some(self.model.messages.len() - 1));
157+
self.ui.history_follow_last = true;
158158
}
159159
while self.running {
160160
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
@@ -288,20 +288,19 @@ impl App<'_> {
288288
AgentOutputEvent::NewTask => {
289289
self.model.messages.clear();
290290
self.ui.history_state.select(None);
291+
self.ui.history_follow_last = false;
291292
self.ui.history_opened_state.clear();
292293
self.ui.focus = FocusedComponent::Input;
293294
}
294295
AgentOutputEvent::AddMessage(message) => {
295296
self.model.messages.push(message);
296-
self.ui
297-
.history_state
298-
.select(Some(self.model.messages.len() - 1));
297+
self.ui.history_follow_last = true;
299298
}
300299
AgentOutputEvent::UpdateMessage(message) => {
301300
if !self.model.messages.is_empty() {
302301
let len = self.model.messages.len() - 1;
303302
self.model.messages[len] = message;
304-
self.ui.history_state.select(Some(len));
303+
self.ui.history_follow_last = true;
305304
}
306305
}
307306
AgentOutputEvent::CommandStatus(status) => {

src/tui/widgets.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ impl Widget for &mut App<'_> {
184184
}
185185
let chat_len = messages.len();
186186

187+
if self.ui.history_follow_last && chat_len >= 1 {
188+
self.ui.history_follow_last = false;
189+
self.ui.history_state.select(Some(chat_len - 1));
190+
}
191+
187192
if self
188193
.ui
189194
.history_state

0 commit comments

Comments
 (0)