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

JLine3 "steals" input from other program #138

Closed
hyee opened this issue Jun 24, 2017 · 7 comments
Closed

JLine3 "steals" input from other program #138

hyee opened this issue Jun 24, 2017 · 7 comments

Comments

@hyee
Copy link
Contributor

hyee commented Jun 24, 2017

It is possible that an application integrates Jline3 together with other program which uses its own read user input method. In such case, I found that Jline3 always "steals" the first character for each input line.

With below test code(in native console window), try input following lines:

  • 11
  • 11
  • 11
  • qq

And then the result likes below, the first char is eaten:

    Input: 1
    Input: 1
    Input: 1
    Input: q

Test code:

import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
public class TestInput {
    public static void main(String[] args) throws Exception {
        Terminal terminal=TerminalBuilder.builder().system(true).exec(true).jansi(true).build();
        boolean exit=false;
        while (!exit) {
            System.out.write("Input: ".getBytes());
            while(true) {
                int c = System.in.read(); //Don't use JLine3 as the input reader
                if(c==10) break;
                if((char)c=='q') {
                    exit=true;
                    break;
                }
            }
        }
    }
}
@hyee hyee changed the title JLines "steals" input from other program JLine3 "steals" input from other program Jun 24, 2017
@gnodet
Copy link
Member

gnodet commented Jun 25, 2017

Yes, you're supposed to use the terminal.reader().

@gnodet
Copy link
Member

gnodet commented Jun 25, 2017

The reason is that signals have to be processed as soon as possible, so that the input stream is read in advanced.
If you use terminal.reader(), it will work correctly. You can also set System.in() to actually return terminal.input() in case you can't change the code. That's what is done in the demo actually, so that all access to System.in() / out() / err() delegates to the current terminal, even if you have multiple incoming connections through ssh for example.

@hyee
Copy link
Contributor Author

hyee commented Jun 26, 2017

Thank you for your comment, actually the program is a C library, I think I can shutdown/run the reader before/after calling the relative C interface.

@hyee hyee closed this as completed Jun 26, 2017
@hyee
Copy link
Contributor Author

hyee commented Jun 26, 2017

Maybe the terminal reader can be temoparily paused during the enter key is pressed and before new line request is raised

@gnodet
Copy link
Member

gnodet commented Jun 26, 2017

Yeah, we could add a two methods to the terminal to pause/resume the input stream processing.
If you can't get around the problem, please reopen this or another issue.

@hyee
Copy link
Contributor Author

hyee commented Jun 27, 2017

That would be nice, thanks, previously I tested System.setIn to redirect the input of the C program, it didn't work so well.

@gnodet
Copy link
Member

gnodet commented Jun 27, 2017

I've raised #140 and #141. I may be able to work on #141, but #140 is a bigger issue and I won't have the bandwidth in the short term.

@gnodet gnodet closed this as completed Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants