1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cli.jansi;
20
21 import java.util.Locale;
22
23 import org.fusesource.jansi.Ansi;
24 import org.fusesource.jansi.Ansi.Color;
25
26
27
28
29
30 enum Style {
31 TRACE("bold,magenta"),
32 DEBUG("bold,cyan"),
33 INFO("bold,blue"),
34 WARNING("bold,yellow"),
35 ERROR("bold,red"),
36 SUCCESS("bold,green"),
37 FAILURE("bold,red"),
38 STRONG("bold"),
39 MOJO("green"),
40 PROJECT("cyan");
41
42 private final boolean bold;
43
44 private final boolean bright;
45
46 private final Color color;
47
48 private final boolean bgBright;
49
50 private final Color bgColor;
51
52 Style(String defaultValue) {
53 boolean currentBold = false;
54 boolean currentBright = false;
55 Color currentColor = null;
56 boolean currentBgBright = false;
57 Color currentBgColor = null;
58
59 String value = System.getProperty("style." + name().toLowerCase(Locale.ENGLISH), defaultValue)
60 .toLowerCase(Locale.ENGLISH);
61
62 for (String token : value.split(",")) {
63 if ("bold".equals(token)) {
64 currentBold = true;
65 } else if (token.startsWith("bg")) {
66 token = token.substring(2);
67 if (token.startsWith("bright")) {
68 currentBgBright = true;
69 token = token.substring(6);
70 }
71 currentBgColor = toColor(token);
72 } else {
73 if (token.startsWith("bright")) {
74 currentBright = true;
75 token = token.substring(6);
76 }
77 currentColor = toColor(token);
78 }
79 }
80
81 this.bold = currentBold;
82 this.bright = currentBright;
83 this.color = currentColor;
84 this.bgBright = currentBgBright;
85 this.bgColor = currentBgColor;
86 }
87
88 private static Color toColor(String token) {
89 for (Color color : Color.values()) {
90 if (color.toString().equalsIgnoreCase(token)) {
91 return color;
92 }
93 }
94 return null;
95 }
96
97 Ansi apply(Ansi ansi) {
98 if (bold) {
99 ansi.bold();
100 }
101 if (color != null) {
102 if (bright) {
103 ansi.fgBright(color);
104 } else {
105 ansi.fg(color);
106 }
107 }
108 if (bgColor != null) {
109 if (bgBright) {
110 ansi.bgBright(bgColor);
111 } else {
112 ansi.bg(bgColor);
113 }
114 }
115 return ansi;
116 }
117
118 @Override
119 public String toString() {
120 if (!bold && color == null && bgColor == null) {
121 return name();
122 }
123 StringBuilder sb = new StringBuilder(name() + '=');
124 if (bold) {
125 sb.append("bold");
126 }
127 if (color != null) {
128 if (sb.length() > 0) {
129 sb.append(',');
130 }
131 if (bright) {
132 sb.append("bright");
133 }
134 sb.append(color.name());
135 }
136 if (bgColor != null) {
137 if (sb.length() > 0) {
138 sb.append(',');
139 }
140 sb.append("bg");
141 if (bgBright) {
142 sb.append("bright");
143 }
144 sb.append(bgColor.name());
145 }
146 return sb.toString();
147 }
148 }