p2p_chat/ui/log_mode/input/
history.rs

1//! This module contains logic for navigating command history in log mode.
2use crate::ui::log_mode::LogMode;
3use crate::ui::UIState;
4
5impl LogMode {
6    /// Navigates through the input history in log mode.
7    ///
8    /// # Arguments
9    ///
10    /// * `state` - The current UI state, which holds the input buffer.
11    /// * `up` - If `true`, navigates to an older history entry; if `false`, navigates to a newer entry.
12    pub(crate) fn navigate_history(&mut self, state: &mut UIState, up: bool) {
13        if self.input_history.is_empty() {
14            return;
15        }
16
17        let new_index = if up {
18            match self.history_index {
19                None => Some(self.input_history.len() - 1),
20                Some(0) => Some(0),
21                Some(i) => Some(i - 1),
22            }
23        } else {
24            match self.history_index {
25                None => None,
26                Some(i) if i + 1 >= self.input_history.len() => None,
27                Some(i) => Some(i + 1),
28            }
29        };
30
31        self.history_index = new_index;
32
33        if let Some(index) = new_index {
34            state.input_buffer = self.input_history[index].clone();
35            state.safe_cursor_end();
36        } else {
37            state.input_buffer.clear();
38            state.safe_cursor_home();
39        }
40    }
41}