[MNG-7875] colorize transfer messages

This commit is contained in:
Hervé Boutemy 2023-09-10 08:45:31 +02:00
parent 45075233c7
commit 25488f9231
1 changed files with 21 additions and 8 deletions

View File

@ -23,6 +23,7 @@ import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.util.Locale; import java.util.Locale;
import org.apache.maven.cli.jansi.MessageUtils;
import org.eclipse.aether.transfer.AbstractTransferListener; import org.eclipse.aether.transfer.AbstractTransferListener;
import org.eclipse.aether.transfer.TransferCancelledException; import org.eclipse.aether.transfer.TransferCancelledException;
import org.eclipse.aether.transfer.TransferEvent; import org.eclipse.aether.transfer.TransferEvent;
@ -33,6 +34,10 @@ import org.eclipse.aether.transfer.TransferResource;
*/ */
public abstract class AbstractMavenTransferListener extends AbstractTransferListener { 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 // CHECKSTYLE_OFF: LineLength
/** /**
* Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
@ -188,14 +193,18 @@ public abstract class AbstractMavenTransferListener extends AbstractTransferList
@Override @Override
public void transferInitiated(TransferEvent event) { 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 action = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from"; String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
TransferResource resource = event.getResource(); TransferResource resource = event.getResource();
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId()); message.append(darkOn).append(action).append(' ').append(direction).append(' ');
message.append(": "); message.append(darkOff).append(resource.getRepositoryId());
message.append(resource.getRepositoryUrl()).append(resource.getResourceName()); message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
message.append(darkOff).append(resource.getResourceName());
out.println(message.toString()); out.println(message.toString());
} }
@ -210,6 +219,9 @@ public abstract class AbstractMavenTransferListener extends AbstractTransferList
@Override @Override
public void transferSucceeded(TransferEvent event) { 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 action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from"; 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); FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH);
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId()); message.append(action).append(darkOn).append(' ').append(direction).append(' ');
message.append(": "); message.append(darkOff).append(resource.getRepositoryId());
message.append(resource.getRepositoryUrl()).append(resource.getResourceName()); message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
message.append(" (").append(format.format(contentLength)); message.append(darkOff).append(resource.getResourceName());
message.append(darkOn).append(" (").append(format.format(contentLength));
long duration = System.currentTimeMillis() - resource.getTransferStartTime(); long duration = System.currentTimeMillis() - resource.getTransferStartTime();
if (duration > 0L) { 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(" at ").append(format.format((long) bytesPerSecond)).append("/s");
} }
message.append(')'); message.append(')').append(darkOff);
out.println(message.toString()); out.println(message.toString());
} }
} }