How can I color Java output?
For example in C and other languages I can use ANSI-escape like \033[0m to do this. But in Java it doesn't work.
public static void main(String[] x) {
System.out.println("\033[0m BLABLA \033[0m\n");
}
How can I color Java output?
For example in C and other languages I can use ANSI-escape like \033[0m to do this. But in Java it doesn't work.
public static void main(String[] x) {
System.out.println("\033[0m BLABLA \033[0m\n");
}
I created a jar library called JCDP (Java Colored Debug Printer).
For Linux it uses the ANSI escape codes that WhiteFang mentioned, but abstracts them using words instead of codes which is much more intuitive.
For Windows it actually includes the JAnsi library but creates an abstraction layer over it, maintaining the intuitive and simple interface created for Linux.
This library is licensed under the MIT License so feel free to use it.
Have a look at JCDP's github repository.
I've written a library called AnsiScape that allows you to write coloured output in a more structured way:
Example:
AnsiScape ansiScape = new AnsiScape();
String colors = ansiScape.format("{red {blueBg Red text with blue background}} {b Bold text}");
System.out.println(colors);
The library it also allows you to define your own "escape classes" akin to css classes.
Example:
AnsiScapeContext context = new AnsiScapeContext();
// Defines a "class" for text
AnsiClass text = AnsiClass.withName("text").add(RED);
// Defines a "class" for the title used
AnsiClass title = AnsiClass.withName("title").add(BOLD, BLUE_BG, YELLOW);
// Defines a "class" to render urls
AnsiClass url = AnsiClass.withName("url").add(BLUE, UNDERLINE);
// Registering the classes to the context
context.add(text).add(title).add(url);
// Creating an AnsiScape instance with the custom context
AnsiScape ansiScape = new AnsiScape(context);
String fmt = "{title Chapter 1}\n" +
"{text So it begins:}\n" +
"- {text Option 1}\n" +
"- {text Url: {url www.someurl.xyz}}";
System.out.println(ansiScape.format(fmt));