如何在控制台中打印颜色?我想在处理器发送数据时以彩色显示数据,而在接收数据时以不同的颜色显示数据。
如何在控制台中打印颜色?我想在处理器发送数据时以彩色显示数据,而在接收数据时以不同的颜色显示数据。
Answers:
如果您的终端支持,则可以使用ANSI转义码在输出中使用颜色。它通常适用于Unix shell提示符;但是,它不适用于Windows命令提示符(尽管它适用于Cygwin)。例如,您可以为颜色定义如下常量:
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";
然后,您可以根据需要引用它们。
例如,使用上述常量,可以在支持的终端上输出以下红色文本:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
更新:您可能想签出Jansi库。它提供一个API,并支持使用JNI的Windows。我还没试过 但是,它看起来很有希望。
更新2:此外,如果您希望将文本的背景色更改为其他颜色,也可以尝试以下操作:
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";
例如:
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 (System.console() == null) System.setProperty("jansi.passthrough", "true");
however it doesn't work for Windows command prompt
这是带有public static
字段的Java类中的颜色列表
用法
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
注意
不要忘记使用RESET
后打印,因为如果未清除,效果将保留
public class ConsoleColors {
// Reset
public static final String RESET = "\033[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "\033[0;30m"; // BLACK
public static final String RED = "\033[0;31m"; // RED
public static final String GREEN = "\033[0;32m"; // GREEN
public static final String YELLOW = "\033[0;33m"; // YELLOW
public static final String BLUE = "\033[0;34m"; // BLUE
public static final String PURPLE = "\033[0;35m"; // PURPLE
public static final String CYAN = "\033[0;36m"; // CYAN
public static final String WHITE = "\033[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "\033[1;30m"; // BLACK
public static final String RED_BOLD = "\033[1;31m"; // RED
public static final String GREEN_BOLD = "\033[1;32m"; // GREEN
public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW
public static final String BLUE_BOLD = "\033[1;34m"; // BLUE
public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE
public static final String CYAN_BOLD = "\033[1;36m"; // CYAN
public static final String WHITE_BOLD = "\033[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK
public static final String RED_UNDERLINED = "\033[4;31m"; // RED
public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
public static final String RED_BACKGROUND = "\033[41m"; // RED
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN
public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK
public static final String RED_BRIGHT = "\033[0;91m"; // RED
public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE
}
我创建了一个名为JCDP(Java彩色调试打印机)的库。
对于Linux,macOS和Windows 10,它使用WhiteFang提到的ANSI转义代码,但使用单词而不是代码来抽象它们,这更加直观。它变得像:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
您可以在JCDP的github仓库中找到一些示例。
尝试以下枚举:
enum Color {
//Color end string, color reset
RESET("\033[0m"),
// Regular Colors. Normal color, no bold, background color etc.
BLACK("\033[0;30m"), // BLACK
RED("\033[0;31m"), // RED
GREEN("\033[0;32m"), // GREEN
YELLOW("\033[0;33m"), // YELLOW
BLUE("\033[0;34m"), // BLUE
MAGENTA("\033[0;35m"), // MAGENTA
CYAN("\033[0;36m"), // CYAN
WHITE("\033[0;37m"), // WHITE
// Bold
BLACK_BOLD("\033[1;30m"), // BLACK
RED_BOLD("\033[1;31m"), // RED
GREEN_BOLD("\033[1;32m"), // GREEN
YELLOW_BOLD("\033[1;33m"), // YELLOW
BLUE_BOLD("\033[1;34m"), // BLUE
MAGENTA_BOLD("\033[1;35m"), // MAGENTA
CYAN_BOLD("\033[1;36m"), // CYAN
WHITE_BOLD("\033[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("\033[4;30m"), // BLACK
RED_UNDERLINED("\033[4;31m"), // RED
GREEN_UNDERLINED("\033[4;32m"), // GREEN
YELLOW_UNDERLINED("\033[4;33m"), // YELLOW
BLUE_UNDERLINED("\033[4;34m"), // BLUE
MAGENTA_UNDERLINED("\033[4;35m"), // MAGENTA
CYAN_UNDERLINED("\033[4;36m"), // CYAN
WHITE_UNDERLINED("\033[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("\033[40m"), // BLACK
RED_BACKGROUND("\033[41m"), // RED
GREEN_BACKGROUND("\033[42m"), // GREEN
YELLOW_BACKGROUND("\033[43m"), // YELLOW
BLUE_BACKGROUND("\033[44m"), // BLUE
MAGENTA_BACKGROUND("\033[45m"), // MAGENTA
CYAN_BACKGROUND("\033[46m"), // CYAN
WHITE_BACKGROUND("\033[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("\033[0;90m"), // BLACK
RED_BRIGHT("\033[0;91m"), // RED
GREEN_BRIGHT("\033[0;92m"), // GREEN
YELLOW_BRIGHT("\033[0;93m"), // YELLOW
BLUE_BRIGHT("\033[0;94m"), // BLUE
MAGENTA_BRIGHT("\033[0;95m"), // MAGENTA
CYAN_BRIGHT("\033[0;96m"), // CYAN
WHITE_BRIGHT("\033[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("\033[1;90m"), // BLACK
RED_BOLD_BRIGHT("\033[1;91m"), // RED
GREEN_BOLD_BRIGHT("\033[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("\033[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("\033[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("\033[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("\033[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("\033[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("\033[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("\033[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("\033[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("\033[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("\033[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("\033[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("\033[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("\033[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
现在我们举一个小例子:
class RunApp {
public static void main(String[] args) {
System.out.print(Color.BLACK_BOLD);
System.out.println("Black_Bold");
System.out.print(Color.RESET);
System.out.print(Color.YELLOW);
System.out.print(Color.BLUE_BACKGROUND);
System.out.println("YELLOW & BLUE");
System.out.print(Color.RESET);
System.out.print(Color.YELLOW);
System.out.println("YELLOW");
System.out.print(Color.RESET);
}
}
原始转义序列是一种相当可移植的方法。参见http://en.wikipedia.org/wiki/ANSI_escape_code
[于2017-02-20为user9999999编辑]
Java不会“处理代码”,这是事实,但是Java输出了您告诉它的输出。Windows控制台将ESC(chr(27))当作另一个字形(←)并不是Java的错。
windows
。我记得Windows控制台从不兼容ANSI。
您可以使用ANSI转义序列来执行此操作。实际上,我已经将Java中的此类放在一起,以供任何想要简单解决方法的人使用。它不仅允许使用颜色代码。
https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a
(移植自:https : //github.com/nathan-fiscaletti/ansi-util)
使用示例:
StringBuilder sb = new StringBuilder();
System.out.println(
sb.raw("Hello, ")
.underline("John Doe")
.resetUnderline()
.raw(". ")
.raw("This is ")
.color16(StringBuilder.Color16.FG_RED, "red")
.raw(".")
);
如果有人在寻找快速解决方案,请随时使用以下帮助程序类:)
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);
}
}
用法:
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");
感谢@ whiteFang34提供ANSI代码。
为控制台文本着色的最佳方法是使用ANSI转义码。除文本颜色外,ANSI转义码还允许使用背景颜色,装饰等。
Unix系统
如果使用springboot,则有一个特定的枚举用于文本着色:org.springframework.boot.ansi.AnsiColor
Jansi库要先进一些(可以使用所有ANSI逸出代码功能),提供了API,并支持使用JNA的Windows。
否则,您可以手动定义自己的颜色,如其他响应所示。
Windows 10
Windows 10(自内部版本10.0.10586-2015年11月以来)支持ANSI转义码(MSDN文档),但默认情况下未启用。要启用它:
ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x0400)
flag。Jansi使用此选项。HKEY_CURRENT_USER\Console\VirtualTerminalLevel
通过创建dword并将其设置为0或1进行ANSI处理来更改全局注册表项:
"VirtualTerminalLevel"=dword:00000001
Windows 10之前
Windows控制台不支持ANSI颜色。但是可以使用控制台。
使用颜色功能以彩色打印文本
码:
enum Color {
RED("\033[0;31m"), // RED
GREEN("\033[0;32m"), // GREEN
YELLOW("\033[0;33m"), // YELLOW
BLUE("\033[0;34m"), // BLUE
MAGENTA("\033[0;35m"), // MAGENTA
CYAN("\033[0;36m"), // CYAN
private final String code
Color(String code) {
this.code = code;
}
@Override
String toString() {
return code
}
}
def color = { color, txt ->
def RESET_COLOR = "\033[0m"
return "${color}${txt}${RESET_COLOR}"
}
用法:
test {
println color(Color.CYAN, 'testing')
}
删除:
public static final String ANSI_STRIKEOUT_BLACK = "\u001B[30;9m";
public static final String ANSI_STRIKEOUT_RED = "\u001B[31;9m";
public static final String ANSI_STRIKEOUT_GREEN = "\u001B[32;9m";
public static final String ANSI_STRIKEOUT_YELLOW = "\u001B[33;9m";
public static final String ANSI_STRIKEOUT_BLUE = "\u001B[34;9m";
public static final String ANSI_STRIKEOUT_PURPLE = "\u001B[35;9m";
public static final String ANSI_STRIKEOUT_CYAN = "\u001B[36;9m";
public static final String ANSI_STRIKEOUT_WHITE = "\u001B[37;9m";
在Java中以红色打印任何文本的最佳解决方案是:
System.err.print("Hello World");
如果使用Kotlin(可与Java无缝集成),则可以进行这样的枚举:
enum class AnsiColor(private val colorNumber: Byte) {
BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7);
companion object {
private const val prefix = "\u001B"
const val RESET = "$prefix[0m"
private val isCompatible = "win" !in System.getProperty("os.name").toLowerCase()
}
val regular get() = if (isCompatible) "$prefix[0;3${colorNumber}m" else ""
val bold get() = if (isCompatible) "$prefix[1;3${colorNumber}m" else ""
val underline get() = if (isCompatible) "$prefix[4;3${colorNumber}m" else ""
val background get() = if (isCompatible) "$prefix[4${colorNumber}m" else ""
val highIntensity get() = if (isCompatible) "$prefix[0;9${colorNumber}m" else ""
val boldHighIntensity get() = if (isCompatible) "$prefix[1;9${colorNumber}m" else ""
val backgroundHighIntensity get() = if (isCompatible) "$prefix[0;10${colorNumber}m" else ""
}
然后这样使用:(下面的代码展示了所有颜色的不同样式)
val sampleText = "This is a sample text"
enumValues<AnsiColor>().forEach { ansiColor ->
println("${ansiColor.regular}$sampleText${AnsiColor.RESET}")
println("${ansiColor.bold}$sampleText${AnsiColor.RESET}")
println("${ansiColor.underline}$sampleText${AnsiColor.RESET}")
println("${ansiColor.background}$sampleText${AnsiColor.RESET}")
println("${ansiColor.highIntensity}$sampleText${AnsiColor.RESET}")
println("${ansiColor.boldHighIntensity}$sampleText${AnsiColor.RESET}")
println("${ansiColor.backgroundHighIntensity}$sampleText${AnsiColor.RESET}")
}
如果在不支持这些ANSI代码的Windows上运行,该isCompatible
检查通过将代码替换为空字符串来避免问题。