-
Notifications
You must be signed in to change notification settings - Fork 182
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
non-blocking console input #58
Comments
Do you think you could propose a patch for this issue ? |
Yes, I could propose a patch. I'm not sure which is the best way to implement it. In NonBlockingInputStream.read(long timeout, boolean isPeek) the value 0 for the timeout parameter is already used to wait forever. We could use an additional boolean parameter e.g. noWait, or e.g. the special value Long.MIN_VALUE for zero timeout. I have no experience with Git. What is the best way to propose a patch? Do I have to fork the jline2 repository, make the changes and send a pull request? |
Just so you know, the NonBlockInputStream does use a separate thread to do On Thu, Sep 27, 2012 at 6:00 PM, Christian d'Heureuse <
|
NonBlockInputStream (with nonBlockingEnabled=true) is already used now within ConsoleReader, even if STDIN is redirected from a file (redirecting STDIN works on Linux, but has no effect on Windows). But I found another problem. The data that passes through the NonBlockInputStream is a byte stream and the character set decoding (e.g. from UTF-8 to Java char Unicode) is done outside in the InputStreamReader. So to implement ConsoleReader.readCharacterNoWait() we need a NonBlockingReader, which would add another thread to the console input chain. The negative effect could be reduced if the NonBlockingReader is inserted only when readCharacterNoWait() is called for the first time. |
Yes, I'm sorry, what I meant to say was that if you actually request Also, note that while NonBlockInputStream is always used, it isn't always Because the NBInputStream is an InputStream, it has no concept of a -scot On Fri, Sep 28, 2012 at 6:16 AM, Christian d'Heureuse <
|
Scott, thanks for your explanation. I now understand that the thread in NonBlockInputStream is bypassed when a blocking read() is used. So there could be two possible implementations of ConsoleReader.readCharacterNoWait(), if it has to be really non-blocking and character set conversion is done right:
A problem with version 1 is that in the current implementation of ConsoleReader, the NonBockingInputStream is only set into non-blocking mode when escapeTimeout > 0 && terminal.isSupported(). This could be solved by adding a setNonBlockingEnabled() method to NonBockingInputStream. It would be used to enable non-blocking mode afterwards and start the internal thread when readCharacterNoWait() is called for the first time. |
I have written a Java class for non-blocking console input: It's based on JNA. It uses a CharsetDecoder to decode UTF-8 and other encodings and reads only as much bytes from the console input stream at once as are necessary to decode a single character. No thread is necessary for non-blocking input. Maybe the same techniques could be used in JLine. |
Thank you for the RawConsoleInput script. Is it possible to modify it to enable echo as well as read the input without hitting enter? |
Please implement non-blocking console input, e.g. ConsoleReader.readCharacterNoWait(). It would only require a minor change in NonBlockingInputStream and ConsoleReader.
I'm writing a console-mode communications tool, where I have to read from the console without blocking. I know that I could use a separate thread to do that.
I tried the following code:
But: 1. i'ts not guaranteed that future versions of ConsoleReader.getInput() will return a NonBlockingInputStream and 2. it blocks 1ms if no keyboard input is ready.
The text was updated successfully, but these errors were encountered: