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

hboutemy pushed a commit to branch MNG-7875_transfer_color
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 43f6cef00a283d63b5c999d91b9e2b63eff1cc03
Author: Hervé Boutemy <hbout...@apache.org>
AuthorDate: Sun Sep 10 08:45:31 2023 +0200

    [MNG-7875] colorize transfer messages
---
 .../transfer/AbstractMavenTransferListener.java    | 29 ++++++++++++++++------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git 
a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
 
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
index 7e71925b5c..e91be53209 100644
--- 
a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
+++ 
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
@@ -24,6 +24,7 @@ import java.text.DecimalFormatSymbols;
 import java.util.Locale;
 
 import org.apache.commons.lang3.Validate;
+import org.apache.maven.shared.utils.logging.MessageUtils;
 import org.eclipse.aether.transfer.AbstractTransferListener;
 import org.eclipse.aether.transfer.TransferCancelledException;
 import org.eclipse.aether.transfer.TransferEvent;
@@ -34,6 +35,10 @@ import org.eclipse.aether.transfer.TransferResource;
  */
 public abstract class AbstractMavenTransferListener extends 
AbstractTransferListener {
 
+    private static final String ESC = "\u001B";
+    private static final String ANSI_DARK_SET = ESC + "[90m";
+    private static final String ANSI_DARK_RESET = ESC + "[0m";
+
     // CHECKSTYLE_OFF: LineLength
     /**
      * Formats file size with the associated <a 
href="https://en.wikipedia.org/wiki/Metric_prefix";>SI</a> prefix
@@ -184,14 +189,18 @@ public abstract class AbstractMavenTransferListener 
extends AbstractTransferList
 
     @Override
     public void transferInitiated(TransferEvent event) {
+        String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
+        String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
+
         String action = event.getRequestType() == 
TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
         String direction = event.getRequestType() == 
TransferEvent.RequestType.PUT ? "to" : "from";
 
         TransferResource resource = event.getResource();
         StringBuilder message = new StringBuilder();
-        message.append(action).append(' ').append(direction).append(' 
').append(resource.getRepositoryId());
-        message.append(": ");
-        
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
+        message.append(darkOn).append(action).append(' 
').append(direction).append(' ');
+        message.append(darkOff).append(resource.getRepositoryId());
+        message.append(darkOn).append(": 
").append(resource.getRepositoryUrl());
+        message.append(darkOff).append(resource.getResourceName());
 
         out.println(message.toString());
     }
@@ -206,6 +215,9 @@ public abstract class AbstractMavenTransferListener extends 
AbstractTransferList
 
     @Override
     public void transferSucceeded(TransferEvent event) {
+        String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
+        String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
+
         String action = (event.getRequestType() == 
TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
         String direction = event.getRequestType() == 
TransferEvent.RequestType.PUT ? "to" : "from";
 
@@ -214,10 +226,11 @@ public abstract class AbstractMavenTransferListener 
extends AbstractTransferList
         FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH);
 
         StringBuilder message = new StringBuilder();
-        message.append(action).append(' ').append(direction).append(' 
').append(resource.getRepositoryId());
-        message.append(": ");
-        
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
-        message.append(" (").append(format.format(contentLength));
+        message.append(action).append(darkOn).append(' 
').append(direction).append(' ');
+        message.append(darkOff).append(resource.getRepositoryId());
+        message.append(darkOn).append(": 
").append(resource.getRepositoryUrl());
+        message.append(darkOff).append(resource.getResourceName());
+        message.append(darkOn).append(" 
(").append(format.format(contentLength));
 
         long duration = System.currentTimeMillis() - 
resource.getTransferStartTime();
         if (duration > 0L) {
@@ -225,7 +238,7 @@ public abstract class AbstractMavenTransferListener extends 
AbstractTransferList
             message.append(" at ").append(format.format((long) 
bytesPerSecond)).append("/s");
         }
 
-        message.append(')');
+        message.append(')').append(darkOff);
         out.println(message.toString());
     }
 }

Reply via email to