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