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

Fix: allow tokens to be used multiple times #198

Open
wants to merge 1 commit 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
19 changes: 19 additions & 0 deletions examples/multipletokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
const ProgressBar = require('../');

const bar = new ProgressBar("Multiple tokens contain current::current current::current total::total elapsed::elapseds etas::etas rate::rate percent::percent [:bar] current::current total::total elapsed::elapseds etas::etas rate::rate percent::percent, hello world!",
{ total: 100 });
let timerBar = setInterval( () => {
bar.tick();
if (bar.complete) {
clearInterval(timerBar);
}
}, 10);

const baz = new ProgressBar('The two bar [:bar] and [:bar], hello world!', {total: 100});
let timerBaz = setInterval( () => {
baz.tick();
if (baz.complete) {
clearInterval(timerBaz);
}
}, 10);
24 changes: 13 additions & 11 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,20 @@ ProgressBar.prototype.render = function (tokens, force) {
var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);
var rate = this.curr / (elapsed / 1000);

/* populate the bar template with percentages and timestamps */
/**
* populate the bar template with percentages and timestamps.
* replace multiple tokens, for example, the curr of tokens replace multiple ':current' .
*/
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(':percent', percent.toFixed(0) + '%')
.replace(':rate', Math.round(rate));
.replaceAll(':current', this.curr)
.replaceAll(':total', this.total)
.replaceAll(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1))
.replaceAll(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000).toFixed(1))
.replaceAll(':percent', percent.toFixed(0) + '%')
.replaceAll(':rate', Math.round(rate));

/* compute the available space (non-zero) for the bar */
var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
var availableSpace = Math.max(0, this.stream.columns - str.replaceAll(':bar', '').length);
if(availableSpace && process.platform === 'win32'){
availableSpace = availableSpace - 1;
}
Expand All @@ -165,10 +167,10 @@ ProgressBar.prototype.render = function (tokens, force) {
complete = complete.slice(0, -1) + this.chars.head;

/* fill in the actual progress bar */
str = str.replace(':bar', complete + incomplete);
str = str.replaceAll(':bar', complete + incomplete);

/* replace the extra tokens */
if (this.tokens) for (var key in this.tokens) str = str.replace(':' + key, this.tokens[key]);
if (this.tokens) for (var key in this.tokens) str = str.replaceAll(':' + key, this.tokens[key]);

if (this.lastDraw !== str) {
this.stream.cursorTo(0);
Expand Down