mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
a081bdc5e1
commit
cf0fc45080
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.lang.ansi;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
/**
|
||||
* ANSI背景颜色枚举
|
||||
*
|
||||
@ -8,102 +10,112 @@ package cn.hutool.core.lang.ansi;
|
||||
* @author Phillip Webb, Geoffrey Chandler
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public enum AnsiBackground implements AnsiElement {
|
||||
public enum Ansi4BitBackgroundColor implements AnsiElement {
|
||||
|
||||
/**
|
||||
* 默认背景色
|
||||
*/
|
||||
DEFAULT("49"),
|
||||
DEFAULT(49),
|
||||
|
||||
/**
|
||||
* 黑色
|
||||
*/
|
||||
BLACK("40"),
|
||||
BLACK(40),
|
||||
|
||||
/**
|
||||
* 红
|
||||
*/
|
||||
RED("41"),
|
||||
RED(41),
|
||||
|
||||
/**
|
||||
* 绿
|
||||
*/
|
||||
GREEN("42"),
|
||||
GREEN(42),
|
||||
|
||||
/**
|
||||
* 黄
|
||||
*/
|
||||
YELLOW("43"),
|
||||
YELLOW(43),
|
||||
|
||||
/**
|
||||
* 蓝
|
||||
*/
|
||||
BLUE("44"),
|
||||
BLUE(44),
|
||||
|
||||
/**
|
||||
* 品红
|
||||
*/
|
||||
MAGENTA("45"),
|
||||
MAGENTA(45),
|
||||
|
||||
/**
|
||||
* 青
|
||||
*/
|
||||
CYAN("46"),
|
||||
CYAN(46),
|
||||
|
||||
/**
|
||||
* 白
|
||||
*/
|
||||
WHITE("47"),
|
||||
WHITE(47),
|
||||
|
||||
/**
|
||||
* 亮黑
|
||||
*/
|
||||
BRIGHT_BLACK("100"),
|
||||
BRIGHT_BLACK(100),
|
||||
|
||||
/**
|
||||
* 亮红
|
||||
*/
|
||||
BRIGHT_RED("101"),
|
||||
BRIGHT_RED(101),
|
||||
|
||||
/**
|
||||
* 亮绿
|
||||
*/
|
||||
BRIGHT_GREEN("102"),
|
||||
BRIGHT_GREEN(102),
|
||||
|
||||
/**
|
||||
* 亮黄
|
||||
*/
|
||||
BRIGHT_YELLOW("103"),
|
||||
BRIGHT_YELLOW(103),
|
||||
|
||||
/**
|
||||
* 亮蓝
|
||||
*/
|
||||
BRIGHT_BLUE("104"),
|
||||
BRIGHT_BLUE(104),
|
||||
|
||||
/**
|
||||
* 亮品红
|
||||
*/
|
||||
BRIGHT_MAGENTA("105"),
|
||||
BRIGHT_MAGENTA(105),
|
||||
|
||||
/**
|
||||
* 亮青
|
||||
*/
|
||||
BRIGHT_CYAN("106"),
|
||||
BRIGHT_CYAN(106),
|
||||
|
||||
/**
|
||||
* 亮白
|
||||
*/
|
||||
BRIGHT_WHITE("107");
|
||||
BRIGHT_WHITE(107);
|
||||
|
||||
private final String code;
|
||||
private final int code;
|
||||
|
||||
AnsiBackground(final String code) {
|
||||
Ansi4BitBackgroundColor(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ANSI颜色代码
|
||||
*
|
||||
* @return 颜色代码
|
||||
*/
|
||||
@Override
|
||||
public int getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.code;
|
||||
return StrUtil.toString(this.code);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
package cn.hutool.core.lang.ansi;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
/**
|
||||
* ANSI标准颜色
|
||||
*
|
||||
* <p>来自Spring Boot</p>
|
||||
*
|
||||
* @author Phillip Webb, Geoffrey Chandler
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public enum Ansi4BitColor implements AnsiElement {
|
||||
|
||||
/**
|
||||
* 默认前景色
|
||||
*/
|
||||
DEFAULT(39),
|
||||
|
||||
/**
|
||||
* 黑
|
||||
*/
|
||||
BLACK(30),
|
||||
|
||||
/**
|
||||
* 红
|
||||
*/
|
||||
RED(31),
|
||||
|
||||
/**
|
||||
* 绿
|
||||
*/
|
||||
GREEN(32),
|
||||
|
||||
/**
|
||||
* 黄
|
||||
*/
|
||||
YELLOW(33),
|
||||
|
||||
/**
|
||||
* 蓝
|
||||
*/
|
||||
BLUE(34),
|
||||
|
||||
/**
|
||||
* 品红
|
||||
*/
|
||||
MAGENTA(35),
|
||||
|
||||
/**
|
||||
* 青
|
||||
*/
|
||||
CYAN(36),
|
||||
|
||||
/**
|
||||
* 白
|
||||
*/
|
||||
WHITE(37),
|
||||
|
||||
/**
|
||||
* 亮黑
|
||||
*/
|
||||
BRIGHT_BLACK(90),
|
||||
|
||||
/**
|
||||
* 亮红
|
||||
*/
|
||||
BRIGHT_RED(91),
|
||||
|
||||
/**
|
||||
* 亮绿
|
||||
*/
|
||||
BRIGHT_GREEN(92),
|
||||
|
||||
/**
|
||||
* 亮黄
|
||||
*/
|
||||
BRIGHT_YELLOW(93),
|
||||
|
||||
/**
|
||||
* 亮蓝
|
||||
*/
|
||||
BRIGHT_BLUE(94),
|
||||
|
||||
/**
|
||||
* 亮品红
|
||||
*/
|
||||
BRIGHT_MAGENTA(95),
|
||||
|
||||
/**
|
||||
* 亮青
|
||||
*/
|
||||
BRIGHT_CYAN(96),
|
||||
|
||||
/**
|
||||
* 亮白
|
||||
*/
|
||||
BRIGHT_WHITE(97);
|
||||
|
||||
private final int code;
|
||||
|
||||
Ansi4BitColor(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ANSI颜色代码(前景色)
|
||||
*
|
||||
* @return 颜色代码
|
||||
*/
|
||||
@Override
|
||||
public int getCode() {
|
||||
return getCode(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ANSI颜色代码
|
||||
*
|
||||
* @param isBackground 是否背景色
|
||||
* @return 颜色代码
|
||||
*/
|
||||
public int getCode(boolean isBackground) {
|
||||
return isBackground ? this.code + 10 : this.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return StrUtil.toString(this.code);
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.lang.ansi;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
/**
|
||||
* ANSI 8-bit前景或背景色(即8位编码,共256种颜色(2^8) )<br>
|
||||
@ -18,7 +19,7 @@ import cn.hutool.core.lang.Assert;
|
||||
* @see #background(int)
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public final class Ansi8BitColor implements AnsiElement {
|
||||
public class Ansi8BitColor implements AnsiElement {
|
||||
|
||||
private static final String PREFIX_FORE = "38;5;";
|
||||
private static final String PREFIX_BACK = "48;5;";
|
||||
@ -29,7 +30,7 @@ public final class Ansi8BitColor implements AnsiElement {
|
||||
* @param code 颜色代码(0-255)
|
||||
* @return 前景色ANSI颜色实例
|
||||
*/
|
||||
public static Ansi8BitColor foreground(final int code) {
|
||||
public static Ansi8BitColor foreground(int code) {
|
||||
return new Ansi8BitColor(PREFIX_FORE, code);
|
||||
}
|
||||
|
||||
@ -39,7 +40,7 @@ public final class Ansi8BitColor implements AnsiElement {
|
||||
* @param code 颜色代码(0-255)
|
||||
* @return 背景色ANSI颜色实例
|
||||
*/
|
||||
public static Ansi8BitColor background(final int code) {
|
||||
public static Ansi8BitColor background(int code) {
|
||||
return new Ansi8BitColor(PREFIX_BACK, code);
|
||||
}
|
||||
|
||||
@ -53,22 +54,32 @@ public final class Ansi8BitColor implements AnsiElement {
|
||||
* @param code 颜色代码(0-255)
|
||||
* @throws IllegalArgumentException 颜色代码不在0~255范围内
|
||||
*/
|
||||
private Ansi8BitColor(final String prefix, final int code) {
|
||||
private Ansi8BitColor(String prefix, int code) {
|
||||
Assert.isTrue(code >= 0 && code <= 255, "Code must be between 0 and 255");
|
||||
this.prefix = prefix;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取颜色代码(0-255)
|
||||
*
|
||||
* @return 颜色代码(0 - 255)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
public int getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Ansi8BitColor other = (Ansi8BitColor) obj;
|
||||
return this.prefix.equals(other.prefix) && this.code == other.code;
|
||||
Ansi8BitColor other = (Ansi8BitColor) obj;
|
||||
return ObjUtil.equals(this.prefix, other.prefix) && this.code == other.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,109 +0,0 @@
|
||||
package cn.hutool.core.lang.ansi;
|
||||
|
||||
/**
|
||||
* ANSI标准颜色
|
||||
*
|
||||
* <p>来自Spring Boot</p>
|
||||
*
|
||||
* @author Phillip Webb, Geoffrey Chandler
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public enum AnsiColor implements AnsiElement {
|
||||
|
||||
/**
|
||||
* 默认前景色
|
||||
*/
|
||||
DEFAULT("39"),
|
||||
|
||||
/**
|
||||
* 黑
|
||||
*/
|
||||
BLACK("30"),
|
||||
|
||||
/**
|
||||
* 红
|
||||
*/
|
||||
RED("31"),
|
||||
|
||||
/**
|
||||
* 绿
|
||||
*/
|
||||
GREEN("32"),
|
||||
|
||||
/**
|
||||
* 黄
|
||||
*/
|
||||
YELLOW("33"),
|
||||
|
||||
/**
|
||||
* 蓝
|
||||
*/
|
||||
BLUE("34"),
|
||||
|
||||
/**
|
||||
* 品红
|
||||
*/
|
||||
MAGENTA("35"),
|
||||
|
||||
/**
|
||||
* 青
|
||||
*/
|
||||
CYAN("36"),
|
||||
|
||||
/**
|
||||
* 白
|
||||
*/
|
||||
WHITE("37"),
|
||||
|
||||
/**
|
||||
* 亮黑
|
||||
*/
|
||||
BRIGHT_BLACK("90"),
|
||||
|
||||
/**
|
||||
* 亮红
|
||||
*/
|
||||
BRIGHT_RED("91"),
|
||||
|
||||
/**
|
||||
* 亮绿
|
||||
*/
|
||||
BRIGHT_GREEN("92"),
|
||||
|
||||
/**
|
||||
* 亮黄
|
||||
*/
|
||||
BRIGHT_YELLOW("93"),
|
||||
|
||||
/**
|
||||
* 亮蓝
|
||||
*/
|
||||
BRIGHT_BLUE("94"),
|
||||
|
||||
/**
|
||||
* 亮品红
|
||||
*/
|
||||
BRIGHT_MAGENTA("95"),
|
||||
|
||||
/**
|
||||
* 亮青
|
||||
*/
|
||||
BRIGHT_CYAN("96"),
|
||||
|
||||
/**
|
||||
* 亮白
|
||||
*/
|
||||
BRIGHT_WHITE("97");
|
||||
|
||||
private final String code;
|
||||
|
||||
AnsiColor(final String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
}
|
@ -9,6 +9,13 @@ package cn.hutool.core.lang.ansi;
|
||||
*/
|
||||
public interface AnsiElement {
|
||||
|
||||
/**
|
||||
* 获取ANSI代码
|
||||
* @return ANSI代码
|
||||
* @since 5.8.7
|
||||
*/
|
||||
int getCode();
|
||||
|
||||
/**
|
||||
* @return ANSI转义编码
|
||||
*/
|
||||
|
@ -10,7 +10,7 @@ public abstract class AnsiEncoder {
|
||||
private static final String ENCODE_JOIN = ";";
|
||||
private static final String ENCODE_START = "\033[";
|
||||
private static final String ENCODE_END = "m";
|
||||
private static final String RESET = "0;" + AnsiColor.DEFAULT;
|
||||
private static final String RESET = "0;" + Ansi4BitColor.DEFAULT;
|
||||
|
||||
/**
|
||||
* 创建ANSI字符串,参数中的{@link AnsiElement}会被转换为编码形式。
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.lang.ansi;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
/**
|
||||
* ANSI文本样式风格枚举
|
||||
*
|
||||
@ -13,37 +15,47 @@ public enum AnsiStyle implements AnsiElement {
|
||||
/**
|
||||
* 重置/正常
|
||||
*/
|
||||
NORMAL("0"),
|
||||
NORMAL(0),
|
||||
|
||||
/**
|
||||
* 粗体或增加强度
|
||||
*/
|
||||
BOLD("1"),
|
||||
BOLD(1),
|
||||
|
||||
/**
|
||||
* 弱化(降低强度)
|
||||
*/
|
||||
FAINT("2"),
|
||||
FAINT(2),
|
||||
|
||||
/**
|
||||
* 斜体
|
||||
*/
|
||||
ITALIC("3"),
|
||||
ITALIC(3),
|
||||
|
||||
/**
|
||||
* 下划线
|
||||
*/
|
||||
UNDERLINE("4");
|
||||
UNDERLINE(4);
|
||||
|
||||
private final String code;
|
||||
private final int code;
|
||||
|
||||
AnsiStyle(final String code) {
|
||||
AnsiStyle(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ANSI文本样式风格代码
|
||||
*
|
||||
* @return 文本样式风格代码
|
||||
*/
|
||||
@Override
|
||||
public int getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.code;
|
||||
return StrUtil.toString(this.code);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ public class AnsiEncoderTest {
|
||||
|
||||
@Test
|
||||
public void encodeTest(){
|
||||
final String encode = AnsiEncoder.encode(AnsiColor.GREEN, "Hutool test");
|
||||
final String encode = AnsiEncoder.encode(Ansi4BitColor.GREEN, "Hutool test");
|
||||
Assert.assertEquals("\u001B[32mHutool test\u001B[0;39m", encode);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.hutool.log.dialect.console;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.ansi.AnsiColor;
|
||||
import cn.hutool.core.lang.ansi.Ansi4BitColor;
|
||||
import cn.hutool.core.lang.ansi.AnsiEncoder;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
@ -21,29 +21,29 @@ public class ConsoleColorLog extends ConsoleLog {
|
||||
/**
|
||||
* 控制台打印类名的颜色代码
|
||||
*/
|
||||
private static final AnsiColor COLOR_CLASSNAME = AnsiColor.CYAN;
|
||||
private static final Ansi4BitColor COLOR_CLASSNAME = Ansi4BitColor.CYAN;
|
||||
|
||||
/**
|
||||
* 控制台打印时间的颜色代码
|
||||
*/
|
||||
private static final AnsiColor COLOR_TIME = AnsiColor.WHITE;
|
||||
private static final Ansi4BitColor COLOR_TIME = Ansi4BitColor.WHITE;
|
||||
|
||||
/**
|
||||
* 控制台打印正常信息的颜色代码
|
||||
*/
|
||||
private static final AnsiColor COLOR_NONE = AnsiColor.DEFAULT;
|
||||
private static final Ansi4BitColor COLOR_NONE = Ansi4BitColor.DEFAULT;
|
||||
|
||||
private static Function<Level, AnsiColor> colorFactory = (level -> {
|
||||
private static Function<Level, Ansi4BitColor> colorFactory = (level -> {
|
||||
switch (level) {
|
||||
case DEBUG:
|
||||
case INFO:
|
||||
return AnsiColor.GREEN;
|
||||
return Ansi4BitColor.GREEN;
|
||||
case WARN:
|
||||
return AnsiColor.YELLOW;
|
||||
return Ansi4BitColor.YELLOW;
|
||||
case ERROR:
|
||||
return AnsiColor.RED;
|
||||
return Ansi4BitColor.RED;
|
||||
case TRACE:
|
||||
return AnsiColor.MAGENTA;
|
||||
return Ansi4BitColor.MAGENTA;
|
||||
default:
|
||||
return COLOR_NONE;
|
||||
}
|
||||
@ -54,7 +54,7 @@ public class ConsoleColorLog extends ConsoleLog {
|
||||
*
|
||||
* @param colorFactory 颜色工厂函数
|
||||
*/
|
||||
public static void setColorFactory(final Function<Level, AnsiColor> colorFactory) {
|
||||
public static void setColorFactory(final Function<Level, Ansi4BitColor> colorFactory) {
|
||||
ConsoleColorLog.colorFactory = colorFactory;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user