You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2023/09/10 06:45:43 UTC

[maven] 01/01: [MNG-7875] colorize transfer messages

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 db5cf10b8c9527977e276a73e2dfa5ef6d2ccb8f
Author: Hervé Boutemy <hb...@apache.org>
AuthorDate: Sun Sep 10 08:45:31 2023 +0200

    [MNG-7875] colorize transfer messages
---
 .../transfer/AbstractMavenTransferListener.java    | 48 ++++++++++++++++++----
 1 file changed, 40 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..e904d39fe9 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,19 @@ public abstract class AbstractMavenTransferListener extends AbstractTransferList
 
     @Override
     public void transferInitiated(TransferEvent event) {
+        String lowOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
+        String lowOff = 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(lowOn).append(action).append(' ').append(direction).append(' ');
+        message.append(lowOff).append(resource.getRepositoryId());
+        message.append(lowOn).append(": ");
+        message.append(resource.getRepositoryUrl()).append(lowOff);
+        appendResource(message, resource.getResourceName());
 
         out.println(message.toString());
     }
@@ -206,6 +216,9 @@ public abstract class AbstractMavenTransferListener extends AbstractTransferList
 
     @Override
     public void transferSucceeded(TransferEvent event) {
+        String lowOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
+        String lowOff = 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 +227,12 @@ 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(lowOn).append(' ').append(direction).append(' ');
+        message.append(lowOff).append(resource.getRepositoryId());
+        message.append(lowOn).append(": ");
+        message.append(resource.getRepositoryUrl()).append(lowOff);
+        appendResource(message, resource.getResourceName());
+        message.append(lowOn).append(" (").append(format.format(contentLength));
 
         long duration = System.currentTimeMillis() - resource.getTransferStartTime();
         if (duration > 0L) {
@@ -225,7 +240,24 @@ public abstract class AbstractMavenTransferListener extends AbstractTransferList
             message.append(" at ").append(format.format((long) bytesPerSecond)).append("/s");
         }
 
-        message.append(')');
+        message.append(')').append(lowOff);
         out.println(message.toString());
     }
+
+    private void appendResource(StringBuilder message, String resource) {
+        if (!MessageUtils.isColorEnabled()) {
+            message.append(resource);
+            return;
+        }
+        int filename = resource.lastIndexOf('/');
+        int version = resource.substring(0, filename).lastIndexOf('/');
+        int artifactId = resource.substring(0, version).lastIndexOf('/');
+
+        message.append(resource.substring(0, artifactId)); // groupId as directory
+        // artifactId/version
+        message.append(ANSI_DARK_SET)
+                .append(resource.substring(artifactId, filename + 1))
+                .append(ANSI_DARK_RESET);
+        message.append(resource.substring(filename + 1)); // filename
+    }
 }