gnodet commented on code in PR #2031: URL: https://github.com/apache/maven/pull/2031#discussion_r1907552641
########## compat/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java: ########## @@ -33,31 +34,177 @@ */ @Deprecated public abstract class AbstractMavenTransferListener extends AbstractTransferListener { - public static final String STYLE = ".transfer:-faint"; - protected final MessageBuilderFactory messageBuilderFactory; - protected final PrintWriter out; + private static final String ESC = "\u001B"; + private static final String ANSI_DARK_SET = ESC + "[90m"; + private static final String ANSI_DARK_RESET = ESC + "[0m"; - protected AbstractMavenTransferListener(MessageBuilderFactory messageBuilderFactory, PrintStream out) { - this(messageBuilderFactory, new PrintWriter(out)); + // CHECKSTYLE_OFF: LineLength + /** + * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix + * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10 + * and <code>###0</code> for numbers between 10 and 1000+ by default. + * + * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a> + * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a> + * @see <a + * href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a> + */ + // CHECKSTYLE_ON: LineLength + // TODO Move me to Maven Shared Utils + static class FileSizeFormat { + enum ScaleUnit { + BYTE { + @Override + public long bytes() { + return 1L; + } + + @Override + public String symbol() { + return "B"; + } + }, + KILOBYTE { + @Override + public long bytes() { + return 1000L; + } + + @Override + public String symbol() { + return "kB"; + } + }, + MEGABYTE { + @Override + public long bytes() { + return KILOBYTE.bytes() * KILOBYTE.bytes(); + } + + @Override + public String symbol() { + return "MB"; + } + }, + GIGABYTE { + @Override + public long bytes() { + return MEGABYTE.bytes() * KILOBYTE.bytes(); + } + ; + + @Override + public String symbol() { + return "GB"; + } + }; + + public abstract long bytes(); + + public abstract String symbol(); + + public static ScaleUnit getScaleUnit(long size) { + if (size < 0L) { + throw new IllegalArgumentException("file size cannot be negative: " + size); + } + + if (size >= GIGABYTE.bytes()) { + return GIGABYTE; + } else if (size >= MEGABYTE.bytes()) { + return MEGABYTE; + } else if (size >= KILOBYTE.bytes()) { + return KILOBYTE; + } else { + return BYTE; + } + } + } + + private DecimalFormat smallFormat; + private DecimalFormat largeFormat; + + FileSizeFormat(Locale locale) { + smallFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(locale)); + largeFormat = new DecimalFormat("###0", new DecimalFormatSymbols(locale)); + } + + public String format(long size) { + return format(size, null); + } + + public String format(long size, ScaleUnit unit) { + return format(size, unit, false); + } + + public String format(long size, ScaleUnit unit, boolean omitSymbol) { + if (size < 0L) { + throw new IllegalArgumentException("file size cannot be negative: " + size); + } + + if (unit == null) { + unit = ScaleUnit.getScaleUnit(size); + } + + double scaledSize = (double) size / unit.bytes(); + String scaledSymbol = " " + unit.symbol(); + + if (omitSymbol) { + scaledSymbol = ""; + } + + if (unit == ScaleUnit.BYTE) { + return largeFormat.format(size) + scaledSymbol; + } + + if (scaledSize < 0.05 || scaledSize >= 10.0) { + return largeFormat.format(scaledSize) + scaledSymbol; + } else { + return smallFormat.format(scaledSize) + scaledSymbol; + } + } + + public String formatProgress(long progressedSize, long size) { + if (progressedSize < 0L) { + throw new IllegalArgumentException("progressed file size cannot be negative: " + progressedSize); + } + if (size >= 0L && progressedSize > size) { + throw new IllegalArgumentException( + "progressed file size cannot be greater than size: " + progressedSize + " > " + size); + } + + if (size >= 0L && progressedSize != size) { + ScaleUnit unit = ScaleUnit.getScaleUnit(size); + String formattedProgressedSize = format(progressedSize, unit, true); + String formattedSize = format(size, unit); + + return formattedProgressedSize + "/" + formattedSize; + } else { + return format(progressedSize); + } + } } - protected AbstractMavenTransferListener(MessageBuilderFactory messageBuilderFactory, PrintWriter out) { - this.messageBuilderFactory = messageBuilderFactory; + protected PrintStream out; Review Comment: I fully agree, however the code has already been cleaned up in the non deprecated version: https://github.com/apache/maven/blob/698614e3cc71c38f2979e5e165172aeb911bc8be/impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/AbstractMavenTransferListener.java#L36 That class is purely for compatibility, so no `protected` stuff should be changed. The maven-embedder is here for compatibility and not used when launching Maven from CLI directly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org