kkawakam / rustyline

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

Help, how to use multiline input prompt? Following the example code in `input_multiline.rs` but it does not compile #649

Open weezy20 opened 2 years ago

weezy20 commented 2 years ago

I am using code from https://github.com/kkawakam/rustyline/blob/master/examples/input_multiline.rs and it does not compile on my system. I can't understand the error here:

error[E0308]: mismatched types
   --> src\cli.rs:52:28
    |
52  |         rl.set_helper(Some(h));
    |                       ---- ^ expected `()`, found struct `InputValidator`
    |                       |
    |                       arguments to this enum variant are incorrect
    |
note: tuple variant defined here
   --> C:\Users\turtle\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\option.rs:526:5
    |
526 |     Some(#[stable(feature = "rust1", since = "1.0.0")] T),
    |     ^^^^

Here's my code:

    use rustyline::{error::ReadlineError, Editor};
    use rustyline::validate::MatchingBracketValidator;
    use rustyline::{Cmd, EventHandler, KeyCode, KeyEvent, Modifiers};
    use rustyline_derive::{Completer, Helper, Highlighter, Hinter, Validator};

    #[derive(Completer, Helper, Highlighter, Hinter, Validator)]
    struct InputValidator {
        #[rustyline(Validator)]
        brackets: MatchingBracketValidator,
    }

    #[allow(unreachable_code)]
    pub(crate) fn start_repl() -> std::io::Result<()> {
        let mut buf = String::new();
        let h = InputValidator {
            brackets: MatchingBracketValidator::new(),
        };
        let mut rl = Editor::<()>::new();
        rl.set_helper(Some(h));
        rl.bind_sequence(
            KeyEvent(KeyCode::Char('s'), Modifiers::CTRL),
            EventHandler::Simple(Cmd::Newline),
        );
        if rl.load_history("history.txt").is_err() {
            // println!("No previous history.");
        }
        loop {
            let line = rl.readline("> ");
            match line {
                Ok(line) => {
                    rl.add_history_entry(line.as_str());
                    buf = line;
                }
                Err(ReadlineError::Interrupted) => {
                    println!("CTRL-C");
                    println!("Exiting");
                    std::process::exit(0);
                    break;
                }
                Err(ReadlineError::Eof) => {
                    println!("CTRL-D");
                    break;
                }
                Err(e) => {
                    eprintln!("Unexpected prompt error : {e:?}");
                    std::process::exit(1);
                }
            }
            let input: &str = buf.trim();
            if input == "exit" || input == "quit" {
                println!("Exiting");
                std::process::exit(0);
            }
            buf.clear();
        }
        Ok(())
    }
gwenn commented 2 years ago
        let mut rl = Editor::<()>::new();
        rl.set_helper(Some(h));

Type () does not match InputValidator (type of h).