java idea 控制台彩色文字输出
public class test {
public enum ColorEnum {
RED("red","\033[0;31m","红色"),
BLACK("black","\033[0;30m","黑色"),
GREEN("green","\033[0;32m","绿色"),
YELLOW("yellow","\033[0;33m","黄色"),
BLUE("blue","\033[0;34m","蓝色"),
PURPLE("purple","\033[0;35m","紫色"),
CYAN("cyan","\033[0;36m","青色"),
GREY("grey","\033[0;37m","灰色"),
;
private String name;
private String value;
private String description;
ColorEnum(String name, String value, String description){
this.name = name;
this.value = value;
this.description = description;
}
public String getName(){
return this.name;
}
public String getValue(){
return this.value;
}
public static ColorEnum of(String name){
for (ColorEnum value : ColorEnum.values()) {
if(value.getName().equals(name)){
return value;
}
}
return null;
}
}
public static void log(ColorEnum color, String msg){
if(null==color){
System.out.println(msg);
}else {
System.out.println(color.getValue() + msg + "\u001B[0m");
}
}
public static void main(String[] args) {
log(null,"默认文本");
log(ColorEnum.of("red"),"红色文本");
log(ColorEnum.of("black"),"黑色文本");
log(ColorEnum.of("green"),"绿色文本");
log(ColorEnum.of("yellow"),"黄色文本");
log(ColorEnum.of("blue"),"蓝色文本");
log(ColorEnum.of("purple"),"紫色文本");
log(ColorEnum.of("cyan"),"青色文本");
log(ColorEnum.of("grey"),"灰色文本");
}
}