reedline/examples/validator.rs
perlindgren cd2d263eb4
Custom validator and prompt code examples (#500)
* custom validator and prompt

* Split prompt and validator demo into two files

Also add info text when running

Co-authored-by: sholderbach <sholderbach@users.noreply.github.com>
2022-11-07 20:33:54 +01:00

42 lines
1.4 KiB
Rust

// Create a reedline object with a custom validator to break the line on unfinished input.
// cargo run --example validator
//
// Input "complete" followed by [Enter], will accept the input line (Signal::Succeed will be called)
// Pressing [Enter] will in other cases give you a multi-line prompt.
use reedline::{DefaultPrompt, Reedline, Signal, ValidationResult, Validator};
use std::io;
struct CustomValidator;
// For custom validation, implement the Validator trait
impl Validator for CustomValidator {
fn validate(&self, line: &str) -> ValidationResult {
if line == "complete" {
ValidationResult::Complete
} else {
ValidationResult::Incomplete
}
}
}
fn main() -> io::Result<()> {
println!("Input \"complete\" followed by [Enter], will accept the input line (Signal::Succeed will be called)\nPressing [Enter] will in other cases give you a multi-line prompt.\nAbort with Ctrl-C or Ctrl-D");
let mut line_editor = Reedline::create().with_validator(Box::new(CustomValidator));
let prompt = DefaultPrompt::default();
loop {
let sig = line_editor.read_line(&prompt)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {}", buffer);
}
Signal::CtrlD | Signal::CtrlC => {
println!("\nAborted!");
break Ok(());
}
}
}
}