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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With