From 76379b5a5d322cd3f7105e6db54149d5e5324a63 Mon Sep 17 00:00:00 2001 From: crschnick Date: Wed, 27 Mar 2024 11:41:50 +0000 Subject: [PATCH] Improve progress display for GB+ --- .../app/browser/BrowserFileListComp.java | 2 +- .../app/browser/BrowserStatusBarComp.java | 4 +-- .../xpipe/app/util/HumanReadableFormat.java | 29 +++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java b/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java index b22251a5..b37bd48d 100644 --- a/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java +++ b/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java @@ -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())); } } } diff --git a/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java b/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java index a16d3cfc..148e089e 100644 --- a/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java +++ b/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java @@ -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; } diff --git a/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java b/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java index 1d3c915a..626e2c0c 100644 --- a/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java +++ b/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java @@ -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) {