steer_tui/tui/handlers/
text_manipulation.rs

1use crate::error::Result;
2use crate::tui::Tui;
3use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
4use tui_textarea::{CursorMove, Input};
5
6impl Tui {
7    /// Common text manipulation handler used by both Simple mode and Vim insert mode
8    pub fn handle_text_manipulation(&mut self, key: KeyEvent) -> Result<bool> {
9        match (key.code, key.modifiers) {
10            // Basic navigation
11            (KeyCode::Left, KeyModifiers::NONE) => {
12                self.input_panel_state
13                    .textarea
14                    .move_cursor(CursorMove::Back);
15                Ok(true)
16            }
17            (KeyCode::Right, KeyModifiers::NONE) => {
18                self.input_panel_state
19                    .textarea
20                    .move_cursor(CursorMove::Forward);
21                Ok(true)
22            }
23            (KeyCode::Up, KeyModifiers::NONE) => {
24                self.input_panel_state.textarea.move_cursor(CursorMove::Up);
25                Ok(true)
26            }
27            (KeyCode::Down, KeyModifiers::NONE) => {
28                self.input_panel_state
29                    .textarea
30                    .move_cursor(CursorMove::Down);
31                Ok(true)
32            }
33
34            // Word navigation (Alt/Option + arrows)
35            (KeyCode::Left, KeyModifiers::ALT) => {
36                self.input_panel_state
37                    .textarea
38                    .move_cursor(CursorMove::WordBack);
39                Ok(true)
40            }
41            (KeyCode::Right, KeyModifiers::ALT) => {
42                self.input_panel_state
43                    .textarea
44                    .move_cursor(CursorMove::WordForward);
45                Ok(true)
46            }
47
48            // Line navigation (Cmd/Ctrl + arrows)
49            (KeyCode::Left, m)
50                if m.contains(KeyModifiers::CONTROL) || m.contains(KeyModifiers::SUPER) =>
51            {
52                self.input_panel_state
53                    .textarea
54                    .move_cursor(CursorMove::Head);
55                Ok(true)
56            }
57            (KeyCode::Right, m)
58                if m.contains(KeyModifiers::CONTROL) || m.contains(KeyModifiers::SUPER) =>
59            {
60                self.input_panel_state.textarea.move_cursor(CursorMove::End);
61                Ok(true)
62            }
63
64            // Text deletion
65            (KeyCode::Char('u'), KeyModifiers::CONTROL) => {
66                self.input_panel_state.textarea.delete_line_by_head();
67                Ok(true)
68            }
69            (KeyCode::Char('k'), KeyModifiers::CONTROL) => {
70                self.input_panel_state.textarea.delete_line_by_end();
71                Ok(true)
72            }
73            (KeyCode::Backspace, m)
74                if m.contains(KeyModifiers::SUPER) || m.contains(KeyModifiers::CONTROL) =>
75            {
76                self.input_panel_state.textarea.delete_line_by_head();
77                Ok(true)
78            }
79            (KeyCode::Delete, m)
80                if m.contains(KeyModifiers::SUPER) || m.contains(KeyModifiers::CONTROL) =>
81            {
82                self.input_panel_state.textarea.delete_line_by_end();
83                Ok(true)
84            }
85
86            // Word deletion
87            (KeyCode::Backspace, KeyModifiers::ALT) => {
88                self.input_panel_state.textarea.delete_word();
89                Ok(true)
90            }
91
92            // Multi-line support
93            (KeyCode::Enter, m)
94                if m.contains(KeyModifiers::SHIFT)
95                    || m.contains(KeyModifiers::ALT)
96                    || m.contains(KeyModifiers::CONTROL) =>
97            {
98                self.input_panel_state
99                    .handle_input(Input::from(KeyEvent::new(
100                        KeyCode::Char('\n'),
101                        KeyModifiers::empty(),
102                    )));
103                Ok(true)
104            }
105            (KeyCode::Char('j'), KeyModifiers::CONTROL) => {
106                self.input_panel_state
107                    .handle_input(Input::from(KeyEvent::new(
108                        KeyCode::Char('\n'),
109                        KeyModifiers::empty(),
110                    )));
111                Ok(true)
112            }
113
114            // Not handled by text manipulation
115            _ => Ok(false),
116        }
117    }
118}