jline keep prompt at the bottom

2019-03-22 06:54发布

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.

1条回答
仙女界的扛把子
2楼-- · 2019-03-22 07:14

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.

查看更多
登录 后发表回答