Skip to content

Commit 0c8b7df

Browse files
committed
[MNG-7875] colorize transfer messages
1 parent bbd84c6 commit 0c8b7df

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Locale;
2525

2626
import org.apache.commons.lang3.Validate;
27+
import org.apache.maven.shared.utils.logging.MessageUtils;
2728
import org.eclipse.aether.transfer.AbstractTransferListener;
2829
import org.eclipse.aether.transfer.TransferCancelledException;
2930
import org.eclipse.aether.transfer.TransferEvent;
@@ -34,6 +35,10 @@
3435
*/
3536
public abstract class AbstractMavenTransferListener extends AbstractTransferListener {
3637

38+
private static final String ESC = "\u001B";
39+
private static final String ANSI_DARK_SET = ESC + "[90m";
40+
private static final String ANSI_DARK_RESET = ESC + "[0m";
41+
3742
// CHECKSTYLE_OFF: LineLength
3843
/**
3944
* Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
@@ -184,14 +189,18 @@ protected AbstractMavenTransferListener(PrintStream out) {
184189

185190
@Override
186191
public void transferInitiated(TransferEvent event) {
192+
String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
193+
String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
194+
187195
String action = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
188196
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
189197

190198
TransferResource resource = event.getResource();
191199
StringBuilder message = new StringBuilder();
192-
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
193-
message.append(": ");
194-
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
200+
message.append(darkOn).append(action).append(' ').append(direction).append(' ');
201+
message.append(darkOff).append(resource.getRepositoryId());
202+
message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
203+
message.append(darkOff).append(resource.getResourceName());
195204

196205
out.println(message.toString());
197206
}
@@ -206,6 +215,9 @@ public void transferCorrupted(TransferEvent event) throws TransferCancelledExcep
206215

207216
@Override
208217
public void transferSucceeded(TransferEvent event) {
218+
String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
219+
String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
220+
209221
String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
210222
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
211223

@@ -214,18 +226,19 @@ public void transferSucceeded(TransferEvent event) {
214226
FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH);
215227

216228
StringBuilder message = new StringBuilder();
217-
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
218-
message.append(": ");
219-
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
220-
message.append(" (").append(format.format(contentLength));
229+
message.append(action).append(darkOn).append(' ').append(direction).append(' ');
230+
message.append(darkOff).append(resource.getRepositoryId());
231+
message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
232+
message.append(darkOff).append(resource.getResourceName());
233+
message.append(darkOn).append(" (").append(format.format(contentLength));
221234

222235
long duration = System.currentTimeMillis() - resource.getTransferStartTime();
223236
if (duration > 0L) {
224237
double bytesPerSecond = contentLength / (duration / 1000.0);
225238
message.append(" at ").append(format.format((long) bytesPerSecond)).append("/s");
226239
}
227240

228-
message.append(')');
241+
message.append(')').append(darkOff);
229242
out.println(message.toString());
230243
}
231244
}

0 commit comments

Comments
 (0)