| 393 | } |
| 394 | |
| 395 | pub fn handle_key(&mut self, key: crossterm::event::KeyEvent) -> bool { |
| 396 | use crossterm::event::{KeyCode, KeyModifiers}; |
| 397 | |
| 398 | match key.code { |
| 399 | KeyCode::Char(c) => { |
| 400 | if c == '!' |
| 401 | && key.modifiers.is_empty() |
| 402 | && matches!(self.mode, PromptMode::Normal) |
| 403 | && self.cursor_position == 0 |
| 404 | && self.input.is_empty() |
| 405 | { |
| 406 | self.mode = PromptMode::Shell; |
| 407 | return false; |
| 408 | } |
| 409 | self.input.insert(self.cursor_position, c); |
| 410 | self.cursor_position += c.len_utf8(); |
| 411 | self.reset_history_cursor(); |
| 412 | self.recompute_suggestions(); |
| 413 | } |
| 414 | KeyCode::Backspace => { |
| 415 | if matches!(self.mode, PromptMode::Shell) && self.cursor_position == 0 { |
| 416 | self.mode = PromptMode::Normal; |
| 417 | return false; |
| 418 | } |
| 419 | if let Some(prev) = prev_char_boundary(&self.input, self.cursor_position) { |
| 420 | self.input.replace_range(prev..self.cursor_position, ""); |
| 421 | self.cursor_position = prev; |
| 422 | self.reset_history_cursor(); |
| 423 | self.recompute_suggestions(); |
| 424 | } |
| 425 | } |
| 426 | KeyCode::Delete => { |
| 427 | if let Some(next) = next_char_boundary(&self.input, self.cursor_position) { |
| 428 | self.input.replace_range(self.cursor_position..next, ""); |
| 429 | self.reset_history_cursor(); |
| 430 | self.recompute_suggestions(); |
| 431 | } |
| 432 | } |
| 433 | KeyCode::Left => { |
| 434 | if let Some(prev) = prev_char_boundary(&self.input, self.cursor_position) { |
| 435 | self.cursor_position = prev; |
| 436 | } |
| 437 | } |
| 438 | KeyCode::Right => { |
| 439 | if let Some(next) = next_char_boundary(&self.input, self.cursor_position) { |
| 440 | self.cursor_position = next; |
| 441 | } |
| 442 | } |
| 443 | KeyCode::Home => { |
| 444 | self.cursor_position = 0; |
| 445 | } |
| 446 | KeyCode::End => { |
| 447 | self.cursor_position = self.input.len(); |
| 448 | } |
| 449 | KeyCode::Tab => { |
| 450 | if key.modifiers.contains(KeyModifiers::SHIFT) { |
| 451 | self.apply_autocomplete_previous(); |
| 452 | } else { |