View Javadoc
1   package org.apache.maven.shared.utils.logging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.fusesource.jansi.Ansi;
23  import org.fusesource.jansi.Ansi.Color;
24  
25  import java.util.Locale;
26  
27  /**
28   * Configurable message styles.
29   */
30  enum Style
31  {
32  
33      DEBUG(   "bold,cyan"   ),
34      INFO(    "bold,blue"   ),
35      WARNING( "bold,yellow" ),
36      ERROR(   "bold,red"    ),
37      SUCCESS( "bold,green"  ),
38      FAILURE( "bold,red"    ),
39      STRONG(  "bold"        ),
40      MOJO(    "green"       ),
41      PROJECT( "cyan"        );
42  
43      private final boolean bold;
44  
45      private final boolean bright;
46  
47      private final Color color;
48  
49      private final boolean bgBright;
50  
51      private final Color bgColor;
52  
53      Style( String defaultValue )
54      {
55          boolean currentBold = false;
56          boolean currentBright = false;
57          Color currentColor = null;
58          boolean currentBgBright = false;
59          Color currentBgColor = null;
60  
61          String value = System.getProperty( "style." + name().toLowerCase( Locale.ENGLISH ),
62                                             defaultValue ).toLowerCase( Locale.ENGLISH );
63  
64          for ( String token : value.split( "," ) )
65          {
66              if ( "bold".equals( token ) )
67              {
68                  currentBold = true;
69              }
70              else if ( token.startsWith( "bg" ) )
71              {
72                  token = token.substring( 2 );
73                  if ( token.startsWith( "bright" ) )
74                  {
75                      currentBgBright = true;
76                      token = token.substring( 6 );
77                  }
78                  currentBgColor = toColor( token );
79              }
80              else
81              {
82                  if ( token.startsWith( "bright" ) )
83                  {
84                      currentBright = true;
85                      token = token.substring( 6 );
86                  }
87                  currentColor = toColor( token );
88              }
89          }
90  
91          this.bold = currentBold;
92          this.bright = currentBright;
93          this.color = currentColor;
94          this.bgBright = currentBgBright;
95          this.bgColor = currentBgColor;
96      }
97  
98      private static Color toColor( String token )
99      {
100         for ( Color color : Color.values() )
101         {
102             if ( color.toString().equalsIgnoreCase( token ) )
103             {
104                 return color;
105             }
106         }
107         return null;
108     }
109 
110     Ansi apply( Ansi ansi )
111     {
112         if ( bold )
113         {
114             ansi.bold();
115         }
116         if ( color != null )
117         {
118             if ( bright )
119             {
120                 ansi.fgBright( color );
121             }
122             else
123             {
124                 ansi.fg( color );
125             }
126         }
127         if ( bgColor != null )
128         {
129             if ( bgBright )
130             {
131                 ansi.bgBright( bgColor );
132             }
133             else
134             {
135                 ansi.bg( bgColor );
136             }
137         }
138         return ansi;
139     }
140 
141     @Override
142     public String toString()
143     {
144         if ( !bold && color == null && bgColor == null )
145         {
146             return name();
147         }
148         StringBuilder sb = new StringBuilder( name() + '=' );
149         if ( bold )
150         {
151             sb.append( "bold" );
152         }
153         if ( color != null )
154         {
155             if ( sb.length() > 0 )
156             {
157                 sb.append(  ',' );
158             }
159             if ( bright )
160             {
161                 sb.append( "bright" );
162             }
163             sb.append( color.name() );
164         }
165         if ( bgColor != null )
166         {
167             if ( sb.length() > 0 )
168             {
169                 sb.append(  ',' );
170             }
171             sb.append( "bg" );
172             if ( bgBright )
173             {
174                 sb.append( "bright" );
175             }
176             sb.append( bgColor.name() );
177         }
178         return sb.toString();
179     }
180 
181 }