Skip to content

Commit 0c84646

Browse files
committed
终端彩色输出 工具类 ok
1 parent a2a9d73 commit 0c84646

File tree

8 files changed

+392
-15
lines changed

8 files changed

+392
-15
lines changed

banner.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2+
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3+
. . . . . . . . . . . . . . . . . . # # # # . . . . . . . . . . . . . . . . . .
4+
. . . . . . . . . . . . . . . . . . # j j # . . . . . . . . . . . . . . . . . .
5+
. . . . . . . . . . . . . . . . . . t # K j . . . . . . . . . . . . . . . . . .
6+
. . . . . . . . . . . . . . . . . j L ; ; G j . . . . . . . . . . . . . . . .
7+
. . . . . . . . . . . . . . . . # : : ; # : : # . . . . . . . . . . . . . . . .
8+
. # # # . . . . . . . . . . # , G ; ; ; ; ; ; ; i # . . . . . . . . . . # # # .
9+
. . . # # # . . . . . . # ; ; ; ; ; ; , ; ; ; ; ; # . . . . . . # # # . . .
10+
. . . . # # # # # # # . ; ; ; ; ; ; ; ; ; ; E ; ; ; # # # # # # # . . . .
11+
. . . . . # , # ; # ; ; ; ; ; ; ; ; ; ; ; ; ; ; W ; # ; # , # . . . . .
12+
. . . . . . # # ; ; j ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; G j ; ; # # . . . . . .
13+
. . . . . . . . # ; ; # ; ; ; ; ; ; W ; ; ; ; ; ; ; ; ; ; ; ; # . . . . . . . .
14+
. . . . . . . . # ; ; ; ; ; ; ; ; W ; ; ; ; ; ; ; ; ; ; ; ; ; # . . . . . . . .
15+
. . . . . . . . . # ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; # . . . . . . . . .
16+
. . . . . . . . . . : ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; : . . . . . . . . . .
17+
. . . . . . . . . . . ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; . . . . . . . . . . .
18+
. . . . . . . . . . . ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; . . . . . . . . . . .
19+
. . . . . . . . . . . W ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; W . . . . . . . . . . .
20+
. . . . . . . . . . . t ; ; ; ; ; ; ; i ; ; ; ; ; ; ; ; ; . . . . . . . . . .
21+
. . . . . . . . . . . . ; ; ; ; ; ; ; f f f t t K j # . . . . . . . . .
22+
. . . . . . . . . . . . ; E ; ; E t K t t t K W E # . . . . . . . . .
23+
. . . . . . . . . . . . # i ; ; t t t t j j . . . . . . . . . .
24+
. . . . . . . . . . . . . ; ; t t ; ; ; ; ; f L L f f . . . . . . . . . . .
25+
. . . . . . . . . . . . . . ; ; ; ; # # ; ; ; ; # . . . . . . . . . . . .
26+
. . . . . . . . . . . . . . # ; ; ; E E E E ; ; ; # . . . . . . . . . . . . . .
27+
. . . . . . . . . . . . . . . # ; ; W D D D ; ; # . . . . . . . . . . . . . . .
28+
. . . . . . . . . . . . . . . . # ; ; D D # ; # . . . . . . . . . . . . . . . .
29+
. . . . . . . . . . . . . . t t t j ; ; j t D i # . . . . . . . . . . . . . .
30+
. . . . . . . . . . . . . . . . j f K , ; K f # . . . . . . . . . . . . . . . .
31+
. . . . . . . . . . . . . . . # ; W K K K K # ; # . . . . . . . . . . . . . . .
32+
. . . . . . . . . . . . . . # ; # . ; t # . # ; # . . . . . . . . . . . . . .
33+
. . . . . . . . . . . . . # # W . . ; j ; t # . # # # . . . . . . . . . . . . .
34+
. . . . . . . . . . . . . . : . . t # # j j t . . # . . . . . . . . . . . . . .
35+
. . . . . . . . . . . . . . . . . . # # # ; ; . . . . . . . . . . . . . . . . .
36+
. . . . . . . . . . . . . . i i i i t # i # i i i i . . . . . . . . . . . . . .
37+
. . . . . . . . . . . . . . . . ; i i i i i i i . . . . . . . . . . . . . . . .
38+
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ginyu.png

-871 KB
Loading

pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
<version>4.13</version>
3434
<scope>test</scope>
3535
</dependency>
36-
36+
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
37+
<dependency>
38+
<groupId>commons-io</groupId>
39+
<artifactId>commons-io</artifactId>
40+
<version>2.8.0</version>
41+
</dependency>
3742
</dependencies>
3843

3944

