This commit is contained in:
Looly 2022-09-15 02:45:05 +08:00
parent a081bdc5e1
commit cf0fc45080
9 changed files with 221 additions and 157 deletions

View File

@ -1,5 +1,7 @@
package cn.hutool.core.lang.ansi; package cn.hutool.core.lang.ansi;
import cn.hutool.core.text.StrUtil;
/** /**
* ANSI背景颜色枚举 * ANSI背景颜色枚举
* *
@ -8,102 +10,112 @@ package cn.hutool.core.lang.ansi;
* @author Phillip Webb, Geoffrey Chandler * @author Phillip Webb, Geoffrey Chandler
* @since 5.8.0 * @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; this.code = code;
} }
/**
* 获取ANSI颜色代码
*
* @return 颜色代码
*/
@Override
public int getCode() {
return this.code;
}
@Override @Override
public String toString() { public String toString() {
return this.code; return StrUtil.toString(this.code);
} }
} }

View File

@ -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);
}
}

View File

@ -1,6 +1,7 @@
package cn.hutool.core.lang.ansi; package cn.hutool.core.lang.ansi;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
/** /**
* ANSI 8-bit前景或背景色即8位编码共256种颜色2^8 <br> * ANSI 8-bit前景或背景色即8位编码共256种颜色2^8 <br>
@ -18,7 +19,7 @@ import cn.hutool.core.lang.Assert;
* @see #background(int) * @see #background(int)
* @since 5.8.0 * @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_FORE = "38;5;";
private static final String PREFIX_BACK = "48;5;"; private static final String PREFIX_BACK = "48;5;";
@ -29,7 +30,7 @@ public final class Ansi8BitColor implements AnsiElement {
* @param code 颜色代码(0-255) * @param code 颜色代码(0-255)
* @return 前景色ANSI颜色实例 * @return 前景色ANSI颜色实例
*/ */
public static Ansi8BitColor foreground(final int code) { public static Ansi8BitColor foreground(int code) {
return new Ansi8BitColor(PREFIX_FORE, code); return new Ansi8BitColor(PREFIX_FORE, code);
} }
@ -39,7 +40,7 @@ public final class Ansi8BitColor implements AnsiElement {
* @param code 颜色代码(0-255) * @param code 颜色代码(0-255)
* @return 背景色ANSI颜色实例 * @return 背景色ANSI颜色实例
*/ */
public static Ansi8BitColor background(final int code) { public static Ansi8BitColor background(int code) {
return new Ansi8BitColor(PREFIX_BACK, code); return new Ansi8BitColor(PREFIX_BACK, code);
} }
@ -53,22 +54,32 @@ public final class Ansi8BitColor implements AnsiElement {
* @param code 颜色代码(0-255) * @param code 颜色代码(0-255)
* @throws IllegalArgumentException 颜色代码不在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"); Assert.isTrue(code >= 0 && code <= 255, "Code must be between 0 and 255");
this.prefix = prefix; this.prefix = prefix;
this.code = code; this.code = code;
} }
/**
* 获取颜色代码(0-255)
*
* @return 颜色代码(0 - 255)
*/
@Override @Override
public boolean equals(final Object obj) { public int getCode() {
return this.code;
}
@Override
public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null || getClass() != obj.getClass()) { if (obj == null || getClass() != obj.getClass()) {
return false; return false;
} }
final Ansi8BitColor other = (Ansi8BitColor) obj; Ansi8BitColor other = (Ansi8BitColor) obj;
return this.prefix.equals(other.prefix) && this.code == other.code; return ObjUtil.equals(this.prefix, other.prefix) && this.code == other.code;
} }
@Override @Override

View File

@ -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;
}
}

View File

@ -9,6 +9,13 @@ package cn.hutool.core.lang.ansi;
*/ */
public interface AnsiElement { public interface AnsiElement {
/**
* 获取ANSI代码
* @return ANSI代码
* @since 5.8.7
*/
int getCode();
/** /**
* @return ANSI转义编码 * @return ANSI转义编码
*/ */

View File

@ -10,7 +10,7 @@ public abstract class AnsiEncoder {
private static final String ENCODE_JOIN = ";"; private static final String ENCODE_JOIN = ";";
private static final String ENCODE_START = "\033["; private static final String ENCODE_START = "\033[";
private static final String ENCODE_END = "m"; 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}会被转换为编码形式 * 创建ANSI字符串参数中的{@link AnsiElement}会被转换为编码形式

View File

@ -1,5 +1,7 @@
package cn.hutool.core.lang.ansi; package cn.hutool.core.lang.ansi;
import cn.hutool.core.text.StrUtil;
/** /**
* ANSI文本样式风格枚举 * 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; this.code = code;
} }
/**
* 获取ANSI文本样式风格代码
*
* @return 文本样式风格代码
*/
@Override
public int getCode() {
return this.code;
}
@Override @Override
public String toString() { public String toString() {
return this.code; return StrUtil.toString(this.code);
} }
} }

View File

@ -7,7 +7,7 @@ public class AnsiEncoderTest {
@Test @Test
public void encodeTest(){ 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); Assert.assertEquals("\u001B[32mHutool test\u001B[0;39m", encode);
} }
} }

View File

@ -1,7 +1,7 @@
package cn.hutool.log.dialect.console; package cn.hutool.log.dialect.console;
import cn.hutool.core.date.DateUtil; 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.lang.ansi.AnsiEncoder;
import cn.hutool.core.reflect.ClassUtil; import cn.hutool.core.reflect.ClassUtil;
import cn.hutool.core.text.StrUtil; 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) { switch (level) {
case DEBUG: case DEBUG:
case INFO: case INFO:
return AnsiColor.GREEN; return Ansi4BitColor.GREEN;
case WARN: case WARN:
return AnsiColor.YELLOW; return Ansi4BitColor.YELLOW;
case ERROR: case ERROR:
return AnsiColor.RED; return Ansi4BitColor.RED;
case TRACE: case TRACE:
return AnsiColor.MAGENTA; return Ansi4BitColor.MAGENTA;
default: default:
return COLOR_NONE; return COLOR_NONE;
} }
@ -54,7 +54,7 @@ public class ConsoleColorLog extends ConsoleLog {
* *
* @param colorFactory 颜色工厂函数 * @param colorFactory 颜色工厂函数
*/ */
public static void setColorFactory(final Function<Level, AnsiColor> colorFactory) { public static void setColorFactory(final Function<Level, Ansi4BitColor> colorFactory) {
ConsoleColorLog.colorFactory = colorFactory; ConsoleColorLog.colorFactory = colorFactory;
} }