[MSHARED-562] added colored message rendering helpers style(message) to ease style().a(message).reset() pattern
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1749138 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/shared/project/utils/AnsiUtils.java b/src/main/java/org/apache/maven/shared/project/utils/AnsiUtils.java
index d7967df..9a5959c 100644
--- a/src/main/java/org/apache/maven/shared/project/utils/AnsiUtils.java
+++ b/src/main/java/org/apache/maven/shared/project/utils/AnsiUtils.java
@@ -85,7 +85,7 @@
//
/**
* Insert color for DEBUG level display.
- * @return by default, bold cyan
+ * By default, bold cyan
*/
public AnsiUtils debug()
{
@@ -95,7 +95,7 @@
/**
* Insert color for INFO level display.
- * @return by default, bold blue
+ * By default, bold blue
*/
public AnsiUtils info()
{
@@ -105,7 +105,7 @@
/**
* Insert color for WARNING level or warning message display.
- * @return by default, bold yellow
+ * By default, bold yellow
*/
public AnsiUtils warning()
{
@@ -115,7 +115,7 @@
/**
* Insert color for ERROR level display.
- * @return by default, bold red
+ * By default, bold red
*/
public AnsiUtils error()
{
@@ -125,7 +125,7 @@
/**
* Insert color for success message display.
- * @return by default, bold green
+ * By default, bold green
*/
public AnsiUtils success()
{
@@ -134,8 +134,16 @@
}
/**
+ * Append success message: equivalent to appending success color, then message, then reset.
+ */
+ public AnsiUtils success( Object message )
+ {
+ return success().a( message ).reset();
+ }
+
+ /**
* Insert color for failure message display.
- * @return by default, bold red
+ * By default, bold red
*/
public AnsiUtils failure()
{
@@ -144,8 +152,16 @@
}
/**
+ * Append failure message: equivalent to appending failure color, then message, then reset.
+ */
+ public AnsiUtils failure( Object message )
+ {
+ return failure().a( message ).reset();
+ }
+
+ /**
* Insert color for strong message display.
- * @return by default, bold
+ * By default, bold
*/
public AnsiUtils strong()
{
@@ -154,8 +170,16 @@
}
/**
+ * Append strong message: equivalent to appending strong color, then message, then reset.
+ */
+ public AnsiUtils strong( Object message )
+ {
+ return strong().a( message ).reset();
+ }
+
+ /**
* Insert color for mojo message display.
- * @return by default, green
+ * By default, green
*/
public AnsiUtils mojo()
{
@@ -164,8 +188,16 @@
}
/**
+ * Append mojo message: equivalent to appending mojo color, then message, then reset.
+ */
+ public AnsiUtils mojo( Object message )
+ {
+ return mojo().a( message ).reset();
+ }
+
+ /**
* Insert color for project message display.
- * @return by default, cyan
+ * By default, cyan
*/
public AnsiUtils project()
{
@@ -173,6 +205,14 @@
return this;
}
+ /**
+ * Append project message: equivalent to appending project color, then message, then reset.
+ */
+ public AnsiUtils project( Object message )
+ {
+ return project().a( message ).reset();
+ }
+
//
// message building methods (modelled after Ansi methods)
//
diff --git a/src/test/java/org/apache/maven/shared/project/utils/AnsiUtilsTest.java b/src/test/java/org/apache/maven/shared/project/utils/AnsiUtilsTest.java
index 495da3b..6209dd6 100644
--- a/src/test/java/org/apache/maven/shared/project/utils/AnsiUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/project/utils/AnsiUtilsTest.java
@@ -24,31 +24,39 @@
import static org.junit.Assert.assertNotEquals;
import org.fusesource.jansi.Ansi;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
public class AnsiUtilsTest
{
+ private boolean savedAnsiEnable;
+
+ @Before
+ public void saveAnsiState()
+ {
+ boolean savedAnsiEnable = Ansi.isEnabled();
+ }
+
+ @After
+ public void restoreAnsiState()
+ {
+ Ansi.setEnabled( savedAnsiEnable );
+ }
+
@Test
public void constructors()
{
- boolean enabled = Ansi.isEnabled();
- try
- {
- // check that ANSI color disable is taken into account
- Ansi.setEnabled( false );
- assertEquals( "test", ansi().error().a( "test" ).reset().toString() );
- assertEquals( "test", ansi( 16 ).error().a( "test" ).reset().toString() );
- assertEquals( "test", ansi( new StringBuilder() ).error().a( "test" ).reset().toString() );
-
- Ansi.setEnabled( true );
- assertNotEquals( "test", ansi().error().a( "test" ).reset().toString() );
- assertNotEquals( "test", ansi( 16 ).error().a( "test" ).reset().toString() );
- assertNotEquals( "test", ansi( new StringBuilder() ).error().a( "test" ).reset().toString() );
- }
- finally
- {
- Ansi.setEnabled( enabled );
- }
+ // check that ANSI color disable is taken into account
+ Ansi.setEnabled( false );
+ assertEquals( "test", ansi().error().a( "test" ).reset().toString() );
+ assertEquals( "test", ansi( 16 ).error().a( "test" ).reset().toString() );
+ assertEquals( "test", ansi( new StringBuilder() ).error().a( "test" ).reset().toString() );
+
+ Ansi.setEnabled( true );
+ assertNotEquals( "test", ansi().error().a( "test" ).reset().toString() );
+ assertNotEquals( "test", ansi( 16 ).error().a( "test" ).reset().toString() );
+ assertNotEquals( "test", ansi( new StringBuilder() ).error().a( "test" ).reset().toString() );
}
@Test
@@ -62,4 +70,15 @@
assertEquals( "true", ansi().a( true ).toString() );
assertEquals( "c", ansi().a( 'c' ).toString() );
}
+
+ @Test
+ public void messages()
+ {
+ Ansi.setEnabled( true );
+ assertEquals( ansi().success().a( "test" ).reset().toString(), ansi().success( "test" ).toString() );
+ assertEquals( ansi().failure().a( "test" ).reset().toString(), ansi().failure( "test" ).toString() );
+ assertEquals( ansi().strong().a( "test" ).reset().toString(), ansi().strong( "test" ).toString() );
+ assertEquals( ansi().mojo().a( "test" ).reset().toString(), ansi().mojo( "test" ).toString() );
+ assertEquals( ansi().project().a( "test" ).reset().toString(), ansi().project( "test" ).toString() );
+ }
}