我使用的是jline,我有一个整洁的ConsoleReader,一切都运行得很好。但是,如果您在提示符中键入某些内容,并且在stdout上有输出(来自另一个线程),则输出将拆分您正在键入的单词/命令。
如何将jline提示符保留在终端底部?
我正在使用jline 1,但如果jline 2足够稳定,我对使用它持开放态度。
发布于 2014-12-10 06:16:15
终于想明白了..。这就是你要做的。首先,定义以下函数:
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
}
}然后,当您想要输出新数据时,首先调用stashLine()来保存当前的控制台输入,然后输出新的输出行,然后调用unstashLine()来恢复它。
https://stackoverflow.com/questions/9010099
复制相似问题