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