Author: hboutemy Date: Sat Jul 2 17:42:52 2016 New Revision: 1751085 URL: http://svn.apache.org/viewvc?rev=1751085&view=rev Log: [MSHARED-562] override default colors with java properties Submitted by: Sébastian Le Merdy Applied without the unit test which fails locally and with minor modifications this closes #13
Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java (with props) Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/AnsiMessageBuffer.java Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/AnsiMessageBuffer.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/AnsiMessageBuffer.java?rev=1751085&r1=1751084&r2=1751085&view=diff ============================================================================== --- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/AnsiMessageBuffer.java (original) +++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/AnsiMessageBuffer.java Sat Jul 2 17:42:52 2016 @@ -22,7 +22,8 @@ package org.apache.maven.shared.utils.lo import org.fusesource.jansi.Ansi; /** - * Message buffer implementation that supports ANSI colors through JAnsi. + * Message buffer implementation that supports ANSI colors through JAnsi with configurable + * styles through {@link Style}. */ class AnsiMessageBuffer implements MessageBuffer @@ -31,56 +32,56 @@ class AnsiMessageBuffer AnsiMessageBuffer() { - ansi = Ansi.ansi(); + this( Ansi.ansi() ); } AnsiMessageBuffer( StringBuilder builder ) { - ansi = Ansi.ansi( builder ); + this( Ansi.ansi( builder ) ); } AnsiMessageBuffer( int size ) { - ansi = Ansi.ansi( size ); + this( Ansi.ansi( size ) ); + } + + AnsiMessageBuffer( Ansi ansi ) + { + this.ansi = ansi; } - // consistent color management - // TODO make configurable - // settings.xml? during systemInstall(Settings)? - // or project properties (that can be injected by settings)? - // public AnsiMessageBuffer debug() { - ansi.bold().fgCyan(); + Style.DEBUG.apply( ansi ); return this; } - + public AnsiMessageBuffer info() { - ansi.bold().fgBlue(); + Style.INFO.apply( ansi ); return this; } - + public AnsiMessageBuffer warning() { - ansi.bold().fgYellow(); + Style.WARNING.apply( ansi ); return this; } - + public AnsiMessageBuffer warning( Object message ) { return warning().a( message ).reset(); } - + public AnsiMessageBuffer error() { - ansi.bold().fgRed(); + Style.ERROR.apply( ansi ); return this; } public AnsiMessageBuffer success() { - ansi.bold().fgGreen(); + Style.SUCCESS.apply( ansi ); return this; } @@ -91,7 +92,7 @@ class AnsiMessageBuffer public AnsiMessageBuffer failure() { - ansi.bold().fgRed(); + Style.FAILURE.apply( ansi ); return this; } @@ -102,7 +103,7 @@ class AnsiMessageBuffer public AnsiMessageBuffer strong() { - ansi.bold(); + Style.STRONG.apply( ansi ); return this; } @@ -113,7 +114,7 @@ class AnsiMessageBuffer public AnsiMessageBuffer mojo() { - ansi.fgGreen(); + Style.MOJO.apply( ansi ); return this; } @@ -121,11 +122,11 @@ class AnsiMessageBuffer { return mojo().a( message ).reset(); } - + public AnsiMessageBuffer project() { - ansi.fgCyan(); + Style.PROJECT.apply( ansi ); return this; } Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java?rev=1751085&view=auto ============================================================================== --- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java (added) +++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java Sat Jul 2 17:42:52 2016 @@ -0,0 +1,108 @@ +package org.apache.maven.shared.utils.logging; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.Ansi.Attribute; +import org.fusesource.jansi.Ansi.Color; + +import static org.fusesource.jansi.Ansi.Attribute.INTENSITY_BOLD; + +import java.util.Locale; + +/** + * Configurable message styles. + */ +enum Style +{ + + DEBUG( "bold,cyan" ), + INFO( "bold,blue" ), + WARNING( "bold,yellow" ), + ERROR( "bold,red" ), + SUCCESS( "bold,green" ), + FAILURE( "bold,red" ), + STRONG( "bold" ), + MOJO( "green" ), + PROJECT( "cyan" ); + + private final Attribute attribute; + + private final Color color; + + Style( String defaultValue ) + { + Attribute currentAttribute = null; + Color currentColor = null; + + String value = System.getProperty( "style." + name().toLowerCase( Locale.ENGLISH ), defaultValue ); + + for ( String token : value.split( "," ) ) + { + for ( Color color : Color.values() ) + { + if ( color.toString().equalsIgnoreCase( token ) ) + { + currentColor = color; + break; + } + } + + if ( "bold".equalsIgnoreCase( token ) ) + { + currentAttribute = INTENSITY_BOLD; + } + } + + this.attribute = currentAttribute; + this.color = currentColor; + } + + void apply( Ansi ansi ) + { + if ( attribute != null ) + { + ansi.a( attribute ); + } + if ( color != null ) + { + ansi.fg( color ); + } + } + + @Override + public String toString() + { + if ( attribute == null && color == null ) + { + return name(); + } + if ( attribute == null ) + { + return name() + "=" + color.toString(); + } + if ( color == null ) + { + return name() + "=" + attribute.toString(); + } + return name() + "=" + attribute + "," + color; + } + +} Propchange: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java ------------------------------------------------------------------------------ svn:mime-type = text/plain