3

I am using jline and I have a neat ConsoleReader and everything works great. However, if you are typing something into the the prompt and there is output on stdout (from another thread), the output splits the word/command that you are typing.

How can I keep the jline prompt at the bottom of the terminal?

I am using jline 1, but I am open to using jline 2 if it is stable enough.

Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
  • The library is purportedly design to handle console input; I don't see how it can handle console output. The problem is that console I/O events are asynchronous (in your case). Maybe, you need to store the console output in a temporary buffer while performing the input and display the output from the buffer once the input has been done. – ee. Jan 26 '12 at 04:41
  • Maybe you need to look for Curses library for Java http://sourceforge.net/projects/javacurses/, http://sourceforge.net/projects/enigma-shell/, or http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html which allows you to reposition your console input/output cursor – ee. Jan 26 '12 at 05:04

1 Answers1

6

Finally figured this out... here's what you do. First, define these functions:

private ConsoleReader console = ...;
private CursorBuffer stashed;

private void stashLine() {
    this.stashed = this.console.getCursorBuffer().copy();
    try {
        this.console.getOutput().write("\u001b[1G\u001b[K");
        this.console.flush();
    } catch (IOException e) {
        // ignore
    }
}

private void unstashLine() {
    try {
        this.console.resetPromptLine(this.console.getPrompt(),
          this.stashed.toString(), this.stashed.cursor);
    } catch (IOException e) {
        // ignore
    }
}

Then when you want to output new data, first invoke stashLine() to save the current console input, then output whatever new lines of output, then invoke unstashLine() to restore it.

Archie
  • 4,959
  • 1
  • 30
  • 36