From f401a52be1d89ac98230348834fa6c9e76c83630 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Fri, 26 Nov 2021 21:32:06 +0200 Subject: [PATCH] added auto-changing between GB, MB, KB.. in progress bar --- src/ts/lib/LauncherUI.ts | 4 ++-- src/ts/lib/Tools.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/ts/lib/LauncherUI.ts b/src/ts/lib/LauncherUI.ts index d13f6ec..3e429a4 100644 --- a/src/ts/lib/LauncherUI.ts +++ b/src/ts/lib/LauncherUI.ts @@ -183,7 +183,7 @@ export default class LauncherUI public static updateProgressBar (prefix: string, current: number, total: number, difference: number): void { - $('#downloaded').text(`${prefix}: ${ Math.round(current / total * 100) }% (${ (current / 1024 / 1024 / 1024).toFixed(2) } GB / ${ Math.round(total / 1024 / 1024 / 1024).toFixed(2) } GB)`); + $('#downloaded').text(`${prefix}: ${ Math.round(current / total * 100) }% (${Tools.prettifyBytes(current)} / ${Tools.prettifyBytes(total)})`); this.progressBar.temp += difference; @@ -208,7 +208,7 @@ export default class LauncherUI etaSeconds = '0' + etaSeconds.toString(); $('#downloader .progress').css('width', `${ Math.round(current / total * 100) }%`); - $('#speed').text(`${ (this.progressBar.temp / (Date.now() - this.progressBar.prevTime) * 1000 / 1024 / 1024).toFixed(2) } MB/s`); + $('#speed').text(`${Tools.prettifyBytes(this.progressBar.temp / (Date.now() - this.progressBar.prevTime) * 1000)}/s`); $('#eta').text(`ETA: ${etaHours}:${etaMinutes}:${etaSeconds}`); this.progressBar.prevTime = Date.now(); diff --git a/src/ts/lib/Tools.ts b/src/ts/lib/Tools.ts index c07248b..6706cf5 100644 --- a/src/ts/lib/Tools.ts +++ b/src/ts/lib/Tools.ts @@ -24,6 +24,32 @@ type Pixel = { export default class Tools { + public static prettifyBytes (bytes: number): string + { + const types = [ + { + name: 'B', + multiplier: 1 + }, + { + name: 'KB', + multiplier: 1024 + }, + { + name: 'MB', + multiplier: 1024 * 1024 + }, + { + name: 'GB', + multiplier: 1024 * 1024 * 1024 + } + ].filter(type => type.multiplier < bytes); + + return types.length == 0 ? + `${bytes} B` : + `${(bytes / types[types.length - 1].multiplier).toFixed(2)} ${types[types.length - 1].name}`; + } + public static getImagePixels (path: string): Promise { return new Promise(resolve => {