// Display-only formatters shared across pages. Pure functions, // no I/O, no locale fall-through — locale-specific labels (the // unit word itself, e.g. "Bytes" vs. "bytes") stay in `.arb`; // these helpers only handle the numeric formatting. /// Render a byte count with an SI suffix that stays at three /// significant digits ("23.3 kB", "1.04 MB", "847 B"). Uses 1000 /// as the base, not 1024 — matches macOS Finder / GNOME Files /// and is what most operators expect when they read "kB". /// /// Returns plain `n B` for values under 1000 so small flows /// and tiny inputs stay precise instead of becoming "0.5 kB". String humanBytes(int bytes) { if (bytes < 1000) return '$bytes B'; const units = ['kB', 'MB', 'GB', 'TB', 'PB']; var v = bytes.toDouble() / 1000.0; var unitIndex = 0; while (v >= 1000 && unitIndex < units.length - 1) { v /= 1000.0; unitIndex++; } // Pick decimals so the rendered number stays at 3 significant // figures: 9.99 / 99.9 / 999 — same approach Finder uses. final String formatted; if (v >= 100) { formatted = v.toStringAsFixed(0); } else if (v >= 10) { formatted = v.toStringAsFixed(1); } else { formatted = v.toStringAsFixed(2); } return '$formatted ${units[unitIndex]}'; }