1

I was wondering if it was possible to colorize the output in IntelliJ's run console from within my Java code. For example, if I have something like

System.out.println("Error: " + message);

I would like to display the "Error" in red and the rest in a different color. Alternatively, the whole line as one color would be fine as well and already a big improvement over having everything in one color.

Thank you in advance!

Edit: So the answer, thanks to a kind redditor, was a link back here Stack Overflow - List of ANSI color escape sequences .

While I don't think I'm fully there (IntelliJ gives more color options than I was able to get to work), I was able to get 8 colors to work. I have now created a little helper function to easily print text to the console:

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());
}

Maybe it's not the most efficient and you have suggestions to improve this, but for now I'm happy that it works!

Markstar
  • 345
  • 4
  • 17
  • 1
    In case the author was asking about highlighting errors, I don't agree it's a duplicate... – Cargeh Apr 28 '18 at 12:12
  • Sadly, the linked answer doesn't really help me. Besides the last answer being 4 years old, I still have no idea which is correct for my case and how to actually do this. I had actually seen this before posting and found the setting **Preferences > Editor > Colors & Fonts > Console Colors** (if this is what I'm supposed to use, I don't know), but I still have no idea how make this work. – Markstar Apr 28 '18 at 19:11
  • Duplicate of what? Why close an issue with duplicate without a reference to the issue duplicated? – Al-Punk Oct 10 '18 at 10:39

1 Answers1

2

If you want to highlight specifically errors, then there's a different system output that is highlighted in red in Intellij IDEA's console.

System.err.println("This line will be red");

However, System.err can be out of sync with System.out, read about it here.

Cargeh
  • 939
  • 7
  • 17
  • While I was looking for more than just error messages, this is a start. So thank you! However, I'm still hoping to get it to work that I also can have green or yellow lines/text passages. – Markstar Apr 28 '18 at 19:14