aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r--helix-term/src/ui/mod.rs2
-rw-r--r--helix-term/src/ui/prompt.rs27
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 {