kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.53k stars 176 forks source link

Validator usage is unclear #459

Open lberrymage opened 3 years ago

lberrymage commented 3 years ago

I'm trying to use Validator to automatically validate custom, single-line commands. As a test for handling invalid input I set up the following code:

// `use` statements omitted for brevity
struct CommandHelper;

impl Completer for CommandHelper {
    type Candidate = String;
}
impl Helper for CommandHelper {}
impl Highlighter for CommandHelper {}
impl Hinter for CommandHelper {}
impl Validator for CommandHelper {
    fn validate(&self, ctx: &mut ValidationContext) -> rustyline::Result<ValidationResult> {
        let mut words = ctx.input().split_whitespace();

        match words.next() {
            None => Ok(ValidationResult::Valid(None)),
            Some(command) => Ok(ValidationResult::Invalid(Some("invalid command".to_string()))),
            Some(_) => (),
        }
    }
}

fn main() {
    let mut rl = Editor::<CommandHelper>::new();
    loop {
        match rl.readline("prompt >") {
            Ok(line) => println!("Got line: {}", line),
            Err(e) => eprintln!("Error: {}", e),
        }
    }
}

What's curious is that "invalid command" is never printed to the terminal no matter whether the input is empty, contains one "word," or contains multiple "words." Notably, adding rl.set_helper(Some(CommandHelper { })) before the REPL prevents readline() from ever returning without interrupting the process with CTRL-C. The input_validation example suffers from these same problems if the user enters anything but SELECT <anything_goes_here> ; on the first line.

My questions are:

Additionally:

I am very willing to improve documentation on these subjects (once I understand them myself), write/fix examples, etc..

gwenn commented 3 years ago
gwenn commented 3 years ago

Your "invalid command" message should be displayed with rustyline version >= 7.0.0