jline keep prompt at the bottom

百般思念 提交于 2019-12-21 02:58:04

问题


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:


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.



来源:https://stackoverflow.com/questions/9010099/jline-keep-prompt-at-the-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!