From ca41ad26d3c7b08497012c79b6fc44bf76c03542 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ALLEGRO Date: Sun, 19 Jul 2020 04:28:36 +0200 Subject: [PATCH 1/4] Update node-progress.js --- lib/node-progress.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..7f23842 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -145,7 +145,7 @@ ProgressBar.prototype.render = function (tokens, force) { .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000) .toFixed(1)) .replace(':percent', percent.toFixed(0) + '%') - .replace(':rate', Math.round(rate)); + .replace(':rate', humanFileSize(rate, true)); /* compute the available space (non-zero) for the bar */ var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length); @@ -234,3 +234,25 @@ ProgressBar.prototype.terminate = function () { this.stream.write('\n'); } }; + +function humanFileSize(bytes, si=false, dp=1) { + const thresh = si ? 1000 : 1024; + + if (Math.abs(bytes) < thresh) { + return bytes + ' B'; + } + + const units = si + ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] + : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; + let u = -1; + const r = 10**dp; + + do { + bytes /= thresh; + ++u; + } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); + + + return bytes.toFixed(dp) + ' ' + units[u]; +} From f8ebc96c5bc9755736b110cdde2f583146a92fc9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ALLEGRO Date: Mon, 20 Jul 2020 10:25:05 +0200 Subject: [PATCH 2/4] human readable download rate --- lib/node-progress.js | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/lib/node-progress.js b/lib/node-progress.js index 7f23842..39172c9 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -145,7 +145,7 @@ ProgressBar.prototype.render = function (tokens, force) { .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000) .toFixed(1)) .replace(':percent', percent.toFixed(0) + '%') - .replace(':rate', humanFileSize(rate, true)); + .replace(':rate', humanFileSize(rate)); /* compute the available space (non-zero) for the bar */ var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length); @@ -235,24 +235,7 @@ ProgressBar.prototype.terminate = function () { } }; -function humanFileSize(bytes, si=false, dp=1) { - const thresh = si ? 1000 : 1024; - - if (Math.abs(bytes) < thresh) { - return bytes + ' B'; - } - - const units = si - ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] - : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; - let u = -1; - const r = 10**dp; - - do { - bytes /= thresh; - ++u; - } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); - - - return bytes.toFixed(dp) + ' ' + units[u]; -} +function humanFileSize(size) { + var i = size == 0 ? 0 : Math.floor( Math.log(size) / Math.log(1024) ); + return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; +}; From b624fdd2b9b36b2094272d02a5aa879379fd412f Mon Sep 17 00:00:00 2001 From: Jean-Philippe ALLEGRO Date: Tue, 21 Jul 2020 20:38:38 +0200 Subject: [PATCH 3/4] human readable ETA and RATE --- lib/node-progress.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/node-progress.js b/lib/node-progress.js index 39172c9..cccb913 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -142,10 +142,9 @@ ProgressBar.prototype.render = function (tokens, force) { .replace(':current', this.curr) .replace(':total', this.total) .replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1)) - .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000) - .toFixed(1)) + .replace(':eta', this.humanETA(eta)) .replace(':percent', percent.toFixed(0) + '%') - .replace(':rate', humanFileSize(rate)); + .replace(':rate', this.humanFileSize(rate)); /* compute the available space (non-zero) for the bar */ var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length); @@ -235,7 +234,29 @@ ProgressBar.prototype.terminate = function () { } }; -function humanFileSize(size) { - var i = size == 0 ? 0 : Math.floor( Math.log(size) / Math.log(1024) ); - return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; -}; +ProgressBar.prototype.humanFileSize = function(size) { + var i = size == 0 ? 0 : Math.floor( Math.log(size) / Math.log(1024) ); + return ( size / Math.pow(1024, i) ).toFixed(2) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i] + '/s'; +} + +ProgressBar.prototype.humanETA = function(ms) { + if(!isFinite(ms)) return 0+'ms' + if (ms < 0) ms = -ms; + if(ms<1000 && ms>0) return ms+'ms' + const time = { + day: Math.floor(ms / 86400000), + hour: Math.floor(ms / 3600000) % 24, + minute: Math.floor(ms / 60000) % 60, + s: Math.floor(ms / 1000) % 60 + }; + return Object.entries(time) + .filter(val => val[1] !== 0) + .map(val => { + if(val[0] !== 's') { + return val[1] + ' ' + (val[1] !== 1 ? val[0] + 's' : val[0]) + } else { + return val[1] + 's' + } + }) + .join(' '); + }; From c03cded0b5439604513d6c8aaed15e5cda38f5aa Mon Sep 17 00:00:00 2001 From: Jean-Philippe ALLEGRO Date: Sun, 26 Jul 2020 16:07:02 +0200 Subject: [PATCH 4/4] ETA improvements on display - parseInt(ms) when ms < 1000 (avoid stuff like '950.195985641978315ms') - display 'done' when ms === 0 --- lib/node-progress.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node-progress.js b/lib/node-progress.js index cccb913..b4d7d83 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -242,7 +242,8 @@ ProgressBar.prototype.humanFileSize = function(size) { ProgressBar.prototype.humanETA = function(ms) { if(!isFinite(ms)) return 0+'ms' if (ms < 0) ms = -ms; - if(ms<1000 && ms>0) return ms+'ms' + if(ms===0) return 'done' + if(ms<1000) return parseInt(ms)+'ms' const time = { day: Math.floor(ms / 86400000), hour: Math.floor(ms / 3600000) % 24,