Java命令行界面:使用'\r'在不同的行上具有多个进度条 [英] Java command line interface: having multiple progress bars on different lines using '\r'
问题描述
我正在编写的程序的命令行界面的一部分,要求多个进度条。我目前可以使用 \r
转义序列在控制台中更新一行,类似于以下内容:
Part of the command line interface for a program I'm writing calls for multiple progress bars. I can currently update a single line in the console by using the \r
escape sequence with something similar to this:
System.out.printf("\rProcess is %d%% complete", percentageComplete);
不过,回车只返回到该行的开头。我想要一种退回两行(或更普遍地说,任意数量的行)并同时更新它们的方法。
However the carriage return only goes back to the start of that line. I want a way of going back two lines (or more generally, any number of lines) and have them both/all update.
有什么办法吗?
推荐答案
我为命令行进度条编写了一个小项目,该项目可以执行一个划线员或主/细节操作-参见 https://github.com/tomas-langer/cli/tree/ master / cli-progress 。它也可以在Windows上使用-将ANSI转义序列与MS Windows的本机实现一起使用(Chalk + Jansi)
I have written a small project for command line progress bars that can do either one liners or a "master/detail" - see https://github.com/tomas-langer/cli/tree/master/cli-progress. It works also on Windows - using ANSI escape sequences with native implementation for MS Windows (Chalk + Jansi)
如果您想做更多的事情,请检查Chalk库( https://github.com/tomas-langer/chalk ),而后者又使用了Jansi(在前面已经提到过)
If you want to do more, check the Chalk library (https://github.com/tomas-langer/chalk) that in turn uses Jansi (mentioned already in previous posts).
排队和清除行的ansi转义代码在Chalk库中。
要使用它们:
The ansi escape code for line up and clear line are in Chalk library. To use them:
import com.github.tomaslanger.chalk.Ansi;
...
System.out.print(Ansi.cursorUp(2)); //move cursor up two lines
System.out.print(Ansi.eraseLine()); //erase current line
这篇关于Java命令行界面:使用'\r'在不同的行上具有多个进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!