Skip to content

Commit

Permalink
[MNG-7875] colorize transfer messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Sep 11, 2023
1 parent bbd84c6 commit 0c8b7df
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand All @@ -34,6 +35,10 @@
*/
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
Expand Down Expand Up @@ -184,14 +189,18 @@ protected AbstractMavenTransferListener(PrintStream out) {

@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());
}
Expand All @@ -206,6 +215,9 @@ public void transferCorrupted(TransferEvent event) throws TransferCancelledExcep

@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";

Expand All @@ -214,18 +226,19 @@ public void transferSucceeded(TransferEvent event) {
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) {
double bytesPerSecond = contentLength / (duration / 1000.0);
message.append(" at ").append(format.format((long) bytesPerSecond)).append("/s");
}

message.append(')');
message.append(')').append(darkOff);
out.println(message.toString());
}
}

0 comments on commit 0c8b7df

Please sign in to comment.