steer_tui/tui/handlers/
bash.rs

1use crate::error::Result;
2use crate::tui::Tui;
3use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
4use steer_core::app::AppCommand;
5use tui_textarea::CursorMove;
6
7impl Tui {
8    pub async fn handle_bash_mode(&mut self, key: KeyEvent) -> Result<bool> {
9        // Check for Ctrl+C
10        if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
11            if self.is_processing {
12                // Cancel processing
13                self.client
14                    .send_command(AppCommand::CancelProcessing)
15                    .await?;
16            } else {
17                // Cancel bash mode and return to normal without clearing text
18                self.input_mode = self.default_input_mode();
19                self.input_panel_state
20                    .textarea
21                    .set_placeholder_text("Type your message here...");
22            }
23            return Ok(false);
24        }
25
26        // Check for Esc
27        if key.code == KeyCode::Esc {
28            // Return to normal mode without clearing text
29            self.input_mode = self.default_input_mode();
30            self.input_panel_state
31                .textarea
32                .set_placeholder_text("Type your message here...");
33        // Check for Alt+Left/Right for word navigation
34        } else if key.modifiers == KeyModifiers::ALT {
35            match key.code {
36                KeyCode::Left => {
37                    self.input_panel_state
38                        .textarea
39                        .move_cursor(CursorMove::WordBack);
40                }
41                KeyCode::Right => {
42                    self.input_panel_state
43                        .textarea
44                        .move_cursor(CursorMove::WordForward);
45                }
46                _ => {
47                    // Convert KeyEvent to Input and let the panel state handle it
48                    let input = tui_textarea::Input::from(key);
49                    self.input_panel_state.handle_input(input);
50                }
51            }
52        } else if (key.code == KeyCode::Enter
53            && (key.modifiers == KeyModifiers::SHIFT
54                || key.modifiers == KeyModifiers::ALT
55                || key.modifiers == KeyModifiers::CONTROL))
56            || (key.code == KeyCode::Char('j') && key.modifiers == KeyModifiers::CONTROL)
57        {
58            // Insert a newline for various key combinations
59            let input = tui_textarea::Input::from(KeyEvent::new(
60                KeyCode::Char('\n'),
61                KeyModifiers::empty(),
62            ));
63            self.input_panel_state.handle_input(input);
64        } else if key.code == KeyCode::Enter {
65            // Execute the bash command
66            let command = self.input_panel_state.content();
67            if !command.trim().is_empty() {
68                self.client
69                    .send_command(AppCommand::ExecuteBashCommand { command })
70                    .await?;
71                self.input_panel_state.clear(); // Clear after executing
72                self.input_mode = self.default_input_mode();
73                self.input_panel_state
74                    .textarea
75                    .set_placeholder_text("Type your message here...");
76            }
77        } else {
78            // Convert KeyEvent to Input and let the panel state handle it
79            let input = tui_textarea::Input::from(key);
80            self.input_panel_state.handle_input(input);
81        }
82        Ok(false)
83    }
84}