diff options
author | Blaž Hrastnik | 2020-12-15 10:29:56 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2020-12-15 10:29:56 +0000 |
commit | 8f0b28aeb872797e4be3f07575e628f5f93e74e0 (patch) | |
tree | 10d7964d79cd2c6959e9f38be4682bea7c3b4754 /helix-term/src/ui | |
parent | 1a843b6c06b5d4d75feb0a424bbfdcfb33ab7651 (diff) |
Make the select prompt interactive.
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/mod.rs | 2 | ||||
-rw-r--r-- | helix-term/src/ui/prompt.rs | 27 |
2 files changed, 23 insertions, 6 deletions
diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index bc79e09c..9a70d1bd 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -2,7 +2,7 @@ mod editor; mod prompt; pub use editor::EditorView; -pub use prompt::Prompt; +pub use prompt::{Prompt, PromptEvent}; pub use tui::layout::Rect; pub use tui::style::{Color, Modifier, Style}; diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index f5ef9477..07c0f917 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -12,14 +12,24 @@ pub struct Prompt { pub completion: Vec<String>, pub completion_selection_index: Option<usize>, completion_fn: Box<dyn FnMut(&str) -> Vec<String>>, - callback_fn: Box<dyn FnMut(&mut Editor, &str)>, + callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>, +} + +#[derive(PartialEq)] +pub enum PromptEvent { + /// The prompt input has been updated. + Update, + /// Validate and finalize the change. + Validate, + /// Abort the change, reverting to the initial state. + Abort, } impl Prompt { pub fn new( prompt: String, mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static, - callback_fn: impl FnMut(&mut Editor, &str) + 'static, + callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static, ) -> Prompt { Prompt { prompt, @@ -160,10 +170,14 @@ impl Component for Prompt { KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, - } => self.insert_char(c), + } => { + self.insert_char(c); + (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update); + } KeyEvent { code: KeyCode::Esc, .. } => { + (self.callback_fn)(cx.editor, &self.line, PromptEvent::Abort); return close_fn; } KeyEvent { @@ -185,12 +199,15 @@ impl Component for Prompt { KeyEvent { code: KeyCode::Backspace, modifiers: KeyModifiers::NONE, - } => self.delete_char_backwards(), + } => { + self.delete_char_backwards(); + (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update); + } KeyEvent { code: KeyCode::Enter, .. } => { - (self.callback_fn)(cx.editor, &self.line); + (self.callback_fn)(cx.editor, &self.line, PromptEvent::Validate); return close_fn; } KeyEvent { |