Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EOF should maybe yield ReadlineError::Eof instead of just terminating Editor::iter #647

Closed
MultisampledNight opened this issue Aug 25, 2022 · 5 comments · Fixed by #650
Closed

Comments

@MultisampledNight
Copy link

MultisampledNight commented Aug 25, 2022

Context

I'm using using rustyline for the first time in a situation like this:

    rl.iter(" > ").try_for_each(|line| {
        // ...
        let result = match line {
            Ok(line) => process_line(line),
            Err(Eof | Interrupted) => Err(Exit), // user wants to abort normally, fine
            Err(err) => Err(Fatal(err)),         // help
        };
        // ...
    });

I wanted to bind both Ctrl-D and Ctrl-C to exiting, as my program isn't hosting anything else it could reach out to for processing the interrupt. But to my surprise, it seems like Eof is never emitted when using Editor::iter, it just plainly ends the iterator. I'd have expected it to be a constant stream of events.

Suggestions

  • Make the iterator yielded by Editor::iter infinite, while providing ReadlineError::Eof as a normal "error". (I'd be open to implement that and open a PR, if wanted)
  • If ^ is not a viable option for whatever reason, document in Editor::iter logical OR in ReadlineError::Eof the special case of when the iterator terminates.

Minimal reproducable code example

Relying on rustyline 10.0.0

use rustyline::{error::ReadlineError, Editor};

fn main() {
    let mut rl = Editor::<()>::new().unwrap();

    for line in rl.iter("pls hit ctrl-d> ") {
        match line {
            Err(ReadlineError::Eof) => unreachable!("I want to get here"),
            Err(ReadlineError::Interrupted) => {
                unreachable!("This is also a real error though it technically isn't")
            }
            Err(err) => panic!("irrelevant: {}", err),
            Ok(_) => panic!("please do enter ctrl-d"),
        }
    }

    println!("done! :sparkles:");
}
@gwenn
Copy link
Collaborator

gwenn commented Aug 25, 2022

https://github.com/kkawakam/rustyline/blob/master/src/lib.rs#L942
The iterator currently behaves like a line iterator on a file: the iterator ends at end of file.

@MultisampledNight
Copy link
Author

Yep, I get the current behavior. I'd love to have some documentation shine light on this behavior though, or allow it to be handled specifically by yielding Eof instead of just terminating.

@gwenn
Copy link
Collaborator

gwenn commented Aug 25, 2022

For me, the line iterator becomes useless without the EOF condition: just use a loop.
But yes, this should be documented.

@MultisampledNight
Copy link
Author

Alright. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants