Skip to main content

codetether_agent/tui/app/state/
input_edit.rs

1//! Input text editing methods (insert, delete, clear).
2
3use crate::tui::models::InputMode;
4
5impl super::AppState {
6    pub fn insert_char(&mut self, c: char) {
7        self.clamp_input_cursor();
8        let byte_index = self.char_to_byte_index(self.input_cursor);
9        self.input.insert(byte_index, c);
10        self.input_cursor += 1;
11        self.history_index = None;
12        self.refresh_slash_suggestions();
13    }
14
15    pub fn insert_text(&mut self, text: &str) {
16        if text.is_empty() {
17            return;
18        }
19        self.clamp_input_cursor();
20        let byte_index = self.char_to_byte_index(self.input_cursor);
21        self.input.insert_str(byte_index, text);
22        self.input_cursor += text.chars().count();
23        self.history_index = None;
24        self.refresh_slash_suggestions();
25    }
26
27    pub fn delete_backspace(&mut self) {
28        self.clamp_input_cursor();
29        if self.input_cursor == 0 {
30            return;
31        }
32        let start = self.char_to_byte_index(self.input_cursor - 1);
33        let end = self.char_to_byte_index(self.input_cursor);
34        self.input.replace_range(start..end, "");
35        self.input_cursor -= 1;
36        self.history_index = None;
37        self.refresh_slash_suggestions();
38    }
39
40    pub fn delete_forward(&mut self) {
41        self.clamp_input_cursor();
42        if self.input_cursor >= self.input_char_count() {
43            return;
44        }
45        let start = self.char_to_byte_index(self.input_cursor);
46        let end = self.char_to_byte_index(self.input_cursor + 1);
47        self.input.replace_range(start..end, "");
48        self.history_index = None;
49        self.refresh_slash_suggestions();
50    }
51
52    pub fn clear_input(&mut self) {
53        self.input.clear();
54        self.input_cursor = 0;
55        self.input_scroll = 0;
56        self.input_mode = InputMode::Normal;
57        self.slash_suggestions.clear();
58        self.selected_slash_suggestion = 0;
59        self.history_index = None;
60    }
61}