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  
24  /**
25   * Message builder implementation that supports ANSI colors through
26   * <a href="http://fusesource.github.io/jansi/">Jansi</a> with configurable styles through {@link Style}.
27   */
28  class AnsiMessageBuilder
29      implements MessageBuilder, LoggerLevelRenderer
30  {
31      private Ansi ansi;
32  
33      AnsiMessageBuilder()
34      {
35          this( Ansi.ansi() );
36      }
37  
38      AnsiMessageBuilder( StringBuilder builder )
39      {
40          this( Ansi.ansi( builder ) );
41      }
42  
43      AnsiMessageBuilder( int size )
44      {
45          this( Ansi.ansi( size ) );
46      }
47  
48      AnsiMessageBuilder( Ansi ansi )
49      {
50          this.ansi = ansi;
51      }
52  
53      public String debug( String level )
54      {
55          return Style.DEBUG.apply( ansi ).a( level ).reset().toString();
56      }
57  
58      public String info( String level )
59      {
60          return Style.INFO.apply( ansi ).a( level ).reset().toString();
61      }
62  
63      public String warning( String level )
64      {
65          return Style.WARNING.apply( ansi ).a( level ).reset().toString();
66      }
67  
68      public String error( String level )
69      {
70          return Style.ERROR.apply( ansi ).a( level ).reset().toString();
71      }
72  
73      public AnsiMessageBuilder success( Object message )
74      {
75          Style.SUCCESS.apply( ansi ).a( message ).reset();
76          return this;
77      }
78  
79      public AnsiMessageBuilder warning( Object message )
80      {
81          Style.WARNING.apply( ansi ).a( message ).reset();
82          return this;
83      }
84  
85      public AnsiMessageBuilder failure( Object message )
86      {
87          Style.FAILURE.apply( ansi ).a( message ).reset();
88          return this;
89      }
90  
91      public AnsiMessageBuilder strong( Object message )
92      {
93          Style.STRONG.apply( ansi ).a( message ).reset();
94          return this;
95      }
96  
97      public AnsiMessageBuilder mojo( Object message )
98      {
99          Style.MOJO.apply( ansi ).a( message ).reset();
100         return this;
101     }
102 
103     public AnsiMessageBuilder project( Object message )
104     {
105         Style.PROJECT.apply( ansi ).a( message ).reset();
106         return this;
107     }
108 
109     public AnsiMessageBuilder a( char[] value, int offset, int len )
110     {
111         ansi.a( value, offset, len );
112         return this;
113     }
114 
115     public AnsiMessageBuilder a( char[] value )
116     {
117         ansi.a( value );
118         return this;
119     }
120 
121     public AnsiMessageBuilder a( CharSequence value, int start, int end )
122     {
123         ansi.a( value, start, end );
124         return this;
125     }
126 
127     public AnsiMessageBuilder a( CharSequence value )
128     {
129         ansi.a( value );
130         return this;
131     }
132 
133     public AnsiMessageBuilder a( Object value )
134     {
135         ansi.a( value );
136         return this;
137     }
138 
139     public AnsiMessageBuilder newline()
140     {
141         ansi.newline();
142         return this;
143     }
144 
145     public AnsiMessageBuilder format( String pattern, Object... args )
146     {
147         ansi.format( pattern, args );
148         return this;
149     }
150 
151     @Override
152     public String toString()
153     {
154         return ansi.toString();
155     }
156 }