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!