Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

[MSHARED-562] Color recognition based on color related methods' name #14

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Copy link

@seblm seblm Jul 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not a good idea to be as coupled as Jansi library method names for configuration purpose.


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 ) )
Expand All @@ -72,7 +71,8 @@ enum Style
}

this.attribute = currentAttribute;
this.color = currentColor;
this.fgColor = currentFgColor;
this.bgColor = currentBgColor;
}

void apply( Ansi ansi )
Expand All @@ -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 );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reflection introduce an overhead that could be avoided.

}
if ( color == null )
catch ( Exception e )
{
return name() + "=" + attribute.toString();
e.printStackTrace();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user's configuration doesn't match with an existing method name, then it always fails here.
I think it should fail only once.

}
return name() + "=" + attribute + "," + color;
}

}