如何在 IntelliJ 运行控制台中使用颜色?

Mar*_*tar 3 java intellij-idea

我想知道是否可以从我的 Java 代码中对 IntelliJ 运行控制台中的输出进行着色。例如,如果我有类似的东西

System.out.println("Error: " + message);
Run Code Online (Sandbox Code Playgroud)

我想将“错误”显示为红色,其余部分显示为不同的颜色。或者,整个系列作为一种颜色也很好,并且比所有东西都采用一种颜色已经是一个很大的改进。

先感谢您!

编辑:感谢一位好心的 redditor,答案是这里的链接Stack Overflow - ANSI 颜色转义序列列表

虽然我认为我还没有完全做到这一点(IntelliJ 提供了比我能够工作的更多的颜色选项),但我能够获得 8 种颜色来工作。我现在创建了一个小辅助函数,可以轻松地将文本打印到控制台:

public static void colorSystemOut(String text, Color color, 
                                    boolean bold, boolean underlined) {
    StringBuilder cString = new StringBuilder("\033[");
    if(color == Color.WHITE) {
        cString.append("30");
    }
    else if(color == Color.RED) {
        cString.append("31");
    }
    else if(color == Color.GREEN) {
        cString.append("32");
    }
    else if(color == Color.YELLOW) {
        cString.append("33");
    }
    else if(color == Color.BLUE) {
        cString.append("34");
    }
    else if(color == Color.MAGENTA) {
        cString.append("35");
    }
    else if(color == Color.CYAN) {
        cString.append("36");
    }
    else if(color == Color.GRAY) {
        cString.append("37");
    }
    else {
        cString.append("30");
    }
    if(bold) { cString.append(";1"); }
    if(underlined) { cString.append(";4"); }
    cString.append(";0m" + text + "\033[0m");
    System.out.print(cString.toString());
}
Run Code Online (Sandbox Code Playgroud)

也许这不是最有效的,您有改进的建议,但现在我很高兴它有效!

Car*_*geh 5

如果您想突出显示特定错误,那么 Intellij IDEA 的控制台中会有一个不同的系统输出以红色突出显示。

System.err.println("This line will be red");
Run Code Online (Sandbox Code Playgroud)

但是,System.err可能与 不同步System.out请在此处阅读