Java 如何让退格 \b 在 Eclipse 的控制台中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3095986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:25:40  来源:igfitidea点击:

How to get backspace \b to work in Eclipse's console?

javaeclipsebackspace

提问by BalusC

I'm creating a little Java application which should have a progress indicator with percentages. In every loop it uses backspace \bto remove the displayed progress before displaying the next percentage.

我正在创建一个小的 Java 应用程序,它应该有一个带有百分比的进度指示器。在每个循环中,它\b在显示下一个百分比之前使用退格键删除显示的进度。

Here's a simplified example:

这是一个简化的示例:

public static void main(String[] args) throws Exception {
    System.out.print("Progress: ");
    for (int percentage = 0; percentage < 100; percentage++) {
        System.out.print(percentage + "%");
        Thread.sleep(10); // Stub for "long running task".
        int length = String.valueOf(percentage).length() + 1;
        while (length-- > 0) {
            System.out.print('\b');
        }
    }
    System.out.println("finished!");
}

This works perfectly in command prompt, but the backspace character isn't recognized in Eclipse's console (Galileo build 20090920-1017). It instead displays an empty square denoting an unknown character. See screenshot:

这在命令提示符下完美运行,但在 Eclipse 的控制台(Galileo build 20090920-1017)中无法识别退格字符。而是显示一个表示未知字符的空方块。看截图:

alt text

替代文字

How do I get Eclipse to "display" the backspace properly? I.e. let it remove the previous character.

如何让 Eclipse 正确“显示”退格键?即让它删除前一个字符。

This is actually no showstopper since it will just be run in command console, but it would be just nice to get it to work in Eclipse as well :)

这实际上不是什么showstopper,因为它只会在命令控制台中运行,但是让它在Eclipse中也能工作会很好:)

采纳答案by Mark Peters

Eclipse Bug #76936.I wouldn't count on them to fix it, and there are no workarounds listed.

Eclipse 错误 #76936。我不会指望他们来修复它,并且没有列出解决方法。

You might have luck finding a plugin that contributes a more advanced console.

您可能很幸运地找到了一个可以提供更高级控制台的插件。

回答by EMurnane

Well, it's true you can't use backspace \b to remove the displayed progress, but you could remove it by clearing the console with a loop calling println. Of course this kluge won't clear your log file!

好吧,确实您不能使用退格键 \b 来删除显示的进度,但是您可以通过调用 println 的循环清除控制台来删除它。当然,这个 kluge 不会清除您的日志文件!

回答by gagarwa

Fixed, Eclipse Mars.

固定,日食火星。

Note, I wouldn't use it to do constant updating, as the eclipse console lags.

请注意,我不会使用它来进行持续更新,因为 Eclipse 控制台滞后。

回答by Fisho2008

use: System.out.print("\b ") inside the while loop, instead of System.out.print('\b');

在while循环中使用:System.out.print("\b"),而不是System.out.print('\b');

回答by Павел

Now they fixed it, but it's disabled by default. You should enable it via Interpret ASCII control charactersin console preferences.

现在他们修复了它,但默认情况下它是禁用的。您应该通过Interpret ASCII control characters在控制台首选项中启用它。