mirror of
https://github.com/nushell/reedline.git
synced 2024-11-13 08:50:55 +03:00
cd2d263eb4
* 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>
42 lines
1.4 KiB
Rust
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(());
|
|
}
|
|
}
|
|
}
|
|
}
|