diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..b06a484 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -69,6 +69,7 @@ function ProgressBar(fmt, options) { this.lastRender = -Infinity; this.callback = options.callback || function () {}; this.tokens = {}; + this.spaceRequiredForTokens = 0; this.lastDraw = ''; } @@ -147,8 +148,22 @@ ProgressBar.prototype.render = function (tokens, force) { .replace(':percent', percent.toFixed(0) + '%') .replace(':rate', Math.round(rate)); + /* account for the replacement of the extra tokens */ + var spaceRequiredForTokens = 0; + if (this.tokens) for (var key in this.tokens) { + if(str.indexOf(":"+key) == -1) continue; + spaceRequiredForTokens += this.tokens[key].length - (key.length+1) + }; + + /* ensure spaceRequiredForTokens is at least 0, and is consistent accross ticks */ + if(spaceRequiredForTokens > this.spaceRequiredForTokens) { + this.spaceRequiredForTokens = spaceRequiredForTokens; + } else { + spaceRequiredForTokens = this.spaceRequiredForTokens; + } + /* 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 - spaceRequiredForTokens - str.replace(':bar', '').length); if(availableSpace && process.platform === 'win32'){ availableSpace = availableSpace - 1; }