2020-10-13 00:23:48 +02:00
|
|
|
use crate::commands;
|
|
|
|
use crate::View;
|
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 {
|
|
|
|
pub buffer: String,
|
2020-10-13 00:23:48 +02:00
|
|
|
pub cursor_loc: usize,
|
2020-10-15 12:08:01 +02:00
|
|
|
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
|
2020-10-15 12:14:54 +02:00
|
|
|
callback_fn: Box<dyn FnMut(&str)>,
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Prompt {
|
2020-10-15 12:08:01 +02:00
|
|
|
pub fn new(
|
|
|
|
completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
|
2020-10-15 12:14:54 +02:00
|
|
|
callback_fn: impl FnMut(&str) + 'static,
|
2020-10-15 12:08:01 +02:00
|
|
|
) -> Prompt {
|
|
|
|
Prompt {
|
2020-10-13 18:57:55 +02:00
|
|
|
buffer: String::from(""),
|
2020-10-13 00:23:48 +02:00
|
|
|
cursor_loc: 0,
|
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-13 18:57:55 +02:00
|
|
|
self.buffer.insert(self.cursor_loc, c);
|
|
|
|
self.cursor_loc += 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-13 18:57:55 +02:00
|
|
|
if self.cursor_loc > 1 {
|
|
|
|
self.cursor_loc -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_right(&mut self) {
|
2020-10-13 18:57:55 +02:00
|
|
|
if self.cursor_loc < self.buffer.len() {
|
|
|
|
self.cursor_loc += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_start(&mut self) {
|
|
|
|
self.cursor_loc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_end(&mut self) {
|
|
|
|
self.cursor_loc = self.buffer.len();
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:57:55 +02:00
|
|
|
pub fn delete_char_backwards(&mut self) {
|
|
|
|
if self.cursor_loc > 0 {
|
|
|
|
self.buffer.remove(self.cursor_loc - 1);
|
|
|
|
self.cursor_loc -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
|
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, ..
|
|
|
|
} => commands::normal_mode(view, 1),
|
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,
|
|
|
|
..
|
|
|
|
} => (self.callback_fn)(&self.buffer),
|
2020-10-13 00:23:48 +02:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|