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.AnsiConsole;
24  
25  /**
26   * Colored message utils, to manage colors consistently across plugins (only if Maven version is at least 3.4.0).
27   * For Maven version before 3.4.0, message built with this util will never add color.
28   * <p>
29   * Internally, <a href="http://fusesource.github.io/jansi/">Jansi</a> is used to render
30   * <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#Colors">ANSI colors</a> on any platform.
31   */
32  public class MessageUtils
33  {
34      private static final boolean JANSI;
35  
36      static
37      {
38          boolean jansi = true;
39          try
40          {
41              // JAnsi is provided by Maven core since 3.4.0
42              Class.forName( "org.fusesource.jansi.Ansi" );
43          }
44          catch ( ClassNotFoundException cnfe )
45          {
46              jansi = false;
47          }
48          JANSI = jansi;
49      }
50  
51      /**
52       * Install color support.
53       * This method is called by Maven core, and calling it is not necessary in plugins. 
54       */
55      public static void systemInstall()
56      {
57          if ( JANSI )
58          {
59              AnsiConsole.systemInstall();
60          }
61      }
62  
63      /**
64       * Undo a previous {@link #systemInstall()}.  If {@link #systemInstall()} was called
65       * multiple times, {@link #systemUninstall()} must be called call the same number of times before
66       * it is actually uninstalled.
67       */
68      public static void systemUninstall()
69      {
70          if ( JANSI )
71          {
72              AnsiConsole.systemUninstall();
73          }
74      }
75  
76      /**
77       * Enables message color (if JAnsi is available).
78       * @param flag
79       */
80      public static void setColorEnabled( boolean flag )
81      {
82          if ( JANSI )
83          {
84              Ansi.setEnabled( flag );
85          }
86      }
87  
88      /**
89       * Is message color enabled: requires JAnsi available (through Maven) and the color has not been disabled.
90       */
91      public static boolean isColorEnabled()
92      {
93          return JANSI ? Ansi.isEnabled() : false;
94      }
95  
96      /**
97       * Create a default message buffer.
98       * @return a new buffer
99       */
100     public static MessageBuilder buffer()
101     {
102         return JANSI ? new AnsiMessageBuilder() : new PlainMessageBuilder();
103     }
104 
105     /**
106      * Create a message buffer with defined String builder.
107      * @return a new buffer
108      */
109     public static MessageBuilder buffer( StringBuilder builder )
110     {
111         return JANSI ? new AnsiMessageBuilder( builder ) : new PlainMessageBuilder( builder );
112     }
113 
114     /**
115      * Create a message buffer with an internal buffer of defined size.
116      * @return a new buffer
117      */
118     public static MessageBuilder buffer( int size )
119     {
120         return JANSI ? new AnsiMessageBuilder( size ) : new PlainMessageBuilder( size );
121     }
122 
123     /**
124      * Remove any ANSI code from a message (colors or other escape sequences).
125      * @param msg message eventually containing ANSI codes
126      * @return the message with ANSI codes removed
127      */
128     public static String stripAnsiCodes( String msg )
129     {
130         return msg.replaceAll( "\u001B\\[[;\\d]*[ -/]*[@-~]", "" );
131     }
132 
133 }