Skip to main content

codetether_agent/tui/app/state/
history.rs

1//! Command history navigation (↑/↓ recall).
2//!
3//! Browsing mode cycles through previous inputs; releasing returns to editing.
4
5use crate::tui::models::InputMode;
6
7impl super::AppState {
8    pub fn push_history(&mut self, entry: String) {
9        if !entry.trim().is_empty() {
10            self.command_history.push(entry);
11            self.history_index = None;
12        }
13    }
14
15    pub fn history_prev(&mut self) -> bool {
16        if self.command_history.is_empty() {
17            return false;
18        }
19        let history_len = self.command_history.len();
20        let new_index = match self.history_index {
21            Some(index) if index > 0 => Some(index - 1),
22            Some(index) => Some(index),
23            None => Some(history_len - 1),
24        };
25        if let Some(index) = new_index {
26            self.history_index = Some(index);
27            self.input = self.command_history[index].clone();
28            self.input_cursor = self.input_char_count();
29            self.input_mode = if self.input.starts_with('/') {
30                InputMode::Command
31            } else {
32                InputMode::Editing
33            };
34            self.refresh_slash_suggestions();
35            return true;
36        }
37        false
38    }
39
40    pub fn history_next(&mut self) -> bool {
41        if self.command_history.is_empty() {
42            return false;
43        }
44        match self.history_index {
45            Some(index) if index + 1 < self.command_history.len() => {
46                let next = index + 1;
47                self.history_index = Some(next);
48                self.input = self.command_history[next].clone();
49            }
50            Some(_) => {
51                self.history_index = None;
52                self.input.clear();
53            }
54            None => return false,
55        }
56        self.input_cursor = self.input_char_count();
57        self.input_mode = if self.input.is_empty() {
58            InputMode::Normal
59        } else if self.input.starts_with('/') {
60            InputMode::Command
61        } else {
62            InputMode::Editing
63        };
64        self.refresh_slash_suggestions();
65        true
66    }
67}