24
24
import java .util .Locale ;
25
25
26
26
import org .apache .commons .lang3 .Validate ;
27
+ import org .apache .maven .shared .utils .logging .MessageUtils ;
27
28
import org .eclipse .aether .transfer .AbstractTransferListener ;
28
29
import org .eclipse .aether .transfer .TransferCancelledException ;
29
30
import org .eclipse .aether .transfer .TransferEvent ;
34
35
*/
35
36
public abstract class AbstractMavenTransferListener extends AbstractTransferListener {
36
37
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
+
37
42
// CHECKSTYLE_OFF: LineLength
38
43
/**
39
44
* 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) {
184
189
185
190
@ Override
186
191
public void transferInitiated (TransferEvent event ) {
192
+ String darkOn = MessageUtils .isColorEnabled () ? ANSI_DARK_SET : "" ;
193
+ String darkOff = MessageUtils .isColorEnabled () ? ANSI_DARK_RESET : "" ;
194
+
187
195
String action = event .getRequestType () == TransferEvent .RequestType .PUT ? "Uploading" : "Downloading" ;
188
196
String direction = event .getRequestType () == TransferEvent .RequestType .PUT ? "to" : "from" ;
189
197
190
198
TransferResource resource = event .getResource ();
191
199
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 ());
195
204
196
205
out .println (message .toString ());
197
206
}
@@ -206,6 +215,9 @@ public void transferCorrupted(TransferEvent event) throws TransferCancelledExcep
206
215
207
216
@ Override
208
217
public void transferSucceeded (TransferEvent event ) {
218
+ String darkOn = MessageUtils .isColorEnabled () ? ANSI_DARK_SET : "" ;
219
+ String darkOff = MessageUtils .isColorEnabled () ? ANSI_DARK_RESET : "" ;
220
+
209
221
String action = (event .getRequestType () == TransferEvent .RequestType .PUT ? "Uploaded" : "Downloaded" );
210
222
String direction = event .getRequestType () == TransferEvent .RequestType .PUT ? "to" : "from" ;
211
223
@@ -214,18 +226,19 @@ public void transferSucceeded(TransferEvent event) {
214
226
FileSizeFormat format = new FileSizeFormat (Locale .ENGLISH );
215
227
216
228
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 ));
221
234
222
235
long duration = System .currentTimeMillis () - resource .getTransferStartTime ();
223
236
if (duration > 0L ) {
224
237
double bytesPerSecond = contentLength / (duration / 1000.0 );
225
238
message .append (" at " ).append (format .format ((long ) bytesPerSecond )).append ("/s" );
226
239
}
227
240
228
- message .append (')' );
241
+ message .append (')' ). append ( darkOff ) ;
229
242
out .println (message .toString ());
230
243
}
231
244
}
0 commit comments