Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detailed eta #207

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions examples/veta.js
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,23 @@ 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)
.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(':veta', hDisplay + mDisplay + sDisplay)
.replace(':percent', percent.toFixed(0) + '%')
.replace(':rate', Math.round(rate));

Expand Down