src/main/java/common/Consoles.java

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package common;
2+
3+
import utils.OSUtils;
4+
5+
import java.util.logging.Level;
6+
import java.util.regex.Matcher;
7+
8+
/**
9+
* @author: junjiexun
10+
* @date: 2020/10/15 10:12 上午
11+
* @description: 负责终端彩色输出
12+
*/
13+
public abstract class Consoles {
14+
15+
private static boolean enableColor = true;
16+
17+
public static Level LEVEL = Level.FINEST;
18+
19+
static {
20+
if (System.console() != null) {
21+
enableColor = true;
22+
// windows dos, do not support color
23+
if (OSUtils.isWindows()) {
24+
enableColor = false;
25+
}
26+
}
27+
// cygwin and mingw support color
28+
if (OSUtils.isCygwinOrMinGW()) {
29+
enableColor = true;
30+
}
31+
}
32+
33+
private static final String RESET = "\033[0m";
34+
35+
private static final int DEFAULT = 39;
36+
private static final int WHITE = 30;
37+
private static final int RED = 31;
38+
private static final int GREEN = 32;
39+
private static final int YELLOW = 33;
40+
private static final int BLUE = 34;
41+
private static final int MAGENTA = 35;
42+
private static final int CYAN = 36;
43+
private static final int GRAY = 37;
44+
45+
private static String colorStr(String msg, int colorCode) {
46+
return String.format("\033[%sm%s%s", colorCode, msg, RESET);
47+
}
48+
49+
public static void blue(String msg) {
50+
System.out.println(colorStr(msg, BLUE));
51+
}
52+
53+
public static void cyan(String msg) {
54+
System.out.println(colorStr(msg, CYAN));
55+
}
56+
57+
public static void gray(String msg) {
58+
System.out.println(colorStr(msg, GRAY));
59+
}
60+
61+
public static void red(String msg) {
62+
System.out.println(colorStr(msg, RED));
63+
}
64+
65+
public static void green(String msg) {
66+
System.out.println(colorStr(msg, GREEN));
67+
}
68+
69+
public static void yellow(String msg) {
70+
System.out.println(colorStr(msg, YELLOW));
71+
}
72+
73+
public static void magenta(String msg) {
74+
System.out.println(colorStr(msg, MAGENTA));
75+
}
76+
77+
public static void white(String msg) {
78+
System.out.println(colorStr(msg, WHITE));
79+
}
80+
81+
public static void trace(String msg) {
82+
if (canLog(Level.FINEST)) {
83+
if (enableColor) {
84+
cyan(msg);
85+
} else {
86+
System.out.println(msg);
87+
}
88+
}
89+
}
90+
91+
public static void trace(String format, Object... arguments) {
92+
trace(format(format, arguments));
93+
}
94+
95+
public static void debug(String msg) {
96+
if (canLog(Level.FINER)) {
97+
if (enableColor) {
98+
gray(msg);
99+
} else {
100+
System.out.println(msg);
101+
}
102+
}
103+
}
104+
105+
public static void debug(String format, Object... arguments) {
106+
debug(format(format, arguments));
107+
}
108+
109+
public static void info(String msg) {
110+
if (canLog(Level.CONFIG)) {
111+
if (enableColor) {
112+
green(msg);
113+
} else {
114+
System.out.println(msg);
115+
}
116+
}
117+
}
118+
119+
public static void info(String format, Object... arguments) {
120+
info(format(format, arguments));
121+
}
122+
123+
public static void warn(String msg) {
124+
if (canLog(Level.WARNING)) {
125+
if (enableColor) {
126+
yellow(msg);
127+
} else {
128+
System.out.println(msg);
129+
}
130+
}
131+
}
132+
133+
public static void warn(String format, Object... arguments) {
134+
warn(format(format, arguments));
135+
}
136+
137+
public static void error(String msg) {
138+
if (canLog(Level.SEVERE)) {
139+
if (enableColor) {
140+
red(msg);
141+
} else {
142+
System.out.println(msg);
143+
}
144+
}
145+
}
146+
147+
public static void error(String format, Object... arguments) {
148+
error(format(format, arguments));
149+
}
150+
151+
private static String format(String from, Object... arguments) {
152+
if (from != null) {
153+
String computed = from;
154+
if (arguments != null && arguments.length != 0) {
155+
for (Object argument : arguments) {
156+
computed = computed.replaceFirst("\\{\\}", Matcher.quoteReplacement(argument.toString()));
157+
}
158+
}
159+
return computed;
160+
}
161+
return null;
162+
}
163+
164+
private static boolean canLog(Level level) {
165+
return level.intValue() >= LEVEL.intValue();
166+
}
167+
168+
}

src/main/java/common/Constants.java

