[MNG-7875] colorize transfer messages

This commit is contained in:
Hervé Boutemy 2023-09-10 08:45:31 +02:00 committed by Hervé Boutemy
parent bbd84c6c87
commit 0c8b7dfc60
1 changed files with 21 additions and 8 deletions

View File

@ -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());
}
}