From 3575e44451a240608f37f6b2076c46e1ccf4bd48 Mon Sep 17 00:00:00 2001 From: Antoine Broyelle <antoine.broyelle@protonmail.com> Date: Mon, 4 Jul 2016 23:17:47 +0200 Subject: [PATCH] [MSHARED-562] Color recognition based on color related methods' name The string part of enum color are no longer used. Instead, we use the name of methods related to color (starting by 'bg' or 'fg'). Thus, this modification take into account the foreground and the background color. example : -Dstyle.info=fgCyan,bgRed We can also considere the modificator bright (or other ones not yet implemented) example : -Dstyle.info=fgBrightCyan --- .../maven/shared/utils/logging/Style.java | 96 ++++++++++++------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java b/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java index 6a61ed0a2..7454c2d4d 100644 --- a/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java +++ b/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/Style.java @@ -21,48 +21,47 @@ 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.lang.reflect.Method; -import java.util.Locale; +import static org.fusesource.jansi.Ansi.Attribute.INTENSITY_BOLD; /** - * Configurable message styles. + * */ enum Style { - DEBUG( "bold,cyan" ), - INFO( "bold,blue" ), - WARNING( "bold,yellow" ), - ERROR( "bold,red" ), - SUCCESS( "bold,green" ), - FAILURE( "bold,red" ), + DEBUG( "bold,fgCyan" ), + INFO( "bold,fgBlue" ), + WARNING( "bold,fgYellow" ), + ERROR( "bold,fgRed" ), + SUCCESS( "bold,fgGreen" ), + FAILURE( "bold,fgRed" ), STRONG( "bold" ), - MOJO( "green" ), - PROJECT( "cyan" ); + MOJO( "fgGreen" ), + PROJECT( "fgCyan" ); private final Attribute attribute; - private final Color color; + private final String fgColor; + private final String bgColor; - Style( String defaultValue ) + Style( String defaultConfiguration ) { Attribute currentAttribute = null; - Color currentColor = null; + String currentFgColor = null; + String currentBgColor = null; - String value = System.getProperty( "style." + name().toLowerCase( Locale.ENGLISH ), defaultValue ); - - for ( String token : value.split( "," ) ) + for ( String token : System.getProperty( "style." + name().toLowerCase(), defaultConfiguration ).split( "," ) ) { - for ( Color color : Color.values() ) + if ( token.startsWith( "fg" ) ) { - if ( color.toString().equalsIgnoreCase( token ) ) - { - currentColor = color; - break; - } + currentFgColor = token; + } + if ( token.startsWith( "bg" ) ) + { + currentBgColor = token; } if ( "bold".equalsIgnoreCase( token ) ) @@ -72,7 +71,8 @@ enum Style } this.attribute = currentAttribute; - this.color = currentColor; + this.fgColor = currentFgColor; + this.bgColor = currentBgColor; } void apply( Ansi ansi ) @@ -81,28 +81,56 @@ void apply( Ansi ansi ) { ansi.a( attribute ); } - if ( color != null ) + + if ( fgColor != null ) + { + applyColor( ansi, fgColor ); + } + if ( bgColor != null ) { - ansi.fg( color ); + applyColor( ansi, bgColor ); } } @Override public String toString() { - if ( attribute == null && color == null ) + StringBuilder s = new StringBuilder( name() ); + if ( attribute != null || fgColor != null || bgColor != null ) { - return name(); + s.append( "=" ); + String prefix = ""; + if ( attribute == null ) + { + prefix = ","; + s.append( attribute.toString() ); + } + if ( fgColor == null ) + { + s.append( prefix ); + prefix = ","; + s.append( fgColor ); + } + if ( bgColor == null ) + { + s.append( prefix ); + s.append( bgColor ); + } } - if ( attribute == null ) + return s.toString(); + } + + private void applyColor( Ansi ansi, String c ) + { + try { - return name() + "=" + color.toString(); + Method m = ansi.getClass().getMethod( c ); + m.invoke( ansi ); } - if ( color == null ) + catch ( Exception e ) { - return name() + "=" + attribute.toString(); + e.printStackTrace(); } - return name() + "=" + attribute + "," + color; } }