+10
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ public abstract class Constants {
2727
public static final String ARRAYS_FLAG = "*";
2828

2929
public static final String[] STR_EMPTY_ARRAY = new String[]{};
30+
31+
public static final Integer TRACE = 0;
32+
33+
public static final Integer DEBUG = 1;
34+
35+
public static final Integer INFO = 2;
36+
37+
public static final Integer WARN = 3;
38+
39+
public static final Integer ERROR = 4;
3040
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package common;
2+
3+
/**
4+
* Enum of supported operating systems.
5+
*
6+
*/
7+
public enum PlatformEnum {
8+
/**
9+
* Microsoft Windows
10+
*/
11+
WINDOWS,
12+
/**
13+
* A flavor of Linux
14+
*/
15+
LINUX,
16+
/**
17+
* macOS (OS X)
18+
*/
19+
MACOSX,
20+
21+
UNKNOWN
22+
}

src/main/java/utils/OSUtils.java

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package utils;
2+
3+
import common.PlatformEnum;
4+
5+
import java.util.Locale;
6+
7+
/**
8+
*
9+
* @author hengyunabc 2018-11-08
10+
*
11+
*/
12+
public class OSUtils {
13+
private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
14+
private static final String OPERATING_SYSTEM_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
15+
private static final String UNKNOWN = "unknown";
16+
17+
static PlatformEnum platform;
18+
19+
static String arch;
20+
21+
static {
22+
if (OPERATING_SYSTEM_NAME.startsWith("linux")) {
23+
platform = PlatformEnum.LINUX;
24+
} else if (OPERATING_SYSTEM_NAME.startsWith("mac") || OPERATING_SYSTEM_NAME.startsWith("darwin")) {
25+
platform = PlatformEnum.MACOSX;
26+
} else if (OPERATING_SYSTEM_NAME.startsWith("windows")) {
27+
platform = PlatformEnum.WINDOWS;
28+
} else {
29+
platform = PlatformEnum.UNKNOWN;
30+
}
31+
32+
arch = normalizeArch(OPERATING_SYSTEM_ARCH);
33+
}
34+
35+
private OSUtils() {
36+
}
37+
38+
public static boolean isWindows() {
39+
return platform == PlatformEnum.WINDOWS;
40+
}
41+
42+
public static boolean isLinux() {
43+
return platform == PlatformEnum.LINUX;
44+
}
45+
46+
public static boolean isMac() {
47+
return platform == PlatformEnum.MACOSX;
48+
}
49+
50+
public static boolean isCygwinOrMinGW() {
51+
if (isWindows()) {
52+
if ((System.getenv("MSYSTEM") != null && System.getenv("MSYSTEM").startsWith("MINGW"))
53+
|| "/bin/bash".equals(System.getenv("SHELL"))) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
}
59+
60+
public static String arch() {
61+
return arch;
62+
}
63+
64+
public static boolean isArm() {
65+
return "arm_32".equals(arch);
66+
}
67+
68+
private static String normalizeArch(String value) {
69+
value = normalize(value);
70+
if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) {
71+
return "x86_64";
72+
}
73+
if (value.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) {
74+
return "x86_32";
75+
}
76+
if (value.matches("^(ia64w?|itanium64)$")) {
77+
return "itanium_64";
78+
}
79+
if ("ia64n".equals(value)) {
80+
return "itanium_32";
81+
}
82+
if (value.matches("^(sparc|sparc32)$")) {
83+
return "sparc_32";
84+
}
85+
if (value.matches("^(sparcv9|sparc64)$")) {
86+
return "sparc_64";
87+
}
88+
if (value.matches("^(arm|arm32)$")) {
89+
return "arm_32";
90+
}
91+
if ("aarch64".equals(value)) {
92+
return "aarch_64";
93+
}
94+
if (value.matches("^(mips|mips32)$")) {
95+
return "mips_32";
96+
}
97+
if (value.matches("^(mipsel|mips32el)$")) {
98+
return "mipsel_32";
99+
}
100+
if ("mips64".equals(value)) {
101+
return "mips_64";
102+
}
103+
if ("mips64el".equals(value)) {
104+
return "mipsel_64";
105+
}
106+
if (value.matches("^(ppc|ppc32)$")) {
107+
return "ppc_32";
108+
}
109+
if (value.matches("^(ppcle|ppc32le)$")) {
110+
return "ppcle_32";
111+
}
112+
if ("ppc64".equals(value)) {
113+
return "ppc_64";
114+
}
115+
if ("ppc64le".equals(value)) {
116+
return "ppcle_64";
117+
}
118+
if ("s390".equals(value)) {
119+
return "s390_32";
120+
}
121+
if ("s390x".equals(value)) {
122+
return "s390_64";
123+
}
124+
125+
return UNKNOWN;
126+
}
127+
128+
private static String normalize(String value) {
129+
if (value == null) {
130+
return "";
131+
}
132+
return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
133+
}
134+
}

0 commit comments

Comments
 (0)