Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Don't show 100% before the last byte, or "undefined" for sub-byte speeds (#146)

Arpit Khandelwal84752c3c63ba
diff --git a/e2e/format.spec.ts b/e2e/format.spec.ts
new file mode 100644
index 0000000..e2a8b54
--- /dev/null
+++ b/e2e/format.spec.ts
@@ -0,0 +1,35 @@
+import { expect, test } from '@playwright/test'
+import {
+ formatBytes,
+ formatPercent,
+ formatSpeed,
+ speedTicks
+} from '../src/renderer/src/utils/format'
+
+const GiB = 1024 ** 3
+const MiB = 1024 ** 2
+
+test.describe('formatting @smoke', () => {
+ test('a percentage reads 100 only once every byte is in', () => {
+ expect(formatPercent(10 * GiB - 5 * MiB, 10 * GiB)).toBe(99)
+ expect(formatPercent(999, 1000)).toBe(99)
+ expect(formatPercent(1000, 1000)).toBe(100)
+ expect(formatPercent(994, 1000)).toBe(99)
+ expect(formatPercent(500, 1000)).toBe(50)
+ expect(formatPercent(0, 0)).toBe(0)
+ })
+
+ test('less than a byte a second is still in bytes', () => {
+ expect(formatBytes(0.5)).toBe('1 B')
+ expect(formatSpeed(1 / 3, 'bytes')).toBe('0 B/s')
+ expect(formatBytes(1536)).toBe('1.5 KB')
+ })
+
+ test('a speed axis for a sub-byte speed is finite', () => {
+ const { top, ticks } = speedTicks(0.5, 'bytes')
+ expect(Number.isFinite(top)).toBe(true)
+ expect(top).toBeGreaterThanOrEqual(0.5)
+ expect(ticks.length).toBeGreaterThan(0)
+ for (const tick of ticks) expect(tick.label).toMatch(/ B\/s$/)
+ })
+})
diff --git a/src/renderer/src/screens/ErrorScreen.tsx b/src/renderer/src/screens/ErrorScreen.tsx
index dc3e8fe..ee9dba2 100644
--- a/src/renderer/src/screens/ErrorScreen.tsx
+++ b/src/renderer/src/screens/ErrorScreen.tsx
@@ -7,6 +7,7 @@ import {
describeError,
fileExtensionBadge,
formatBytes,
+ formatPercent,
isFolder,
wantedBytes
} from '../utils/format'
@@ -15,9 +16,7 @@ export function ErrorScreen({ download }: { download: DownloadState }): React.JS
const [copied, setCopied] = useState(false)
const cancelled = download.status === 'cancelled'
const knownSize = download.totalBytes > 0
- const percent = knownSize
- ? Math.min(100, Math.round((download.bytesDownloaded / wantedBytes(download)) * 100))
- : 0
+ const percent = knownSize ? formatPercent(download.bytesDownloaded, wantedBytes(download)) : 0
const heading = cancelled ? 'Download cancelled' : 'Download failed'
const description = cancelled
? 'The download was stopped before finishing.'
diff --git a/src/renderer/src/utils/format.ts b/src/renderer/src/utils/format.ts
index a830fbf..8364f99 100644
--- a/src/renderer/src/utils/format.ts
+++ b/src/renderer/src/utils/format.ts
@@ -13,7 +13,11 @@ const UNITS = ['B', 'KB', 'MB', 'GB', 'TB']
export function formatBytes(bytes: number): string {
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'
- let exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), UNITS.length - 1)
+ // Under 1 byte (a slow speed averaged over a few seconds) is still bytes, not a unit below them.
+ let exponent = Math.max(
+ 0,
+ Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), UNITS.length - 1)
+ )
let value = bytes / 1024 ** exponent
if (exponent < UNITS.length - 1 && Number(value.toFixed(exponent === 0 ? 0 : 1)) >= 1024) {
exponent += 1
@@ -137,7 +141,10 @@ export function wantedBytes(download: DownloadState | FinishedDownload): number
export function formatPercent(bytesDownloaded: number, totalBytes: number): number {
if (totalBytes <= 0) return 0
- return Math.min(100, Math.round((bytesDownloaded / totalBytes) * 100))
+ // 100% only once every byte is in: rounding 99.6% up would claim a 10 GB file was done with
+ // 40 MB still to go.
+ if (bytesDownloaded >= totalBytes) return 100
+ return Math.min(99, Math.round((bytesDownloaded / totalBytes) * 100))
}
export function fileNameFromPath(path: string): string {