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.5.0). 27 * For Maven version before 3.5.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 * @since 3.1.0 32 */ 33 public class MessageUtils 34 { 35 private static final boolean JANSI; 36 37 static 38 { 39 boolean jansi = true; 40 try 41 { 42 // JAnsi is provided by Maven core since 3.5.0 43 Class.forName( "org.fusesource.jansi.Ansi" ); 44 } 45 catch ( ClassNotFoundException cnfe ) 46 { 47 jansi = false; 48 } 49 JANSI = jansi; 50 } 51 52 /** 53 * Install color support. 54 * This method is called by Maven core, and calling it is not necessary in plugins. 55 */ 56 public static void systemInstall() 57 { 58 if ( JANSI ) 59 { 60 AnsiConsole.systemInstall(); 61 } 62 } 63 64 /** 65 * Undo a previous {@link #systemInstall()}. If {@link #systemInstall()} was called 66 * multiple times, {@link #systemUninstall()} must be called call the same number of times before 67 * it is actually uninstalled. 68 */ 69 public static void systemUninstall() 70 { 71 if ( JANSI ) 72 { 73 AnsiConsole.systemUninstall(); 74 } 75 } 76 77 /** 78 * Enables message color (if JAnsi is available). 79 * @param flag 80 */ 81 public static void setColorEnabled( boolean flag ) 82 { 83 if ( JANSI ) 84 { 85 Ansi.setEnabled( flag ); 86 } 87 } 88 89 /** 90 * Is message color enabled: requires JAnsi available (through Maven) and the color has not been disabled. 91 */ 92 public static boolean isColorEnabled() 93 { 94 return JANSI ? Ansi.isEnabled() : false; 95 } 96 97 /** 98 * Create a default message buffer. 99 * @return a new buffer 100 */ 101 public static MessageBuilder buffer() 102 { 103 return JANSI ? new AnsiMessageBuilder() : new PlainMessageBuilder(); 104 } 105 106 /** 107 * Create a message buffer with defined String builder. 108 * @return a new buffer 109 */ 110 public static MessageBuilder buffer( StringBuilder builder ) 111 { 112 return JANSI ? new AnsiMessageBuilder( builder ) : new PlainMessageBuilder( builder ); 113 } 114 115 /** 116 * Create a message buffer with an internal buffer of defined size. 117 * @return a new buffer 118 */ 119 public static MessageBuilder buffer( int size ) 120 { 121 return JANSI ? new AnsiMessageBuilder( size ) : new PlainMessageBuilder( size ); 122 } 123 124 /** 125 * Create a logger level renderer. 126 * @return a logger level renderer 127 * @since 3.2.0 128 */ 129 @SuppressWarnings( "checkstyle:magicnumber" ) 130 public static LoggerLevelRenderer level() 131 { 132 return JANSI ? new AnsiMessageBuilder( 20 ) : new PlainMessageBuilder( 7 ); 133 } 134 135 /** 136 * Remove any ANSI code from a message (colors or other escape sequences). 137 * @param msg message eventually containing ANSI codes 138 * @return the message with ANSI codes removed 139 */ 140 public static String stripAnsiCodes( String msg ) 141 { 142 return msg.replaceAll( "\u001B\\[[;\\d]*[ -/]*[@-~]", "" ); 143 } 144 145 }