使用Java在终端中替换两行 [英] Replacing two lines in the terminal using Java
问题描述
我知道如何使用Java通过替换上一行来将终端中的输出包含在一行中.例如:
I know how to use Java to have output in the terminal be contained in one line by replacing the previous line. For example:
System.out.print("old line");
Thread.sleep(3000);
System.out.print("\rnew line");
但是如果我有两行输出并且我想都替换掉
But what if I have two lines of output and I want both replaced
/* array must have length divisible by 2 */
public void doPrinting(String[] array) {
for(int i = 0; i < array.length / 2; i++) {
System.out.println(array[i*2]);
System.out.println(array[i*2+1]);
Thread.sleep(3000);
//I imagine some fancy ascii crypticness shall go here?
}
因此,如果我呼叫doPrinting(new String[]{"Old1", "Old2", "New1", "New2"})
,则在Mac终端中应该看起来像这样:
So, if I call doPrinting(new String[]{"Old1", "Old2", "New1", "New2"})
, this should look like, in the Mac terminal:
MyName$ java MyClass
Old1
Old2
然后暂停3秒钟,然后看起来像
and then pause for 3 seconds, and then look like
MyName$ java MyClass
New1
New2
我该怎么做?我在Mac OS上,并且正在通过终端运行Java主方法.理想的答案是添加代码以修改上面的doPrinting
方法.
How can I do this? I'm on Mac OS and am running my Java main method through the terminal. The ideal answer would add code to modify the doPrinting
method above.
更新:从下面回答一个之后,我尝试了此操作:
UPDATE: After one answer from below, I tried this:
String ANSI_CSI = "\\x1b[";
String[] array = {"old1","old2","new1","new2"};
System.out.print("\n\n");
for(int i = 0; i < array.length / 2; i++) {
System.out.print(ANSI_CSI + "2A"); // Up 2
System.out.print(ANSI_CSI + "K"); // Clear
System.out.println(array[i*2]);
System.out.print(ANSI_CSI + "K"); // Clear
System.out.println(array[i*2+1]);
Thread.sleep(3000);
}
但这只是在终端上给我的输出
but that just gives me this output in the terminal
MyName$ MyClass
\x1b[2A\x1b[Kold1
\x1b[Kold2
\x1b[2A\x1b[Knew1
\x1b[Knew2
推荐答案
您将要使用 ANSI转义代码.
在这里,您可能会对向上移动光标"(CSI-A
)和擦除行"(CSI-K
)感兴趣.
Here, you'll probably be interested in "Move Cursor Up" (CSI-A
) and "Erase line" (CSI-K
).
// outside code
public static final String ANSI_CSI = "\x1b[";
// inside code
System.out.println("First line of text");
System.out.println("[ 5/365]");
System.out.print(ANSI_CSI + "2A"); // up 2 lines
System.out.println("Different first line of text");
System.out.println("[ 11/365)");
System.out.print(ANSI_CSI + "A"); // up line
System.out.print(ANSI_CSI + "2K"); // erase all of line
System.out.print(ANSI_CSI + "A"); // up line
System.out.print(ANSI_CSI + "K"); // erase after cursor
System.out.println("Line one");
System.out.println("[240/365]");
如果要循环执行此操作(为简便起见,省略了System.out
):
If you want to do this in a loop (System.out
has been omitted for brevity):
// Init to safe state
print("\n\n");
for (loop conditions here) {
print(ANSI_CSI + "2A"); // Up 2
print(ANSI_CSI + "K"); // Clear
println(text_a); // Print + newline
print(ANSI_CSI + "K"); // Clear
println(text_b); // Print + newline
Thread.sleep(3000); // Wait
}
请注意,您将需要使用支持这些功能的终端.查看Wikipedia文章以获取更多信息.如果它说某个代码在"ANSI.SYS"下具有不同的行为,则适用于默认的Windows命令提示符.
Note that you're going to need to use a terminal that supports these. Check the Wikipedia article for more information. If it says that a certain code has different behavior under "ANSI.SYS", then that applies to the default Windows command prompt.
我对Mac没有任何经验,因此您的体验可能会因不同的终端而异.
I don't have any experience with Macs, so your experience may vary with different terminals.
大多数备用终端和UNIX终端(gnome-terminal,PuTTY)更全面地支持这些代码(请参阅有关xterm
的完全RGB支持的注释).
Most alternate and UNIX terminals (gnome-terminal, PuTTY) support these codes more fully (see the notes about xterm
's full RGB support).
这篇关于使用Java在终端中替换两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!