This is an automated email from the ASF dual-hosted git repository.

martinkanters pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new a202308  [MNG-6915] Adapt the logging width to the terminal width, 
including sensible limits.
a202308 is described below

commit a20230829c1624cfea89caef87d7b213f51971d6
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Wed May 19 14:20:09 2021 +0200

    [MNG-6915] Adapt the logging width to the terminal width, including 
sensible limits.
    
    Slightly modified version of 
https://github.com/apache/maven/pull/472/commits/e47d647eee04888713a1875ddadf2ae1f28bc94e
---
 .../maven/cli/event/ExecutionEventLogger.java      | 47 ++++++++++++++------
 .../maven/cli/event/ExecutionEventLoggerTest.java  | 50 +++++++++++++++++++---
 2 files changed, 77 insertions(+), 20 deletions(-)

diff --git 
a/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java
 
b/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java
index 24652d4..67369af 100644
--- 
a/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java
+++ 
b/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java
@@ -39,6 +39,7 @@ import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
 import org.codehaus.plexus.util.StringUtils;
 import org.slf4j.ILoggerFactory;
 import org.slf4j.Logger;
@@ -53,22 +54,42 @@ public class ExecutionEventLogger extends 
AbstractExecutionListener
 {
     private final Logger logger;
 
-    private static final int LINE_LENGTH = 72;
+    private static final int MAX_LOG_PREFIX_SIZE = 8; // "[ERROR] "
+    private static final int PROJECT_STATUS_SUFFIX_SIZE = 20; // "SUCCESS [  
0.000 s]"
+    private static final int MIN_TERMINAL_WIDTH = 60;
+    private static final int DEFAULT_TERMINAL_WIDTH = 80;
+    private static final int MAX_TERMINAL_WIDTH = 130;
     private static final int MAX_PADDED_BUILD_TIME_DURATION_LENGTH = 9;
-    private static final int MAX_PROJECT_NAME_LENGTH = 52;
 
+    private final int terminalWidth;
+    private final int lineLength;
+    private final int maxProjectNameLength;
     private int totalProjects;
     private volatile int currentVisitedProjectCount;
 
     public ExecutionEventLogger()
     {
-        logger = LoggerFactory.getLogger( ExecutionEventLogger.class );
+        this( LoggerFactory.getLogger( ExecutionEventLogger.class ) );
+    }
+
+    public ExecutionEventLogger( int terminalWidth )
+    {
+        this( LoggerFactory.getLogger( ExecutionEventLogger.class ), 
terminalWidth );
     }
 
-    // TODO should we deprecate?
     public ExecutionEventLogger( Logger logger )
     {
+        this( logger, MessageUtils.getTerminalWidth() );
+    }
+
+    public ExecutionEventLogger( Logger logger, int terminalWidth )
+    {
         this.logger = Objects.requireNonNull( logger, "logger cannot be null" 
);
+        this.terminalWidth = Math.min( MAX_TERMINAL_WIDTH,
+                                       Math.max( terminalWidth < 0 ? 
DEFAULT_TERMINAL_WIDTH : terminalWidth,
+                                                 MIN_TERMINAL_WIDTH ) );
+        this.lineLength = this.terminalWidth - MAX_LOG_PREFIX_SIZE;
+        this.maxProjectNameLength = this.lineLength - 
PROJECT_STATUS_SUFFIX_SIZE;
     }
 
     private static String chars( char c, int count )
@@ -85,7 +106,7 @@ public class ExecutionEventLogger extends 
AbstractExecutionListener
 
     private void infoLine( char c )
     {
-        infoMain( chars( c, LINE_LENGTH ) );
+        infoMain( chars( c, lineLength ) );
     }
 
     private void infoMain( String msg )
@@ -116,7 +137,7 @@ public class ExecutionEventLogger extends 
AbstractExecutionListener
             final List<MavenProject> projects = 
event.getSession().getProjects();
             for ( MavenProject project : projects )
             {
-                int len = LINE_LENGTH - project.getName().length() - 
project.getPackaging().length() - 2;
+                int len = lineLength - project.getName().length() - 
project.getPackaging().length() - 2;
                 logger.info( "{}{}[{}]",
                         project.getName(), chars( ' ', ( len > 0 ) ? len : 1 
), project.getPackaging() );
             }
@@ -211,9 +232,9 @@ public class ExecutionEventLogger extends 
AbstractExecutionListener
                 buffer.append( ' ' );
             }
 
-            if ( buffer.length() <= MAX_PROJECT_NAME_LENGTH )
+            if ( buffer.length() <= maxProjectNameLength )
             {
-                while ( buffer.length() < MAX_PROJECT_NAME_LENGTH )
+                while ( buffer.length() < maxProjectNameLength )
                 {
                     buffer.append( '.' );
                 }
@@ -320,10 +341,10 @@ public class ExecutionEventLogger extends 
AbstractExecutionListener
 
             final int headerLen = preHeader.length() + projectKey.length() + 
postHeader.length();
 
-            String prefix = chars( '-', Math.max( 0, ( LINE_LENGTH - headerLen 
) / 2 ) ) + preHeader;
+            String prefix = chars( '-', Math.max( 0, ( lineLength - headerLen 
) / 2 ) ) + preHeader;
 
             String suffix = postHeader + chars( '-',
-                    Math.max( 0, LINE_LENGTH - headerLen - prefix.length() + 
preHeader.length() ) );
+                    Math.max( 0, lineLength - headerLen - prefix.length() + 
preHeader.length() ) );
 
             logger.info( buffer().strong( prefix ).project( projectKey 
).strong( suffix ).toString() );
 
