2020-10-19 16:16:00 +02:00
|
|
|
use crate::Editor;
|
2020-10-13 19:10:50 +02:00
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
2020-10-09 22:55:45 +02:00
|
|
|
use std::string::String;
|
|
|
|
|
|
|
|
pub struct Prompt {
|
2020-10-16 09:58:26 +02:00
|
|
|
pub prompt: String,
|
|
|
|
pub line: String,
|
|
|
|
pub cursor: usize,
|
2020-10-19 19:39:35 +02:00
|
|
|
pub completion: Option<Vec<String>>,
|
|
|
|
pub should_close: bool,
|
|
|
|
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
|
2020-10-16 07:37:12 +02:00
|
|
|
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Prompt {
|
2020-10-15 12:08:01 +02:00
|
|
|
pub fn new(
|
2020-10-16 09:58:26 +02:00
|
|
|
prompt: String,
|
2020-10-19 19:39:35 +02:00
|
|
|
completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
|
2020-10-16 07:37:12 +02:00
|
|
|
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
|
2020-10-15 12:08:01 +02:00
|
|
|
) -> Prompt {
|
|
|
|
Prompt {
|
2020-10-16 09:58:26 +02:00
|
|
|
prompt,
|
|
|
|
line: String::new(),
|
|
|
|
cursor: 0,
|
2020-10-19 19:39:35 +02:00
|
|
|
completion: None,
|
|
|
|
should_close: false,
|
2020-10-15 12:08:01 +02:00
|
|
|
completion_fn: Box::new(completion_fn),
|
|
|
|
callback_fn: Box::new(callback_fn),
|
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_char(&mut self, c: char) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.line.insert(self.cursor, c);
|
|
|
|
self.cursor += 1;
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
2020-10-13 00:23:48 +02:00
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_left(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor > 1 {
|
|
|
|
self.cursor -= 1;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_right(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor < self.line.len() {
|
|
|
|
self.cursor += 1;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_start(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor = 0;
|
2020-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_end(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor = self.line.len();
|
2020-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:57:55 +02:00
|
|
|
pub fn delete_char_backwards(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor > 0 {
|
|
|
|
self.line.remove(self.cursor - 1);
|
|
|
|
self.cursor -= 1;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 07:37:12 +02:00
|
|
|
pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
|
2020-10-13 00:23:48 +02:00
|
|
|
match key_event {
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char(c),
|
2020-10-13 19:10:50 +02:00
|
|
|
modifiers: KeyModifiers::NONE,
|
2020-10-13 00:23:48 +02:00
|
|
|
} => self.insert_char(c),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Esc, ..
|
2020-10-19 19:39:35 +02:00
|
|
|
} => self.should_close = true,
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Right,
|
|
|
|
..
|
2020-10-13 19:10:50 +02:00
|
|
|
} => self.move_char_right(),
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Left,
|
|
|
|
..
|
2020-10-13 19:10:50 +02:00
|
|
|
} => self.move_char_left(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('e'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_end(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('a'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_start(),
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Backspace,
|
|
|
|
..
|
|
|
|
} => self.delete_char_backwards(),
|
2020-10-15 12:08:01 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Enter,
|
|
|
|
..
|
2020-10-16 09:58:26 +02:00
|
|
|
} => (self.callback_fn)(editor, &self.line),
|
2020-10-19 16:16:00 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Tab, ..
|
|
|
|
} => {
|
2020-10-19 19:39:35 +02:00
|
|
|
self.completion = (self.completion_fn)(&self.line);
|
2020-10-19 16:16:00 +02:00
|
|
|
}
|
2020-10-13 00:23:48 +02:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|