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
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 AnsiMessageBuilder debug( Object message )
54      {
55          Style.DEBUG.apply( ansi ).a( message ).reset();
56          return this;
57      }
58  
59      public AnsiMessageBuilder info( Object message )
60      {
61          Style.INFO.apply( ansi ).a( message ).reset();
62          return this;
63      }
64  
65      public AnsiMessageBuilder warning( Object message )
66      {
67          Style.WARNING.apply( ansi ).a( message ).reset();
68          return this;
69      }
70  
71      public AnsiMessageBuilder error( Object message )
72      {
73          Style.ERROR.apply( ansi ).a( message ).reset();
74          return this;
75      }
76  
77      public AnsiMessageBuilder success( Object message )
78      {
79          Style.SUCCESS.apply( ansi ).a( message ).reset();
80          return this;
81      }
82  
83      public AnsiMessageBuilder failure( Object message )
84      {
85          Style.FAILURE.apply( ansi ).a( message ).reset();
86          return this;
87      }
88  
89      public AnsiMessageBuilder strong( Object message )
90      {
91          Style.STRONG.apply( ansi ).a( message ).reset();
92          return this;
93      }
94  
95      public AnsiMessageBuilder mojo( Object message )
96      {
97          Style.MOJO.apply( ansi ).a( message ).reset();
98          return this;
99      }
100 
101     public AnsiMessageBuilder project( Object message )
102     {
103         Style.PROJECT.apply( ansi ).a( message ).reset();
104         return this;
105     }
106 
107     public AnsiMessageBuilder a( char[] value, int offset, int len )
108     {
109         ansi.a( value, offset, len );
110         return this;
111     }
112 
113     public AnsiMessageBuilder a( char[] value )
114     {
115         ansi.a( value );
116         return this;
117     }
118 
119     public AnsiMessageBuilder a( CharSequence value, int start, int end )
120     {
121         ansi.a( value, start, end );
122         return this;
123     }
124 
125     public AnsiMessageBuilder a( CharSequence value )
126     {
127         ansi.a( value );
128         return this;
129     }
130 
131     public AnsiMessageBuilder a( Object value )
132     {
133         ansi.a( value );
134         return this;
135     }
136 
137     public AnsiMessageBuilder newline()
138     {
139         ansi.newline();
140         return this;
141     }
142 
143     public AnsiMessageBuilder format( String pattern, Object... args )
144     {
145         ansi.format( pattern, args );
146         return this;
147     }
148 
149     @Override
150     public String toString()
151     {
152         return ansi.toString();
153     }
154 }