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