@@ -344,14 +365,14 @@ public class ExecutionEventLogger extends 
AbstractExecutionListener
                 }
                 String progress = " [" + number + '/' + totalProjects + ']';
 
-                int pad = LINE_LENGTH - building.length() - progress.length();
+                int pad = lineLength - building.length() - progress.length();
 
                 infoMain( building + ( ( pad > 0 ) ? chars( ' ', pad ) : "" ) 
+ progress );
             }
 
             // ----------[ packaging ]----------
-            prefix = chars( '-', Math.max( 0, ( LINE_LENGTH - 
project.getPackaging().length() - 4 ) / 2 ) );
-            suffix = chars( '-', Math.max( 0, LINE_LENGTH - 
project.getPackaging().length() - 4 - prefix.length() ) );
+            prefix = chars( '-', Math.max( 0, ( lineLength - 
project.getPackaging().length() - 4 ) / 2 ) );
+            suffix = chars( '-', Math.max( 0, lineLength - 
project.getPackaging().length() - 4 - prefix.length() ) );
             infoMain( prefix + "[ " + project.getPackaging() + " ]" + suffix );
         }
     }
diff --git 
a/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java
 
b/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java
index d374b24..7d91fb6 100644
--- 
a/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java
+++ 
b/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java
@@ -30,12 +30,11 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.mockito.InOrder;
+import org.mockito.Mockito;
 import org.slf4j.Logger;
 
-public class ExecutionEventLoggerTest
+class ExecutionEventLoggerTest
 {
-    private ExecutionEventLogger executionEventLogger;
-
     @BeforeAll
     public static void setUp()
     {
@@ -49,12 +48,12 @@ public class ExecutionEventLoggerTest
     }
 
     @Test
-    public void testProjectStarted()
+    void testProjectStarted()
     {
         // prepare
         Logger logger = mock( Logger.class );
         when( logger.isInfoEnabled() ).thenReturn( true );
-        executionEventLogger = new ExecutionEventLogger( logger );
+        ExecutionEventLogger executionEventLogger = new ExecutionEventLogger( 
logger );
 
         ExecutionEvent event = mock( ExecutionEvent.class );
         MavenProject project = mock( MavenProject.class );
@@ -77,12 +76,12 @@ public class ExecutionEventLoggerTest
     }
 
     @Test
-    public void testProjectStartedOverflow()
+    void testProjectStartedOverflow()
     {
         // prepare
         Logger logger = mock( Logger.class );
         when( logger.isInfoEnabled() ).thenReturn( true );
-        executionEventLogger = new ExecutionEventLogger( logger );
+        ExecutionEventLogger executionEventLogger = new ExecutionEventLogger( 
logger );
 
         ExecutionEvent event = mock( ExecutionEvent.class );
         MavenProject project = mock( MavenProject.class );
@@ -103,4 +102,41 @@ public class ExecutionEventLoggerTest
         inOrder.verify( logger ).info( "Building Apache Maven Project Info 
Reports Plugin 3.0.0-SNAPSHOT" );
         inOrder.verify( logger ).info( "----------------------------[ 
maven-plugin ]----------------------------" );
     }
+
+    @Test
+    void testTerminalWidth()
+    {
+        // prepare
+        Logger logger = mock( Logger.class );
+        when( logger.isInfoEnabled() ).thenReturn( true );
+
+        ExecutionEvent event = mock( ExecutionEvent.class );
+        MavenProject project = mock( MavenProject.class );
+        when( project.getGroupId() ).thenReturn( 
"org.apache.maven.plugins.overflow" );
+        when( project.getArtifactId() ).thenReturn( 
"maven-project-info-reports-plugin" );
+        when( project.getPackaging() ).thenReturn( "maven-plugin" );
+        when( project.getName() ).thenReturn( "Apache Maven Project Info 
Reports Plugin" );
+        when( project.getVersion() ).thenReturn( "3.0.0-SNAPSHOT" );
+        when( event.getProject() ).thenReturn( project );
+
+        // default width
+        new ExecutionEventLogger( logger, -1 ).projectStarted( event );
+        Mockito.verify( logger ).info( "----------------------------[ 
maven-plugin ]----------------------------" );
+
+        // terminal width: 30
+        new ExecutionEventLogger( logger, 30 ).projectStarted( event );
+        Mockito.verify( logger ).info( "------------------[ maven-plugin 
]------------------" );
+
+        // terminal width: 70
+        new ExecutionEventLogger( logger, 70 ).projectStarted( event );
+        Mockito.verify( logger ).info( "-----------------------[ maven-plugin 
]-----------------------" );
+
+        // terminal width: 110
+        new ExecutionEventLogger( logger, 110 ).projectStarted( event );
+        Mockito.verify( logger ).info( 
"-------------------------------------------[ maven-plugin 
]-------------------------------------------" );
+
+        // terminal width: 200
+        new ExecutionEventLogger( logger, 200 ).projectStarted( event );
+        Mockito.verify( logger ).info( 
"-----------------------------------------------------[ maven-plugin 
]-----------------------------------------------------" );
+    }
 }

Reply via email to