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 24652d4c18..67369afb5f 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.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 @@ private static String chars( char c, int count ) private void infoLine( char c ) { - infoMain( chars( c, LINE_LENGTH ) ); + infoMain( chars( c, lineLength ) ); } private void infoMain( String msg ) @@ -116,7 +137,7 @@ public void sessionStarted( ExecutionEvent event ) final List 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 @@ private void logReactorSummary( MavenSession session ) 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 void projectStarted( ExecutionEvent event ) 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 void projectStarted( ExecutionEvent event ) } 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 d374b249de..7d91fb697e 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.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 static void tearDown() } @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 void testProjectStarted() } @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 void testProjectStartedOverflow() 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 ]-----------------------------------------------------" ); + } }