Improve progress display for GB+

This commit is contained in:
crschnick 2024-03-27 11:41:50 +00:00
parent 92faa26bbc
commit 76379b5a5d
3 changed files with 26 additions and 9 deletions

View file

@ -445,7 +445,7 @@ final class BrowserFileListComp extends SimpleComp {
if (path.getRawFileEntry().resolved().getKind() == FileKind.DIRECTORY) {
setText("");
} else {
setText(byteCount(fileSize.longValue(), true));
setText(byteCount(fileSize.longValue()));
}
}
}

View file

@ -47,8 +47,8 @@ public class BrowserStatusBarComp extends SimpleComp {
if (p == null || p.done()) {
return null;
} else {
var transferred = HumanReadableFormat.byteCount(p.getTransferred(), false);
var all = HumanReadableFormat.byteCount(p.getTotal(), true);
var transferred = HumanReadableFormat.progressByteCount(p.getTransferred());
var all = HumanReadableFormat.byteCount(p.getTotal());
var name = (p.getName() != null ? " @ " + p.getName() + " " : "");
return transferred + " / " + all + name;
}

View file

@ -16,17 +16,34 @@ public final class HumanReadableFormat {
public static final DateTimeFormatter DAY_OF_WEEK = DateTimeFormatter.ofPattern("EEE");
public static final DateTimeFormatter HOUR_MINUTE = DateTimeFormatter.ofPattern("HH:mm");
public static String byteCount(long bytes, boolean precise) {
if (-1024 < bytes && bytes < 1024) {
public static String byteCount(long bytes) {
var b = 1024;
if (-b < bytes && bytes < b) {
return bytes + " B";
}
CharacterIterator ci = new StringCharacterIterator("kMGTPE");
while (bytes <= -1024 * 1024 || bytes >= 1024 * 1024) {
bytes /= 1024;
var mb = b * b;
while (bytes <= -mb || bytes >= mb) {
bytes /= b;
ci.next();
}
var f = precise ? "%.1f" : "%.0f";
return String.format(f + " %cB", bytes / 1024.0, ci.current());
var f = "%.1f";
return String.format(f + " %cB", bytes / (double) b, ci.current());
}
public static String progressByteCount(long bytes) {
var b = 1024;
if (-b < bytes && bytes < b) {
return bytes + " B";
}
CharacterIterator ci = new StringCharacterIterator("kMGTPE");
var mb = b * b;
while (bytes <= -mb || bytes >= mb) {
bytes /= b;
ci.next();
}
var f = ci.getIndex() >= 2 ? "%.3f" : "%.0f";
return String.format(f + " %cB", bytes / (double) b, ci.current());
}
public static String date(LocalDateTime x) {