View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Configurable message styles.
28   * @since 4.0.0
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 }