From 9c17bca3005b4a373e879581910e2bd17cb159e7 Mon Sep 17 00:00:00 2001 From: Mohsen Taleb Date: Mon, 10 Jun 2019 15:12:23 +0430 Subject: [PATCH 1/2] Added a `humanFriendlyRate` option to show the download rate in KiB/s format instead of b/s. --- Readme.md | 6 ++++-- lib/node-progress.js | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 6d4271a..e28dd13 100644 --- a/Readme.md +++ b/Readme.md @@ -40,6 +40,7 @@ These are keys in the options object you can pass to the progress bar along with - `renderThrottle` minimum time between updates in milliseconds defaulting to 16 - `clear` option to clear the bar on completion defaulting to false - `callback` optional function to call when the progress bar completes +- `humanFriendlyRate` will show download rate in KB/s insteaf of B/s. ### Tokens @@ -97,11 +98,12 @@ req.on('response', function(res){ var len = parseInt(res.headers['content-length'], 10); console.log(); - var bar = new ProgressBar(' downloading [:bar] :rate/bps :percent :etas', { + var bar = new ProgressBar(' downloading [:bar] :rate :percent :etas', { complete: '=', incomplete: ' ', width: 20, - total: len + total: len, + humanFriendlyRate" true }); res.on('data', function (chunk) { diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..a7152e9 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -26,6 +26,7 @@ exports = module.exports = ProgressBar; * - `renderThrottle` minimum time between updates in milliseconds defaulting to 16 * - `callback` optional function to call when the progress bar completes * - `clear` will clear the progress bar upon termination + * - `humanFriendlyRate` will show download rate in KB/s insteaf of B/s. * * Tokens: * @@ -70,6 +71,7 @@ function ProgressBar(fmt, options) { this.callback = options.callback || function () {}; this.tokens = {}; this.lastDraw = ''; + this.humanFriendlyRate = options.humanFriendlyRate || false; } /** @@ -145,7 +147,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', (this.humanFriendlyRate ? (Math.round(rate) / 1024).toFixed(1) + 'KiB/s' : Math.round(rate) + 'B/s' )); /* compute the available space (non-zero) for the bar */ var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length); From 6a51ee80a6ed968f2b3437035f64c8d89198fdb2 Mon Sep 17 00:00:00 2001 From: Mohsen Taleb Date: Sat, 24 Aug 2019 12:01:09 +0430 Subject: [PATCH 2/2] Fixed typo. --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index e28dd13..5b9d738 100644 --- a/Readme.md +++ b/Readme.md @@ -103,7 +103,7 @@ req.on('response', function(res){ incomplete: ' ', width: 20, total: len, - humanFriendlyRate" true + humanFriendlyRate: true }); res.on('data', function (chunk) {