Java 更新命令行应用程序状态
我有一个命令行应用程序,当前打印增加的百分比 1% 2% 3% 4% 输出是连续的。但我看到过命令行工具,它们显示的更改就像是内联更新一样 1% 与第一个相同位置的2% 与第一个相同位置的3% 与第一个位置相同的4% 我怎样才能做到这一点?如果有必要的话,我正在使用Java,应用程序将在linux上运行 多谢各位 编辑Java 更新命令行应用程序状态,java,bash,Java,Bash,我有一个命令行应用程序,当前打印增加的百分比 1% 2% 3% 4% 输出是连续的。但我看到过命令行工具,它们显示的更改就像是内联更新一样 1% 与第一个相同位置的2% 与第一个相同位置的3% 与第一个位置相同的4% 我怎样才能做到这一点?如果有必要的话,我正在使用Java,应用程序将在linux上运行 多谢各位 编辑 如何使用颜色?在打印更新之前输出一个\r,然后刷新。这会将光标返回到第一列。这篇文章很老了,但对于那些不想使用的人,我可以同时进行控制台重写和ANSI着色。代码在Scala中,
如何使用颜色?在打印更新之前输出一个
\r
,然后刷新。这会将光标返回到第一列。这篇文章很老了,但对于那些不想使用的人,我可以同时进行控制台重写和ANSI着色。代码在Scala中,但如果JAR在类路径上,则可以从Java调用代码;源代码中有一个,但它在下面复制。输出已在OSX和Ubuntu上验证
public class JavaConsoleDemo {
private final ConsoleWriter screen;
public JavaConsoleDemo(ConsoleWriter screen) {
this.screen = screen;
}
public void rewriteWithColors(String name) throws Exception {
screen.clearAll();
screen
.print(Red().and(BgYellow()).format("Hello"))
.print(" ")
.print(Yellow().format("World"));
screen.display();
Thread.sleep(1000);
screen.clearAll();
screen
.print(Blue().and(BgWhite()).format("Hello"))
.print(" ")
.print(Magenta().and(BgBlue()).format("%s", name));
screen.display();
Thread.sleep(1000);
}
public void rewrite(String name) throws Exception {
screen.clearAll();
screen.print("Hello\nWorld");
screen.display();
Thread.sleep(1000);
screen.clearAll();
screen.print("Hello\n" + name);
screen.display();
Thread.sleep(1000);
}
public static void main(final String[] args) throws Exception {
ConsoleWriter screen = new ConsoleWriter(System.out);
JavaConsoleDemo demo = new JavaConsoleDemo(screen);
demo.rewrite(args[0]);
demo.rewriteWithColors(args[0]);
}
}
非常感谢。那太完美了。?我如何使用颜色?@Nerlan:jCourses看起来是terminfo库的一个不错的入口。