From 25488f923131155de58a88fdf712871a500bdb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Sun, 10 Sep 2023 08:45:31 +0200 Subject: [PATCH] [MNG-7875] colorize transfer messages --- .../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 f1e84d8c8e..803c9f9b60 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 @@ -23,6 +23,7 @@ import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; +import org.apache.maven.cli.jansi.MessageUtils; import org.eclipse.aether.transfer.AbstractTransferListener; import org.eclipse.aether.transfer.TransferCancelledException; import org.eclipse.aether.transfer.TransferEvent; @@ -33,6 +34,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 SI prefix @@ -188,14 +193,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()); } @@ -210,6 +219,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"; @@ -218,10 +230,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) { @@ -229,7 +242,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()); } }