Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added custom tokens support

  • Loading branch information...
commit 46d162ad20ecb60fa517eac53580806d5010e595 1 parent 851b693
TJ Holowaychuk authored
Showing with 17 additions and 5 deletions.
  1. +3 −2 examples/formats.js
  2. +14 −3 lib/node-progress.js
5 examples/formats.js
View
@@ -5,11 +5,12 @@
var ProgressBar = require('../');
-var bar = new ProgressBar(':bar', { total: 10 });
+var bar = new ProgressBar(':bar :title', { total: 10 });
console.log('processing:');
var id = setInterval(function(){
- bar.tick();
+ var randomTitle = ['some', 'random', 'title'][Math.random() * 3 | 0];
+ bar.tick({ title: randomTitle });
if (bar.complete) {
console.log('\ncomplete\n');
clearInterval(id);
17 lib/node-progress.js
View
@@ -58,15 +58,20 @@ function ProgressBar(fmt, options) {
}
/**
- * "tick" the progress bar with optional `len`.
+ * "tick" the progress bar with optional `len` and
+ * optional `tokens`.
*
- * @param {Number} len
+ * @param {Number|Object} len or tokens
+ * @param {Object} tokens
* @api public
*/
-ProgressBar.prototype.tick = function(len){
+ProgressBar.prototype.tick = function(len, tokens){
len = len || 1;
+ // swap tokens
+ if ('object' == typeof len) tokens = len, len = 1;
+
// start time for eta
if (0 == this.curr) this.start = new Date;
@@ -93,5 +98,11 @@ ProgressBar.prototype.tick = function(len){
.replace(':eta', (eta / 1000).toFixed(1))
.replace(':percent', percent.toFixed(0) + '%');
+ if (tokens) {
+ for (var key in tokens) {
+ str = str.replace(':' + key, tokens[key]);
+ }
+ }
+
this.stream.write('\r' + str);
};
Please sign in to comment.
Something went wrong with that request. Please try again.