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.cli.jansi;
20  
21  import org.apache.maven.api.annotations.Experimental;
22  import org.apache.maven.api.annotations.Nonnull;
23  import org.apache.maven.api.services.MessageBuilder;
24  import org.fusesource.jansi.Ansi;
25  
26  @Experimental
27  public class JansiMessageBuilder implements MessageBuilder {
28      private final Ansi ansi;
29      private StringBuilder sb;
30  
31      @SuppressWarnings("magicnumber")
32      public JansiMessageBuilder() {
33          this.sb = new StringBuilder(80);
34          this.ansi = Ansi.ansi();
35      }
36  
37      public JansiMessageBuilder(StringBuilder sb) {
38          this.sb = sb;
39          this.ansi = Ansi.ansi(sb);
40      }
41  
42      @Override
43      @Nonnull
44      public MessageBuilder trace(Object o) {
45          return style(Style.TRACE, o);
46      }
47  
48      @Override
49      @Nonnull
50      public MessageBuilder debug(Object o) {
51          return style(Style.DEBUG, o);
52      }
53  
54      @Override
55      @Nonnull
56      public MessageBuilder info(Object o) {
57          return style(Style.INFO, o);
58      }
59  
60      @Override
61      @Nonnull
62      public MessageBuilder warning(Object o) {
63          return style(Style.WARNING, o);
64      }
65  
66      @Override
67      @Nonnull
68      public MessageBuilder error(Object o) {
69          return style(Style.ERROR, o);
70      }
71  
72      @Override
73      @Nonnull
74      public MessageBuilder success(Object o) {
75          return style(Style.SUCCESS, o);
76      }
77  
78      @Override
79      @Nonnull
80      public MessageBuilder failure(Object o) {
81          return style(Style.FAILURE, o);
82      }
83  
84      @Override
85      @Nonnull
86      public MessageBuilder strong(Object o) {
87          return style(Style.STRONG, o);
88      }
89  
90      @Override
91      @Nonnull
92      public MessageBuilder mojo(Object o) {
93          return style(Style.MOJO, o);
94      }
95  
96      @Override
97      @Nonnull
98      public MessageBuilder project(Object o) {
99          return style(Style.PROJECT, o);
100     }
101 
102     private MessageBuilder style(Style style, Object o) {
103         style.apply(ansi).a(o).reset();
104         return this;
105     }
106 
107     @Override
108     @Nonnull
109     public MessageBuilder a(char[] chars, int i, int i1) {
110         ansi.a(chars, i, i1);
111         return this;
112     }
113 
114     @Override
115     @Nonnull
116     public MessageBuilder a(char[] chars) {
117         ansi.a(chars);
118         return this;
119     }
120 
121     @Override
122     @Nonnull
123     public MessageBuilder a(CharSequence charSequence, int i, int i1) {
124         ansi.a(charSequence, i, i1);
125         return this;
126     }
127 
128     @Override
129     @Nonnull
130     public MessageBuilder a(CharSequence charSequence) {
131         ansi.a(charSequence);
132         return this;
133     }
134 
135     @Override
136     @Nonnull
137     public MessageBuilder a(Object o) {
138         ansi.a(o);
139         return this;
140     }
141 
142     @Override
143     @Nonnull
144     public MessageBuilder newline() {
145         ansi.newline();
146         return this;
147     }
148 
149     @Override
150     @Nonnull
151     public MessageBuilder format(String s, Object... objects) {
152         ansi.format(s, objects);
153         return this;
154     }
155 
156     @Override
157     @Nonnull
158     public String build() {
159         return ansi.toString();
160     }
161 
162     @Override
163     public String toString() {
164         return build();
165     }
166 
167     @Override
168     public void setLength(int length) {
169         sb.setLength(length);
170     }
171 }