diff --git a/examples/input_validation.rs b/examples/input_validation.rs index c80a34dad..6bd643edd 100644 --- a/examples/input_validation.rs +++ b/examples/input_validation.rs @@ -12,7 +12,7 @@ impl Validator for InputValidator { let result = if !input.starts_with("SELECT") { Invalid(Some(" --< Expect: SELECT stmt".to_owned())) } else if !input.ends_with(';') { - Incomplete + Incomplete(None) } else { Valid(None) }; diff --git a/src/edit.rs b/src/edit.rs index 703171210..bbb825252 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -208,7 +208,11 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { let result = validator.validate(&mut ValidationContext::new(self))?; let corrected = self.changes.borrow_mut().end(); match result { - ValidationResult::Incomplete => {} + ValidationResult::Incomplete(ref msg) => { + if msg.is_some() { + self.refresh_line_with_msg(msg.as_deref())?; + } + } ValidationResult::Valid(ref msg) => { // Accept the line regardless of where the cursor is. if corrected || self.has_hint() || msg.is_some() { diff --git a/src/lib.rs b/src/lib.rs index 0d85b1b0e..6488ce56a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -656,7 +656,7 @@ fn readline_direct( validate::ValidationResult::Invalid(Some(msg)) => { writer.write_all(msg.as_bytes())?; } - validate::ValidationResult::Incomplete => { + validate::ValidationResult::Incomplete(msg) => { // Add newline and keep on taking input if trailing_r { input.push('\r'); @@ -664,6 +664,9 @@ fn readline_direct( if trailing_n { input.push('\n'); } + if let Some(msg) = msg { + writer.write_all(msg.as_bytes())?; + } } _ => {} } diff --git a/src/validate.rs b/src/validate.rs index b29c682a5..1d0cdd58e 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -7,7 +7,7 @@ use crate::Result; #[non_exhaustive] pub enum ValidationResult { /// Incomplete input - Incomplete, + Incomplete(Option), /// Validation fails with an optional error message. User must fix the /// input. Invalid(Option), @@ -23,7 +23,9 @@ impl ValidationResult { pub(crate) fn has_message(&self) -> bool { matches!( self, - ValidationResult::Valid(Some(_)) | ValidationResult::Invalid(Some(_)) + ValidationResult::Valid(Some(_)) + | ValidationResult::Invalid(Some(_)) + | ValidationResult::Incomplete(Some(_)) ) } } @@ -142,6 +144,6 @@ fn validate_brackets(input: &str) -> ValidationResult { if stack.is_empty() { ValidationResult::Valid(None) } else { - ValidationResult::Incomplete + ValidationResult::Incomplete(None) } }