From 88b2f511573161e70e7ac7e9318ddc758869b49d Mon Sep 17 00:00:00 2001 From: yousfi saad Date: Sun, 23 May 2021 14:02:26 +0200 Subject: [PATCH 1/2] Add a placeholder for detailed :eta named :veta --- Readme.md | 1 + lib/node-progress.js | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/Readme.md b/Readme.md index 6d4271a..87fd4d9 100644 --- a/Readme.md +++ b/Readme.md @@ -51,6 +51,7 @@ These are tokens you can use in the format of your progress bar. - `:elapsed` time elapsed in seconds - `:percent` completion percentage - `:eta` estimated completion time in seconds +- `:veta` (verbose eta) estimated completion time in hours, minutes and seconds - `:rate` rate of ticks per second ### Custom Tokens diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..6767a79 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -137,6 +137,15 @@ ProgressBar.prototype.render = function (tokens, force) { var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1); var rate = this.curr / (elapsed / 1000); + var eta_s = eta / 1000; + var h = Math.floor(eta_s / 3600); + var m = Math.floor((eta_s % 3600) / 60); + var s = Math.floor((eta_s % 3600) % 60); + + var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : ""; + var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : ""; + var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : ""; + /* populate the bar template with percentages and timestamps */ var str = this.fmt .replace(':current', this.curr) @@ -144,6 +153,7 @@ ProgressBar.prototype.render = function (tokens, force) { .replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1)) .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000) .toFixed(1)) + .replace(':veta', hDisplay + mDisplay + sDisplay) .replace(':percent', percent.toFixed(0) + '%') .replace(':rate', Math.round(rate)); From 697c7ff15e2b90eefa9a9e0f8ecb73b9e788e075 Mon Sep 17 00:00:00 2001 From: yousfi saad Date: Sun, 23 May 2021 14:33:32 +0200 Subject: [PATCH 2/2] Add documentation for :veta --- examples/veta.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/veta.js diff --git a/examples/veta.js b/examples/veta.js new file mode 100644 index 0000000..b07863b --- /dev/null +++ b/examples/veta.js @@ -0,0 +1,23 @@ +var ProgressBar = require(".."); + +// Demonstrates the use of custom tokens with verbose eta + +var list = Array(10000) + .fill(0) + .map((_, i) => `image_${i + 1}.jpg`); + +var bar = new ProgressBar( + ":percent eta: :veta downloading :current/:total :file", + { + total: list.length, + } +); + +var id = setInterval(function () { + bar.tick({ + file: list[bar.curr], + }); + if (bar.complete) { + clearInterval(id); + } +}, 500);