This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
[MSHARED-562] Color recognition based on color related methods' name #14
Open
antoinebrl
wants to merge
1
commit into
apache:trunk
Choose a base branch
from
antoinebrl:trunk
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
return name() + "=" + attribute + "," + color; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.