How to print color in console using System.out.println?
How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.
java text colors
add a comment |
How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.
java text colors
add a comment |
How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.
java text colors
How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.
java text colors
java text colors
edited Jul 24 '17 at 4:06
CodingNinja
1,2381724
1,2381724
asked Apr 23 '11 at 5:52
Taranath Datta
1,147394
1,147394
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
3
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
1
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@PankajNimgade, read the answer again and you'll maybe notice this:however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
|
show 4 more comments
I created a 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. It becomes as easy as:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
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.
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
add a comment |
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note
Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = "33[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "33[0;30m"; // BLACK
public static final String RED = "33[0;31m"; // RED
public static final String GREEN = "33[0;32m"; // GREEN
public static final String YELLOW = "33[0;33m"; // YELLOW
public static final String BLUE = "33[0;34m"; // BLUE
public static final String PURPLE = "33[0;35m"; // PURPLE
public static final String CYAN = "33[0;36m"; // CYAN
public static final String WHITE = "33[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "33[1;30m"; // BLACK
public static final String RED_BOLD = "33[1;31m"; // RED
public static final String GREEN_BOLD = "33[1;32m"; // GREEN
public static final String YELLOW_BOLD = "33[1;33m"; // YELLOW
public static final String BLUE_BOLD = "33[1;34m"; // BLUE
public static final String PURPLE_BOLD = "33[1;35m"; // PURPLE
public static final String CYAN_BOLD = "33[1;36m"; // CYAN
public static final String WHITE_BOLD = "33[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "33[4;30m"; // BLACK
public static final String RED_UNDERLINED = "33[4;31m"; // RED
public static final String GREEN_UNDERLINED = "33[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "33[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "33[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "33[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "33[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "33[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "33[40m"; // BLACK
public static final String RED_BACKGROUND = "33[41m"; // RED
public static final String GREEN_BACKGROUND = "33[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "33[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "33[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "33[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "33[46m"; // CYAN
public static final String WHITE_BACKGROUND = "33[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "33[0;90m"; // BLACK
public static final String RED_BRIGHT = "33[0;91m"; // RED
public static final String GREEN_BRIGHT = "33[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "33[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "33[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "33[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "33[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "33[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "33[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "33[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "33[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "33[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "33[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "33[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "33[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "33[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "33[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "33[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "33[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "33[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "33[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "33[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "33[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "33[0;107m"; // WHITE
}
add a comment |
A fairly portable way of doing it is with the raw escape sequences. See http://en.wikipedia.org/wiki/ANSI_escape_code
[edited for user9999999 on 2017-02-20]
Java doesn't "handle the codes", that's true, but Java outputs what you told it to output. it's not Java's fault that the Windows console treats ESC (chr(27)) as just another glyph (←).
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
the question wasn't taggedwindows
. the Windows console was never ANSI-compliant that I remember.
– jcomeau_ictx
Feb 12 '17 at 14:52
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
2
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
I have same problem
– sgrillon
Mar 15 at 14:58
|
show 1 more comment
public enum Color {
//颜色结尾字符串,重置颜色的
RESET("33[0m"),
// Regular Colors 普通颜色,不带加粗,背景色等
BLACK("33[0;30m"), // BLACK
RED("33[0;31m"), // RED
GREEN("33[0;32m"), // GREEN
YELLOW("33[0;33m"), // YELLOW
BLUE("33[0;34m"), // BLUE
MAGENTA("33[0;35m"), // MAGENTA
CYAN("33[0;36m"), // CYAN
WHITE("33[0;37m"), // WHITE
// Bold
BLACK_BOLD("33[1;30m"), // BLACK
RED_BOLD("33[1;31m"), // RED
GREEN_BOLD("33[1;32m"), // GREEN
YELLOW_BOLD("33[1;33m"), // YELLOW
BLUE_BOLD("33[1;34m"), // BLUE
MAGENTA_BOLD("33[1;35m"), // MAGENTA
CYAN_BOLD("33[1;36m"), // CYAN
WHITE_BOLD("33[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("33[4;30m"), // BLACK
RED_UNDERLINED("33[4;31m"), // RED
GREEN_UNDERLINED("33[4;32m"), // GREEN
YELLOW_UNDERLINED("33[4;33m"), // YELLOW
BLUE_UNDERLINED("33[4;34m"), // BLUE
MAGENTA_UNDERLINED("33[4;35m"), // MAGENTA
CYAN_UNDERLINED("33[4;36m"), // CYAN
WHITE_UNDERLINED("33[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("33[40m"), // BLACK
RED_BACKGROUND("33[41m"), // RED
GREEN_BACKGROUND("33[42m"), // GREEN
YELLOW_BACKGROUND("33[43m"), // YELLOW
BLUE_BACKGROUND("33[44m"), // BLUE
MAGENTA_BACKGROUND("33[45m"), // MAGENTA
CYAN_BACKGROUND("33[46m"), // CYAN
WHITE_BACKGROUND("33[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("33[0;90m"), // BLACK
RED_BRIGHT("33[0;91m"), // RED
GREEN_BRIGHT("33[0;92m"), // GREEN
YELLOW_BRIGHT("33[0;93m"), // YELLOW
BLUE_BRIGHT("33[0;94m"), // BLUE
MAGENTA_BRIGHT("33[0;95m"), // MAGENTA
CYAN_BRIGHT("33[0;96m"), // CYAN
WHITE_BRIGHT("33[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("33[1;90m"), // BLACK
RED_BOLD_BRIGHT("33[1;91m"), // RED
GREEN_BOLD_BRIGHT("33[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("33[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("33[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("33[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("33[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("33[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("33[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("33[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("33[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("33[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("33[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("33[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("33[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("33[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
System.out.print(Color.BLACK_BOLD);
System.out.println("111111111aaaaaaaaaaaaaaaa==============");
System.out.print(Color.RESET);
System.out.print(Color.BLUE_BACKGROUND);
System.out.print(Color.YELLOW); //设置前景色 为YELLOW
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.print(Color.RESET);
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
add a comment |
You could do this using ANSI escape sequences. I've actually put together this class in Java for anyone that would like a simple workaround for this. It allows for the use of custom color codes in text.
https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a
Example Use:
Color code format WITH background color -> :foreground,background:
Color code format WITHOUT background color -> :foreground,N:
Reset Color format -> [RC]
String ansiColoredString = ColorCodes.parseColors("Hello, This :blue,n:is[RC] a :red,white:response[RC].");
or
String ansiColoredString = ColorCodes.RED + "Hello" + ColorCodes.WHITE + ", This is a " + ColorCodes.BLUE + "test";
add a comment |
If anyone is looking for a quick solution, feel free to use the following helper class :)
public class Log {
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
//info
public static void i(String className, String message) {
System.out.println(ANSI_GREEN + className + " : " + message + ANSI_RESET);
}
//error
public static void e(String className, String message) {
System.out.println(ANSI_RED + className + " : " + message + ANSI_RESET);
}
//debug
public static void d(String className, String message) {
System.out.println(ANSI_BLUE + className + " : " + message + ANSI_RESET);
}
//warning
public static void w(String className, String message) {
System.out.println(ANSI_YELLOW + className + " : " + message + ANSI_RESET);
}
}
USAGE:
Log.i(TAG,"This is an info message");
Log.e(TAG,"This is an error message");
Log.w(TAG,"This is a warning message");
Log.d(TAG,"This is a debug message");
Thanks @whiteFang34 for the ANSI codes.
add a comment |
protected by davidkonrad Dec 11 '17 at 16:54
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
3
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
1
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@PankajNimgade, read the answer again and you'll maybe notice this:however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
|
show 4 more comments
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
3
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
1
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@PankajNimgade, read the answer again and you'll maybe notice this:however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
|
show 4 more comments
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
edited Nov 3 '17 at 13:43
SergeyB
5,35112542
5,35112542
answered Apr 23 '11 at 5:56
WhiteFang34
58.1k1688106
58.1k1688106
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
3
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
1
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@PankajNimgade, read the answer again and you'll maybe notice this:however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
|
show 4 more comments
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
3
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
1
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@PankajNimgade, read the answer again and you'll maybe notice this:however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
– Boro
Apr 23 '11 at 8:31
3
3
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
– WhiteFang34
Apr 23 '11 at 8:38
1
1
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:
if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
jansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:
if (System.console() == null) System.setProperty("jansi.passthrough", "true");
– Danny Lo
Apr 13 '14 at 19:51
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@WhiteFang34 i am using windows and for some reason it is not working
– Pankaj Nimgade
Feb 5 '15 at 10:32
@PankajNimgade, read the answer again and you'll maybe notice this:
however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
@PankajNimgade, read the answer again and you'll maybe notice this:
however it doesn't work for Windows command prompt
– Felix Edelmann
Sep 2 '15 at 15:23
|
show 4 more comments
I created a 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. It becomes as easy as:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
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.
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
add a comment |
I created a 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. It becomes as easy as:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
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.
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
add a comment |
I created a 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. It becomes as easy as:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
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 created a 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. It becomes as easy as:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
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.
edited Jul 11 '16 at 9:07
answered Aug 5 '11 at 14:03
dialex
1,37632660
1,37632660
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
add a comment |
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
there's also a typo, dynamic is spelled dinamic in one of the bullets.
– invisible bob
Jan 14 '12 at 18:37
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
@DiAlex I'd love if I could get a hold of a copy of your API. The link on the home page is no longer functioning and is instead resulting in a "404". Anywhere I could get a download?
– Nathan Fiscaletti
Jan 5 '15 at 6:09
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
Solved, thanks for letting me know
– dialex
Dec 29 '17 at 9:17
add a comment |
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note
Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = "33[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "33[0;30m"; // BLACK
public static final String RED = "33[0;31m"; // RED
public static final String GREEN = "33[0;32m"; // GREEN
public static final String YELLOW = "33[0;33m"; // YELLOW
public static final String BLUE = "33[0;34m"; // BLUE
public static final String PURPLE = "33[0;35m"; // PURPLE
public static final String CYAN = "33[0;36m"; // CYAN
public static final String WHITE = "33[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "33[1;30m"; // BLACK
public static final String RED_BOLD = "33[1;31m"; // RED
public static final String GREEN_BOLD = "33[1;32m"; // GREEN
public static final String YELLOW_BOLD = "33[1;33m"; // YELLOW
public static final String BLUE_BOLD = "33[1;34m"; // BLUE
public static final String PURPLE_BOLD = "33[1;35m"; // PURPLE
public static final String CYAN_BOLD = "33[1;36m"; // CYAN
public static final String WHITE_BOLD = "33[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "33[4;30m"; // BLACK
public static final String RED_UNDERLINED = "33[4;31m"; // RED
public static final String GREEN_UNDERLINED = "33[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "33[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "33[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "33[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "33[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "33[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "33[40m"; // BLACK
public static final String RED_BACKGROUND = "33[41m"; // RED
public static final String GREEN_BACKGROUND = "33[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "33[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "33[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "33[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "33[46m"; // CYAN
public static final String WHITE_BACKGROUND = "33[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "33[0;90m"; // BLACK
public static final String RED_BRIGHT = "33[0;91m"; // RED
public static final String GREEN_BRIGHT = "33[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "33[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "33[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "33[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "33[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "33[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "33[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "33[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "33[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "33[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "33[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "33[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "33[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "33[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "33[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "33[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "33[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "33[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "33[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "33[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "33[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "33[0;107m"; // WHITE
}
add a comment |
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note
Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = "33[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "33[0;30m"; // BLACK
public static final String RED = "33[0;31m"; // RED
public static final String GREEN = "33[0;32m"; // GREEN
public static final String YELLOW = "33[0;33m"; // YELLOW
public static final String BLUE = "33[0;34m"; // BLUE
public static final String PURPLE = "33[0;35m"; // PURPLE
public static final String CYAN = "33[0;36m"; // CYAN
public static final String WHITE = "33[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "33[1;30m"; // BLACK
public static final String RED_BOLD = "33[1;31m"; // RED
public static final String GREEN_BOLD = "33[1;32m"; // GREEN
public static final String YELLOW_BOLD = "33[1;33m"; // YELLOW
public static final String BLUE_BOLD = "33[1;34m"; // BLUE
public static final String PURPLE_BOLD = "33[1;35m"; // PURPLE
public static final String CYAN_BOLD = "33[1;36m"; // CYAN
public static final String WHITE_BOLD = "33[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "33[4;30m"; // BLACK
public static final String RED_UNDERLINED = "33[4;31m"; // RED
public static final String GREEN_UNDERLINED = "33[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "33[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "33[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "33[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "33[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "33[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "33[40m"; // BLACK
public static final String RED_BACKGROUND = "33[41m"; // RED
public static final String GREEN_BACKGROUND = "33[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "33[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "33[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "33[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "33[46m"; // CYAN
public static final String WHITE_BACKGROUND = "33[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "33[0;90m"; // BLACK
public static final String RED_BRIGHT = "33[0;91m"; // RED
public static final String GREEN_BRIGHT = "33[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "33[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "33[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "33[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "33[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "33[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "33[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "33[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "33[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "33[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "33[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "33[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "33[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "33[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "33[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "33[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "33[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "33[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "33[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "33[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "33[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "33[0;107m"; // WHITE
}
add a comment |
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note
Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = "33[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "33[0;30m"; // BLACK
public static final String RED = "33[0;31m"; // RED
public static final String GREEN = "33[0;32m"; // GREEN
public static final String YELLOW = "33[0;33m"; // YELLOW
public static final String BLUE = "33[0;34m"; // BLUE
public static final String PURPLE = "33[0;35m"; // PURPLE
public static final String CYAN = "33[0;36m"; // CYAN
public static final String WHITE = "33[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "33[1;30m"; // BLACK
public static final String RED_BOLD = "33[1;31m"; // RED
public static final String GREEN_BOLD = "33[1;32m"; // GREEN
public static final String YELLOW_BOLD = "33[1;33m"; // YELLOW
public static final String BLUE_BOLD = "33[1;34m"; // BLUE
public static final String PURPLE_BOLD = "33[1;35m"; // PURPLE
public static final String CYAN_BOLD = "33[1;36m"; // CYAN
public static final String WHITE_BOLD = "33[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "33[4;30m"; // BLACK
public static final String RED_UNDERLINED = "33[4;31m"; // RED
public static final String GREEN_UNDERLINED = "33[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "33[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "33[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "33[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "33[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "33[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "33[40m"; // BLACK
public static final String RED_BACKGROUND = "33[41m"; // RED
public static final String GREEN_BACKGROUND = "33[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "33[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "33[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "33[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "33[46m"; // CYAN
public static final String WHITE_BACKGROUND = "33[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "33[0;90m"; // BLACK
public static final String RED_BRIGHT = "33[0;91m"; // RED
public static final String GREEN_BRIGHT = "33[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "33[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "33[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "33[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "33[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "33[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "33[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "33[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "33[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "33[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "33[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "33[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "33[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "33[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "33[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "33[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "33[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "33[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "33[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "33[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "33[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "33[0;107m"; // WHITE
}
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note
Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = "33[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "33[0;30m"; // BLACK
public static final String RED = "33[0;31m"; // RED
public static final String GREEN = "33[0;32m"; // GREEN
public static final String YELLOW = "33[0;33m"; // YELLOW
public static final String BLUE = "33[0;34m"; // BLUE
public static final String PURPLE = "33[0;35m"; // PURPLE
public static final String CYAN = "33[0;36m"; // CYAN
public static final String WHITE = "33[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "33[1;30m"; // BLACK
public static final String RED_BOLD = "33[1;31m"; // RED
public static final String GREEN_BOLD = "33[1;32m"; // GREEN
public static final String YELLOW_BOLD = "33[1;33m"; // YELLOW
public static final String BLUE_BOLD = "33[1;34m"; // BLUE
public static final String PURPLE_BOLD = "33[1;35m"; // PURPLE
public static final String CYAN_BOLD = "33[1;36m"; // CYAN
public static final String WHITE_BOLD = "33[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "33[4;30m"; // BLACK
public static final String RED_UNDERLINED = "33[4;31m"; // RED
public static final String GREEN_UNDERLINED = "33[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "33[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "33[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "33[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "33[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "33[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "33[40m"; // BLACK
public static final String RED_BACKGROUND = "33[41m"; // RED
public static final String GREEN_BACKGROUND = "33[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "33[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "33[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "33[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "33[46m"; // CYAN
public static final String WHITE_BACKGROUND = "33[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "33[0;90m"; // BLACK
public static final String RED_BRIGHT = "33[0;91m"; // RED
public static final String GREEN_BRIGHT = "33[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "33[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "33[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "33[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "33[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "33[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "33[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "33[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "33[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "33[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "33[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "33[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "33[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "33[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "33[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "33[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "33[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "33[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "33[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "33[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "33[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "33[0;107m"; // WHITE
}
answered Aug 1 '17 at 17:57
shakram02
2,22321220
2,22321220
add a comment |
add a comment |
A fairly portable way of doing it is with the raw escape sequences. See http://en.wikipedia.org/wiki/ANSI_escape_code
[edited for user9999999 on 2017-02-20]
Java doesn't "handle the codes", that's true, but Java outputs what you told it to output. it's not Java's fault that the Windows console treats ESC (chr(27)) as just another glyph (←).
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
the question wasn't taggedwindows
. the Windows console was never ANSI-compliant that I remember.
– jcomeau_ictx
Feb 12 '17 at 14:52
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
2
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
I have same problem
– sgrillon
Mar 15 at 14:58
|
show 1 more comment
A fairly portable way of doing it is with the raw escape sequences. See http://en.wikipedia.org/wiki/ANSI_escape_code
[edited for user9999999 on 2017-02-20]
Java doesn't "handle the codes", that's true, but Java outputs what you told it to output. it's not Java's fault that the Windows console treats ESC (chr(27)) as just another glyph (←).
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
the question wasn't taggedwindows
. the Windows console was never ANSI-compliant that I remember.
– jcomeau_ictx
Feb 12 '17 at 14:52
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
2
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
I have same problem
– sgrillon
Mar 15 at 14:58
|
show 1 more comment
A fairly portable way of doing it is with the raw escape sequences. See http://en.wikipedia.org/wiki/ANSI_escape_code
[edited for user9999999 on 2017-02-20]
Java doesn't "handle the codes", that's true, but Java outputs what you told it to output. it's not Java's fault that the Windows console treats ESC (chr(27)) as just another glyph (←).
A fairly portable way of doing it is with the raw escape sequences. See http://en.wikipedia.org/wiki/ANSI_escape_code
[edited for user9999999 on 2017-02-20]
Java doesn't "handle the codes", that's true, but Java outputs what you told it to output. it's not Java's fault that the Windows console treats ESC (chr(27)) as just another glyph (←).
edited Feb 21 '17 at 11:46
answered Apr 23 '11 at 5:56
jcomeau_ictx
30k56988
30k56988
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
the question wasn't taggedwindows
. the Windows console was never ANSI-compliant that I remember.
– jcomeau_ictx
Feb 12 '17 at 14:52
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
2
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
I have same problem
– sgrillon
Mar 15 at 14:58
|
show 1 more comment
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
the question wasn't taggedwindows
. the Windows console was never ANSI-compliant that I remember.
– jcomeau_ictx
Feb 12 '17 at 14:52
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
2
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
I have same problem
– sgrillon
Mar 15 at 14:58
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
which doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
– simpleuser
Feb 12 '17 at 6:03
the question wasn't tagged
windows
. the Windows console was never ANSI-compliant that I remember.– jcomeau_ictx
Feb 12 '17 at 14:52
the question wasn't tagged
windows
. the Windows console was never ANSI-compliant that I remember.– jcomeau_ictx
Feb 12 '17 at 14:52
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
but the issue is that java isn't handling the codes, regardless of cmd.com's support
– simpleuser
Feb 20 '17 at 17:31
2
2
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
see edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
– jcomeau_ictx
Feb 21 '17 at 6:32
I have same problem
– sgrillon
Mar 15 at 14:58
I have same problem
– sgrillon
Mar 15 at 14:58
|
show 1 more comment
public enum Color {
//颜色结尾字符串,重置颜色的
RESET("33[0m"),
// Regular Colors 普通颜色,不带加粗,背景色等
BLACK("33[0;30m"), // BLACK
RED("33[0;31m"), // RED
GREEN("33[0;32m"), // GREEN
YELLOW("33[0;33m"), // YELLOW
BLUE("33[0;34m"), // BLUE
MAGENTA("33[0;35m"), // MAGENTA
CYAN("33[0;36m"), // CYAN
WHITE("33[0;37m"), // WHITE
// Bold
BLACK_BOLD("33[1;30m"), // BLACK
RED_BOLD("33[1;31m"), // RED
GREEN_BOLD("33[1;32m"), // GREEN
YELLOW_BOLD("33[1;33m"), // YELLOW
BLUE_BOLD("33[1;34m"), // BLUE
MAGENTA_BOLD("33[1;35m"), // MAGENTA
CYAN_BOLD("33[1;36m"), // CYAN
WHITE_BOLD("33[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("33[4;30m"), // BLACK
RED_UNDERLINED("33[4;31m"), // RED
GREEN_UNDERLINED("33[4;32m"), // GREEN
YELLOW_UNDERLINED("33[4;33m"), // YELLOW
BLUE_UNDERLINED("33[4;34m"), // BLUE
MAGENTA_UNDERLINED("33[4;35m"), // MAGENTA
CYAN_UNDERLINED("33[4;36m"), // CYAN
WHITE_UNDERLINED("33[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("33[40m"), // BLACK
RED_BACKGROUND("33[41m"), // RED
GREEN_BACKGROUND("33[42m"), // GREEN
YELLOW_BACKGROUND("33[43m"), // YELLOW
BLUE_BACKGROUND("33[44m"), // BLUE
MAGENTA_BACKGROUND("33[45m"), // MAGENTA
CYAN_BACKGROUND("33[46m"), // CYAN
WHITE_BACKGROUND("33[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("33[0;90m"), // BLACK
RED_BRIGHT("33[0;91m"), // RED
GREEN_BRIGHT("33[0;92m"), // GREEN
YELLOW_BRIGHT("33[0;93m"), // YELLOW
BLUE_BRIGHT("33[0;94m"), // BLUE
MAGENTA_BRIGHT("33[0;95m"), // MAGENTA
CYAN_BRIGHT("33[0;96m"), // CYAN
WHITE_BRIGHT("33[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("33[1;90m"), // BLACK
RED_BOLD_BRIGHT("33[1;91m"), // RED
GREEN_BOLD_BRIGHT("33[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("33[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("33[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("33[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("33[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("33[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("33[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("33[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("33[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("33[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("33[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("33[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("33[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("33[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
System.out.print(Color.BLACK_BOLD);
System.out.println("111111111aaaaaaaaaaaaaaaa==============");
System.out.print(Color.RESET);
System.out.print(Color.BLUE_BACKGROUND);
System.out.print(Color.YELLOW); //设置前景色 为YELLOW
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.print(Color.RESET);
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
add a comment |
public enum Color {
//颜色结尾字符串,重置颜色的
RESET("33[0m"),
// Regular Colors 普通颜色,不带加粗,背景色等
BLACK("33[0;30m"), // BLACK
RED("33[0;31m"), // RED
GREEN("33[0;32m"), // GREEN
YELLOW("33[0;33m"), // YELLOW
BLUE("33[0;34m"), // BLUE
MAGENTA("33[0;35m"), // MAGENTA
CYAN("33[0;36m"), // CYAN
WHITE("33[0;37m"), // WHITE
// Bold
BLACK_BOLD("33[1;30m"), // BLACK
RED_BOLD("33[1;31m"), // RED
GREEN_BOLD("33[1;32m"), // GREEN
YELLOW_BOLD("33[1;33m"), // YELLOW
BLUE_BOLD("33[1;34m"), // BLUE
MAGENTA_BOLD("33[1;35m"), // MAGENTA
CYAN_BOLD("33[1;36m"), // CYAN
WHITE_BOLD("33[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("33[4;30m"), // BLACK
RED_UNDERLINED("33[4;31m"), // RED
GREEN_UNDERLINED("33[4;32m"), // GREEN
YELLOW_UNDERLINED("33[4;33m"), // YELLOW
BLUE_UNDERLINED("33[4;34m"), // BLUE
MAGENTA_UNDERLINED("33[4;35m"), // MAGENTA
CYAN_UNDERLINED("33[4;36m"), // CYAN
WHITE_UNDERLINED("33[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("33[40m"), // BLACK
RED_BACKGROUND("33[41m"), // RED
GREEN_BACKGROUND("33[42m"), // GREEN
YELLOW_BACKGROUND("33[43m"), // YELLOW
BLUE_BACKGROUND("33[44m"), // BLUE
MAGENTA_BACKGROUND("33[45m"), // MAGENTA
CYAN_BACKGROUND("33[46m"), // CYAN
WHITE_BACKGROUND("33[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("33[0;90m"), // BLACK
RED_BRIGHT("33[0;91m"), // RED
GREEN_BRIGHT("33[0;92m"), // GREEN
YELLOW_BRIGHT("33[0;93m"), // YELLOW
BLUE_BRIGHT("33[0;94m"), // BLUE
MAGENTA_BRIGHT("33[0;95m"), // MAGENTA
CYAN_BRIGHT("33[0;96m"), // CYAN
WHITE_BRIGHT("33[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("33[1;90m"), // BLACK
RED_BOLD_BRIGHT("33[1;91m"), // RED
GREEN_BOLD_BRIGHT("33[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("33[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("33[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("33[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("33[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("33[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("33[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("33[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("33[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("33[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("33[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("33[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("33[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("33[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
System.out.print(Color.BLACK_BOLD);
System.out.println("111111111aaaaaaaaaaaaaaaa==============");
System.out.print(Color.RESET);
System.out.print(Color.BLUE_BACKGROUND);
System.out.print(Color.YELLOW); //设置前景色 为YELLOW
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.print(Color.RESET);
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
add a comment |
public enum Color {
//颜色结尾字符串,重置颜色的
RESET("33[0m"),
// Regular Colors 普通颜色,不带加粗,背景色等
BLACK("33[0;30m"), // BLACK
RED("33[0;31m"), // RED
GREEN("33[0;32m"), // GREEN
YELLOW("33[0;33m"), // YELLOW
BLUE("33[0;34m"), // BLUE
MAGENTA("33[0;35m"), // MAGENTA
CYAN("33[0;36m"), // CYAN
WHITE("33[0;37m"), // WHITE
// Bold
BLACK_BOLD("33[1;30m"), // BLACK
RED_BOLD("33[1;31m"), // RED
GREEN_BOLD("33[1;32m"), // GREEN
YELLOW_BOLD("33[1;33m"), // YELLOW
BLUE_BOLD("33[1;34m"), // BLUE
MAGENTA_BOLD("33[1;35m"), // MAGENTA
CYAN_BOLD("33[1;36m"), // CYAN
WHITE_BOLD("33[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("33[4;30m"), // BLACK
RED_UNDERLINED("33[4;31m"), // RED
GREEN_UNDERLINED("33[4;32m"), // GREEN
YELLOW_UNDERLINED("33[4;33m"), // YELLOW
BLUE_UNDERLINED("33[4;34m"), // BLUE
MAGENTA_UNDERLINED("33[4;35m"), // MAGENTA
CYAN_UNDERLINED("33[4;36m"), // CYAN
WHITE_UNDERLINED("33[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("33[40m"), // BLACK
RED_BACKGROUND("33[41m"), // RED
GREEN_BACKGROUND("33[42m"), // GREEN
YELLOW_BACKGROUND("33[43m"), // YELLOW
BLUE_BACKGROUND("33[44m"), // BLUE
MAGENTA_BACKGROUND("33[45m"), // MAGENTA
CYAN_BACKGROUND("33[46m"), // CYAN
WHITE_BACKGROUND("33[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("33[0;90m"), // BLACK
RED_BRIGHT("33[0;91m"), // RED
GREEN_BRIGHT("33[0;92m"), // GREEN
YELLOW_BRIGHT("33[0;93m"), // YELLOW
BLUE_BRIGHT("33[0;94m"), // BLUE
MAGENTA_BRIGHT("33[0;95m"), // MAGENTA
CYAN_BRIGHT("33[0;96m"), // CYAN
WHITE_BRIGHT("33[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("33[1;90m"), // BLACK
RED_BOLD_BRIGHT("33[1;91m"), // RED
GREEN_BOLD_BRIGHT("33[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("33[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("33[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("33[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("33[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("33[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("33[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("33[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("33[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("33[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("33[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("33[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("33[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("33[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
System.out.print(Color.BLACK_BOLD);
System.out.println("111111111aaaaaaaaaaaaaaaa==============");
System.out.print(Color.RESET);
System.out.print(Color.BLUE_BACKGROUND);
System.out.print(Color.YELLOW); //设置前景色 为YELLOW
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.print(Color.RESET);
public enum Color {
//颜色结尾字符串,重置颜色的
RESET("33[0m"),
// Regular Colors 普通颜色,不带加粗,背景色等
BLACK("33[0;30m"), // BLACK
RED("33[0;31m"), // RED
GREEN("33[0;32m"), // GREEN
YELLOW("33[0;33m"), // YELLOW
BLUE("33[0;34m"), // BLUE
MAGENTA("33[0;35m"), // MAGENTA
CYAN("33[0;36m"), // CYAN
WHITE("33[0;37m"), // WHITE
// Bold
BLACK_BOLD("33[1;30m"), // BLACK
RED_BOLD("33[1;31m"), // RED
GREEN_BOLD("33[1;32m"), // GREEN
YELLOW_BOLD("33[1;33m"), // YELLOW
BLUE_BOLD("33[1;34m"), // BLUE
MAGENTA_BOLD("33[1;35m"), // MAGENTA
CYAN_BOLD("33[1;36m"), // CYAN
WHITE_BOLD("33[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("33[4;30m"), // BLACK
RED_UNDERLINED("33[4;31m"), // RED
GREEN_UNDERLINED("33[4;32m"), // GREEN
YELLOW_UNDERLINED("33[4;33m"), // YELLOW
BLUE_UNDERLINED("33[4;34m"), // BLUE
MAGENTA_UNDERLINED("33[4;35m"), // MAGENTA
CYAN_UNDERLINED("33[4;36m"), // CYAN
WHITE_UNDERLINED("33[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("33[40m"), // BLACK
RED_BACKGROUND("33[41m"), // RED
GREEN_BACKGROUND("33[42m"), // GREEN
YELLOW_BACKGROUND("33[43m"), // YELLOW
BLUE_BACKGROUND("33[44m"), // BLUE
MAGENTA_BACKGROUND("33[45m"), // MAGENTA
CYAN_BACKGROUND("33[46m"), // CYAN
WHITE_BACKGROUND("33[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("33[0;90m"), // BLACK
RED_BRIGHT("33[0;91m"), // RED
GREEN_BRIGHT("33[0;92m"), // GREEN
YELLOW_BRIGHT("33[0;93m"), // YELLOW
BLUE_BRIGHT("33[0;94m"), // BLUE
MAGENTA_BRIGHT("33[0;95m"), // MAGENTA
CYAN_BRIGHT("33[0;96m"), // CYAN
WHITE_BRIGHT("33[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("33[1;90m"), // BLACK
RED_BOLD_BRIGHT("33[1;91m"), // RED
GREEN_BOLD_BRIGHT("33[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("33[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("33[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("33[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("33[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("33[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("33[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("33[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("33[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("33[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("33[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("33[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("33[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("33[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
System.out.print(Color.BLACK_BOLD);
System.out.println("111111111aaaaaaaaaaaaaaaa==============");
System.out.print(Color.RESET);
System.out.print(Color.BLUE_BACKGROUND);
System.out.print(Color.YELLOW); //设置前景色 为YELLOW
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.println("111111111aaaaaaaaaaaaaaaa==============马哥私房菜");
System.out.print(Color.RESET);
answered Aug 21 at 8:28
马哥私房菜
11912
11912
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
add a comment |
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
Excellent enum !
– Amr Lotfy
Aug 28 at 23:54
add a comment |
You could do this using ANSI escape sequences. I've actually put together this class in Java for anyone that would like a simple workaround for this. It allows for the use of custom color codes in text.
https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a
Example Use:
Color code format WITH background color -> :foreground,background:
Color code format WITHOUT background color -> :foreground,N:
Reset Color format -> [RC]
String ansiColoredString = ColorCodes.parseColors("Hello, This :blue,n:is[RC] a :red,white:response[RC].");
or
String ansiColoredString = ColorCodes.RED + "Hello" + ColorCodes.WHITE + ", This is a " + ColorCodes.BLUE + "test";
add a comment |
You could do this using ANSI escape sequences. I've actually put together this class in Java for anyone that would like a simple workaround for this. It allows for the use of custom color codes in text.
https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a
Example Use:
Color code format WITH background color -> :foreground,background:
Color code format WITHOUT background color -> :foreground,N:
Reset Color format -> [RC]
String ansiColoredString = ColorCodes.parseColors("Hello, This :blue,n:is[RC] a :red,white:response[RC].");
or
String ansiColoredString = ColorCodes.RED + "Hello" + ColorCodes.WHITE + ", This is a " + ColorCodes.BLUE + "test";
add a comment |
You could do this using ANSI escape sequences. I've actually put together this class in Java for anyone that would like a simple workaround for this. It allows for the use of custom color codes in text.
https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a
Example Use:
Color code format WITH background color -> :foreground,background:
Color code format WITHOUT background color -> :foreground,N:
Reset Color format -> [RC]
String ansiColoredString = ColorCodes.parseColors("Hello, This :blue,n:is[RC] a :red,white:response[RC].");
or
String ansiColoredString = ColorCodes.RED + "Hello" + ColorCodes.WHITE + ", This is a " + ColorCodes.BLUE + "test";
You could do this using ANSI escape sequences. I've actually put together this class in Java for anyone that would like a simple workaround for this. It allows for the use of custom color codes in text.
https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a
Example Use:
Color code format WITH background color -> :foreground,background:
Color code format WITHOUT background color -> :foreground,N:
Reset Color format -> [RC]
String ansiColoredString = ColorCodes.parseColors("Hello, This :blue,n:is[RC] a :red,white:response[RC].");
or
String ansiColoredString = ColorCodes.RED + "Hello" + ColorCodes.WHITE + ", This is a " + ColorCodes.BLUE + "test";
edited Sep 24 '16 at 1:57
answered Jan 5 '15 at 21:35
Nathan Fiscaletti
7781339
7781339
add a comment |
add a comment |
If anyone is looking for a quick solution, feel free to use the following helper class :)
public class Log {
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
//info
public static void i(String className, String message) {
System.out.println(ANSI_GREEN + className + " : " + message + ANSI_RESET);
}
//error
public static void e(String className, String message) {
System.out.println(ANSI_RED + className + " : " + message + ANSI_RESET);
}
//debug
public static void d(String className, String message) {
System.out.println(ANSI_BLUE + className + " : " + message + ANSI_RESET);
}
//warning
public static void w(String className, String message) {
System.out.println(ANSI_YELLOW + className + " : " + message + ANSI_RESET);
}
}
USAGE:
Log.i(TAG,"This is an info message");
Log.e(TAG,"This is an error message");
Log.w(TAG,"This is a warning message");
Log.d(TAG,"This is a debug message");
Thanks @whiteFang34 for the ANSI codes.
add a comment |
If anyone is looking for a quick solution, feel free to use the following helper class :)
public class Log {
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
//info
public static void i(String className, String message) {
System.out.println(ANSI_GREEN + className + " : " + message + ANSI_RESET);
}
//error
public static void e(String className, String message) {
System.out.println(ANSI_RED + className + " : " + message + ANSI_RESET);
}
//debug
public static void d(String className, String message) {
System.out.println(ANSI_BLUE + className + " : " + message + ANSI_RESET);
}
//warning
public static void w(String className, String message) {
System.out.println(ANSI_YELLOW + className + " : " + message + ANSI_RESET);
}
}
USAGE:
Log.i(TAG,"This is an info message");
Log.e(TAG,"This is an error message");
Log.w(TAG,"This is a warning message");
Log.d(TAG,"This is a debug message");
Thanks @whiteFang34 for the ANSI codes.
add a comment |
If anyone is looking for a quick solution, feel free to use the following helper class :)
public class Log {
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
//info
public static void i(String className, String message) {
System.out.println(ANSI_GREEN + className + " : " + message + ANSI_RESET);
}
//error
public static void e(String className, String message) {
System.out.println(ANSI_RED + className + " : " + message + ANSI_RESET);
}
//debug
public static void d(String className, String message) {
System.out.println(ANSI_BLUE + className + " : " + message + ANSI_RESET);
}
//warning
public static void w(String className, String message) {
System.out.println(ANSI_YELLOW + className + " : " + message + ANSI_RESET);
}
}
USAGE:
Log.i(TAG,"This is an info message");
Log.e(TAG,"This is an error message");
Log.w(TAG,"This is a warning message");
Log.d(TAG,"This is a debug message");
Thanks @whiteFang34 for the ANSI codes.
If anyone is looking for a quick solution, feel free to use the following helper class :)
public class Log {
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
//info
public static void i(String className, String message) {
System.out.println(ANSI_GREEN + className + " : " + message + ANSI_RESET);
}
//error
public static void e(String className, String message) {
System.out.println(ANSI_RED + className + " : " + message + ANSI_RESET);
}
//debug
public static void d(String className, String message) {
System.out.println(ANSI_BLUE + className + " : " + message + ANSI_RESET);
}
//warning
public static void w(String className, String message) {
System.out.println(ANSI_YELLOW + className + " : " + message + ANSI_RESET);
}
}
USAGE:
Log.i(TAG,"This is an info message");
Log.e(TAG,"This is an error message");
Log.w(TAG,"This is a warning message");
Log.d(TAG,"This is a debug message");
Thanks @whiteFang34 for the ANSI codes.
answered Sep 23 '17 at 3:22
Ajmal Salim
2,80822435
2,80822435
add a comment |
add a comment |
protected by davidkonrad Dec 11 '17 at 16:54
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?