19
19
package org .apache .maven .cli .transfer ;
20
20
21
21
import java .io .PrintStream ;
22
- import java .io .PrintWriter ;
22
+ import java .text .DecimalFormat ;
23
+ import java .text .DecimalFormatSymbols ;
24
+ import java .util .Locale ;
23
25
24
- import org .apache .maven .api .services .MessageBuilder ;
25
- import org .apache .maven .api .services .MessageBuilderFactory ;
26
+ import org .apache .maven .jline .MessageUtils ;
26
27
import org .eclipse .aether .transfer .AbstractTransferListener ;
27
28
import org .eclipse .aether .transfer .TransferCancelledException ;
28
29
import org .eclipse .aether .transfer .TransferEvent ;
33
34
*/
34
35
@ Deprecated
35
36
public abstract class AbstractMavenTransferListener extends AbstractTransferListener {
36
- public static final String STYLE = ".transfer:-faint" ;
37
37
38
- protected final MessageBuilderFactory messageBuilderFactory ;
39
- protected final PrintWriter out ;
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" ;
40
41
41
- protected AbstractMavenTransferListener (MessageBuilderFactory messageBuilderFactory , PrintStream out ) {
42
- this (messageBuilderFactory , new PrintWriter (out ));
42
+ // CHECKSTYLE_OFF: LineLength
43
+ /**
44
+ * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
45
+ * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
46
+ * and <code>###0</code> for numbers between 10 and 1000+ by default.
47
+ *
48
+ * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
49
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
50
+ * @see <a
51
+ * href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
52
+ */
53
+ // CHECKSTYLE_ON: LineLength
54
+ // TODO Move me to Maven Shared Utils
55
+ static class FileSizeFormat {
56
+ enum ScaleUnit {
57
+ BYTE {
58
+ @ Override
59
+ public long bytes () {
60
+ return 1L ;
61
+ }
62
+
63
+ @ Override
64
+ public String symbol () {
65
+ return "B" ;
66
+ }
67
+ },
68
+ KILOBYTE {
69
+ @ Override
70
+ public long bytes () {
71
+ return 1000L ;
72
+ }
73
+
74
+ @ Override
75
+ public String symbol () {
76
+ return "kB" ;
77
+ }
78
+ },
79
+ MEGABYTE {
80
+ @ Override
81
+ public long bytes () {
82
+ return KILOBYTE .bytes () * KILOBYTE .bytes ();
83
+ }
84
+
85
+ @ Override
86
+ public String symbol () {
87
+ return "MB" ;
88
+ }
89
+ },
90
+ GIGABYTE {
91
+ @ Override
92
+ public long bytes () {
93
+ return MEGABYTE .bytes () * KILOBYTE .bytes ();
94
+ }
95
+ ;
96
+
97
+ @ Override
98
+ public String symbol () {
99
+ return "GB" ;
100
+ }
101
+ };
102
+
103
+ public abstract long bytes ();
104
+
105
+ public abstract String symbol ();
106
+
107
+ public static ScaleUnit getScaleUnit (long size ) {
108
+ if (size < 0L ) {
109
+ throw new IllegalArgumentException ("file size cannot be negative: " + size );
110
+ }
111
+
112
+ if (size >= GIGABYTE .bytes ()) {
113
+ return GIGABYTE ;
114
+ } else if (size >= MEGABYTE .bytes ()) {
115
+ return MEGABYTE ;
116
+ } else if (size >= KILOBYTE .bytes ()) {
117
+ return KILOBYTE ;
118
+ } else {
119
+ return BYTE ;
120
+ }
121
+ }
122
+ }
123
+
124
+ private DecimalFormat smallFormat ;
125
+ private DecimalFormat largeFormat ;
126
+
127
+ FileSizeFormat (Locale locale ) {
128
+ smallFormat = new DecimalFormat ("#0.0" , new DecimalFormatSymbols (locale ));
129
+ largeFormat = new DecimalFormat ("###0" , new DecimalFormatSymbols (locale ));
130
+ }
131
+
132
+ public String format (long size ) {
133
+ return format (size , null );
134
+ }
135
+
136
+ public String format (long size , ScaleUnit unit ) {
137
+ return format (size , unit , false );
138
+ }
139
+
140
+ public String format (long size , ScaleUnit unit , boolean omitSymbol ) {
141
+ if (size < 0L ) {
142
+ throw new IllegalArgumentException ("file size cannot be negative: " + size );
143
+ }
144
+
145
+ if (unit == null ) {
146
+ unit = ScaleUnit .getScaleUnit (size );
147
+ }
148
+
149
+ double scaledSize = (double ) size / unit .bytes ();
150
+ String scaledSymbol = " " + unit .symbol ();
151
+
152
+ if (omitSymbol ) {
153
+ scaledSymbol = "" ;
154
+ }
155
+
156
+ if (unit == ScaleUnit .BYTE ) {
157
+ return largeFormat .format (size ) + scaledSymbol ;
158
+ }
159
+
160
+ if (scaledSize < 0.05 || scaledSize >= 10.0 ) {
161
+ return largeFormat .format (scaledSize ) + scaledSymbol ;
162
+ } else {
163
+ return smallFormat .format (scaledSize ) + scaledSymbol ;
164
+ }
165
+ }
166
+
167
+ public String formatProgress (long progressedSize , long size ) {
168
+ if (progressedSize < 0L ) {
169
+ throw new IllegalArgumentException ("progressed file size cannot be negative: " + progressedSize );
170
+ }
171
+ if (size >= 0L && progressedSize > size ) {
172
+ throw new IllegalArgumentException (
173
+ "progressed file size cannot be greater than size: " + progressedSize + " > " + size );
174
+ }
175
+
176
+ if (size >= 0L && progressedSize != size ) {
177
+ ScaleUnit unit = ScaleUnit .getScaleUnit (size );
178
+ String formattedProgressedSize = format (progressedSize , unit , true );
179
+ String formattedSize = format (size , unit );
180
+
181
+ return formattedProgressedSize + "/" + formattedSize ;
182
+ } else {
183
+ return format (progressedSize );
184
+ }
185
+ }
43
186
}
44
187
45
- protected AbstractMavenTransferListener (MessageBuilderFactory messageBuilderFactory , PrintWriter out ) {
46
- this .messageBuilderFactory = messageBuilderFactory ;
188
+ protected PrintStream out ;
189
+
190
+ protected AbstractMavenTransferListener (PrintStream out ) {
47
191
this .out = out ;
48
192
}
49
193
50
194
@ Override
51
195
public void transferInitiated (TransferEvent event ) {
196
+ String darkOn = MessageUtils .isColorEnabled () ? ANSI_DARK_SET : "" ;
197
+ String darkOff = MessageUtils .isColorEnabled () ? ANSI_DARK_RESET : "" ;
198
+
52
199
String action = event .getRequestType () == TransferEvent .RequestType .PUT ? "Uploading" : "Downloading" ;
53
200
String direction = event .getRequestType () == TransferEvent .RequestType .PUT ? "to" : "from" ;
54
201
55
202
TransferResource resource = event .getResource ();
56
- MessageBuilder message = messageBuilderFactory . builder ();
57
- message .style ( STYLE ).append (action ).append (' ' ).append (direction ).append (' ' );
58
- message .resetStyle ( ).append (resource .getRepositoryId ());
59
- message .style ( STYLE ).append (": " ).append (resource .getRepositoryUrl ());
60
- message .resetStyle ( ).append (resource .getResourceName ());
203
+ StringBuilder message = new StringBuilder ();
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 ());
61
208
62
209
out .println (message .toString ());
63
210
}
@@ -72,29 +219,30 @@ public void transferCorrupted(TransferEvent event) throws TransferCancelledExcep
72
219
73
220
@ Override
74
221
public void transferSucceeded (TransferEvent event ) {
222
+ String darkOn = MessageUtils .isColorEnabled () ? ANSI_DARK_SET : "" ;
223
+ String darkOff = MessageUtils .isColorEnabled () ? ANSI_DARK_RESET : "" ;
224
+
75
225
String action = (event .getRequestType () == TransferEvent .RequestType .PUT ? "Uploaded" : "Downloaded" );
76
226
String direction = event .getRequestType () == TransferEvent .RequestType .PUT ? "to" : "from" ;
77
227
78
228
TransferResource resource = event .getResource ();
79
229
long contentLength = event .getTransferredBytes ();
80
- FileSizeFormat format = new FileSizeFormat ();
230
+ FileSizeFormat format = new FileSizeFormat (Locale . ENGLISH );
81
231
82
- MessageBuilder message = messageBuilderFactory . builder ();
83
- message .append (action ).style ( STYLE ).append (' ' ).append (direction ).append (' ' );
84
- message .resetStyle ( ).append (resource .getRepositoryId ());
85
- message .style ( STYLE ).append (": " ).append (resource .getRepositoryUrl ());
86
- message .resetStyle ( ).append (resource .getResourceName ());
87
- message .style ( STYLE ).append (" (" ).append (format .format (contentLength ));
232
+ StringBuilder message = new StringBuilder ();
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 ));
88
238
89
239
long duration = System .currentTimeMillis () - resource .getTransferStartTime ();
90
240
if (duration > 0L ) {
91
241
double bytesPerSecond = contentLength / (duration / 1000.0 );
92
- message .append (" at " );
93
- format .format (message , (long ) bytesPerSecond );
94
- message .append ("/s" );
242
+ message .append (" at " ).append (format .format ((long ) bytesPerSecond )).append ("/s" );
95
243
}
96
244
97
- message .append (')' ).resetStyle ( );
245
+ message .append (')' ).append ( darkOff );
98
246
out .println (message .toString ());
99
247
}
100
248
}
0 commit comments