Skip to content

Commit 25488f9

Browse files
committed
[MNG-7875] colorize transfer messages
1 parent 4507523 commit 25488f9

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
@@ -23,6 +23,7 @@
2323
import java.text.DecimalFormatSymbols;
2424
import java.util.Locale;
2525

26+
import org.apache.maven.cli.jansi.MessageUtils;
2627
import org.eclipse.aether.transfer.AbstractTransferListener;
2728
import org.eclipse.aether.transfer.TransferCancelledException;
2829
import org.eclipse.aether.transfer.TransferEvent;
@@ -33,6 +34,10 @@
3334
*/
3435
public abstract class AbstractMavenTransferListener extends AbstractTransferListener {
3536

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

189194
@Override
190195
public void transferInitiated(TransferEvent event) {
196+
String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
197+
String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
198+
191199
String action = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
192200
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
193201

194202
TransferResource resource = event.getResource();
195203
StringBuilder message = new StringBuilder();
196-
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
197-
message.append(": ");
198-
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
204+
message.append(darkOn).append(action).append(' ').append(direction).append(' ');
205+
message.append(darkOff).append(resource.getRepositoryId());
206+
message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
207+
message.append(darkOff).append(resource.getResourceName());
199208

200209
out.println(message.toString());
201210
}
@@ -210,6 +219,9 @@ public void transferCorrupted(TransferEvent event) throws TransferCancelledExcep
210219

211220
@Override
212221
public void transferSucceeded(TransferEvent event) {
222+
String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
223+
String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
224+
213225
String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
214226
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
215227

@@ -218,18 +230,19 @@ public void transferSucceeded(TransferEvent event) {
218230
FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH);
219231

220232
StringBuilder message = new StringBuilder();
221-
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
222-
message.append(": ");
223-
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
224-
message.append(" (").append(format.format(contentLength));
233+
message.append(action).append(darkOn).append(' ').append(direction).append(' ');
234+
message.append(darkOff).append(resource.getRepositoryId());
235+
message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
236+
message.append(darkOff).append(resource.getResourceName());
237+
message.append(darkOn).append(" (").append(format.format(contentLength));
225238

226239
long duration = System.currentTimeMillis() - resource.getTransferStartTime();
227240
if (duration > 0L) {
228241
double bytesPerSecond = contentLength / (duration / 1000.0);
229242
message.append(" at ").append(format.format((long) bytesPerSecond)).append("/s");
230243
}
231244

232-
message.append(')');
245+
message.append(')').append(darkOff);
233246
out.println(message.toString());
234247
}
235248
}

0 commit comments

Comments
 (0)