-
-
Notifications
You must be signed in to change notification settings - Fork 49
readline() function to ask for user input #867
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
Comments
An issue is that this method tries to fill its internal buffer up first, which only works reliably if the input stream actually has an end. With STDIN that isn't the case unless the parent process closes STDIN. To work around this, you have to set
This then behaves as you'd expect: it reads until the input until the newline and in this case (due to Thinking about it, the use of In short, what we should do is this:
|
This solution didn't work for me. However, I managed with using the read() function like this -- which is not a safe solution, but kept my work flow going forward:
|
@PLC-Programmer The supplied snippet relies on changes not yet released, sorry for not making that more clear. Your snippet itself isn't sufficient: all If possible I recommend using the |
This solution works like a charm as a work around:
But it's not safe. When the user gets the idea - and depending on a program different from the shown above - to enter more than 10,000 "characters", dangerous things can happen. |
@PLC-Programmer It largely works by coincidence due to how the Enter key is handled in interactive terminal sessions, but this won't be the case when e.g. piping input to STDIN. For example, if you pipe Either way, for the purpose of just playing around your hack might be sufficient, though I still recommend using |
Description
Hi,
I have a feature request: a readline() function would be helpful (for me) for reading into a ByteArray until a newline is entered by the user:
See with Scala for example: String = scala.io.StdIn.readLine() --> "Ask For User Input": https://docs.scala-lang.org/scala3/book/taste-hello-world.html#ask-for-user-input
or in Rust for example: std::io::stdin().read_line(&mut String) --> https://doc.rust-lang.org/std/io/struct.Stdin.html#method.read_line
I also like the Swift implementation, where I can set a flag to get rid of the final newline character or not:
func readLine(strippingNewline: Bool = true) -> String? --> https://developer.apple.com/documentation/swift/readline(strippingnewline:)
Related work
No response
The text was updated successfully, but these errors were encountered: