Skip to content
Merged
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
77 changes: 51 additions & 26 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,67 @@
### 1.1.5 / 2014-03-25

1.0.0 / 2013-06-18
==================
* updated documentation and various other repo maintenance
* updated makefile to run examples with `make`
* removed dependency on readline module

* remove .version
* >=. Closes #19
* Merge pull request #15 from davglass/readline-osx
* On OSX revert back to terminal hack to avoid a readline bug
### 1.1.4 / 2014-03-14

0.1.0 / 2012-09-19
==================
* now supports streams, for example output progress bar to stderr, while piping
stdout
* increases performance and flicker by remembering the last drawn progress bar

* Fixed logic bug that caused bar to jump one extra space at the end [davglass]
* Working with readline impl, even on Windows [davglass]
* Using readline instead of the \r hack [davglass]
### 1.1.3 / 2013-12-31

0.0.5 / 2012-08-07
==================
* fixes a bug where bar would bug when initializing
* allows to pass updated tokens when ticking or updating the bar
* fixes a bug where the bar would throw if skipping to far

### 1.1.2 / 2013-10-17

* lets you pass an `fmt` and a `total` instead of an options object

### 1.1.0 / 2013-09-18

* eta and elapsed tokens default to 0.0 instead of ?.?
* better JSDocs
* added back and forth example
* added method to update the progress bar to a specific percentage
* added an option to hide the bar on completion

### 1.0.1 / 2013-08-07

* on os x readline now works, reverting the terminal hack

### 1.0.0 / 2013-06-18

* remove .version
* merge pull request #15 from davglass/readline-osx
* on OSX revert back to terminal hack to avoid a readline bug

### 0.1.0 / 2012-09-19

* fixed logic bug that caused bar to jump one extra space at the end [davglass]
* working with readline impl, even on Windows [davglass]
* using readline instead of the \r hack [davglass]

### 0.0.5 / 2012-08-07

* add ability to tick by zero chunks - tick(0)
* fix ETA. Closes #4 [lwille]

0.0.4 / 2011-11-14
==================
### 0.0.4 / 2011-11-14

* Allow more recent versions of node
* allow more recent versions of node

0.0.3 / 2011-04-20
==================
### 0.0.3 / 2011-04-20

* Changed; erase the line when complete
* changed; erase the line when complete

0.0.2 / 2011-04-20
==================
### 0.0.2 / 2011-04-20

* Added custom tokens support
* Fixed; clear line before writing
* added custom tokens support
* fixed; clear line before writing

0.0.1 / 2010-01-03
==================
### 0.0.1 / 2010-01-03

* Initial release
* initial release
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

EXAMPLES = $(foreach EXAMPLE, $(wildcard examples/*.js), $(EXAMPLE))
.PHONY: $(EXAMPLES)

.PHONY: test
test: $(EXAMPLES)

.PHONY: $(EXAMPLES)
$(EXAMPLES): ; node $@ && echo
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flexible CLI ASCII progress bar for Node.js.
Flexible ascii progress bar.

## Installation

Expand Down
85 changes: 34 additions & 51 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
exports = module.exports = ProgressBar;

/**
* Initialize a `ProgressBar` with the given
* `fmt` string and `options` or `total`.
* Initialize a `ProgressBar` with the given `fmt` string and `options` or
* `total`.
*
* Options:
*
Expand All @@ -22,6 +22,7 @@ exports = module.exports = ProgressBar;
* - `complete` completion character defaulting to "="
* - `incomplete` incomplete character defaulting to "-"
* - `callback` optional function to call when the progress bar completes
* - `clear` will clear the progress bar upon termination
*
* Tokens:
*
Expand All @@ -32,21 +33,13 @@ exports = module.exports = ProgressBar;
* - `:percent` completion percentage
* - `:eta` eta in seconds
*
* @param {String} fmt
* @param {Object|Number} options or total
* @param {string} fmt
* @param {object|number} options or total
* @api public
*/

function ProgressBar(fmt, options) {
this.stream = options.stream || process.stderr;
this.rl = require('readline').createInterface({
input: process.stdin,
output: this.stream
});
this.rl.setPrompt('', 0);
this.rl.clearLine = function() {
this.write(null, {ctrl: true, name: 'u'});
};

if (typeof(options) == 'number') {
var total = options;
Expand All @@ -64,19 +57,18 @@ function ProgressBar(fmt, options) {
this.width = options.width || this.total;
this.clear = options.clear
this.chars = {
complete: options.complete || '='
, incomplete: options.incomplete || '-'
complete : options.complete || '=',
incomplete : options.incomplete || '-'
};
this.callback = options.callback || function () {};
this.lastDraw = '';
}

/**
* "tick" the progress bar with optional `len` and
* optional `tokens`.
* "tick" the progress bar with optional `len` and optional `tokens`.
*
* @param {Number|Object} len or tokens
* @param {Object} tokens
* @param {number|object} len or tokens
* @param {object} tokens
* @api public
*/

Expand All @@ -103,26 +95,24 @@ ProgressBar.prototype.tick = function(len, tokens){
};

/**
* Method to render the progress bar with optional `tokens` to
* place in the progress bar's `fmt` field.
* Method to render the progress bar with optional `tokens` to place in the
* progress bar's `fmt` field.
*
* @param {Object} tokens
* @param {object} tokens
* @api public
*/

ProgressBar.prototype.render = function(tokens){
if (!this.stream.isTTY) {
return;
}
ProgressBar.prototype.render = function (tokens) {
if (!this.stream.isTTY) return;

var ratio = this.curr / this.total;
ratio = Math.min(Math.max(ratio, 0), 1);

var percent = ratio * 100
, complete = Math.round(this.width * ratio)
, incomplete
, elapsed = new Date - this.start
, eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);
var percent = ratio * 100;
var complete = Math.round(this.width * ratio);
var incomplete;
var elapsed = new Date - this.start;
var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);

complete = Array(complete).join(this.chars.complete);
incomplete = Array(this.width - complete.length).join(this.chars.incomplete);
Expand All @@ -131,19 +121,17 @@ ProgressBar.prototype.render = function(tokens){
.replace(':bar', complete + incomplete)
.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(':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) + '%');

if (tokens) {
for (var key in tokens) {
str = str.replace(':' + key, tokens[key]);
}
}
if (tokens) for (var key in tokens) str = str.replace(':' + key, tokens[key]);

if (this.lastDraw !== str) {
this.rl.clearLine();
this.rl.write(str);
this.stream.clearLine();
this.stream.cursorTo(0);
this.stream.write(str);
this.lastDraw = str;
}
};
Expand All @@ -157,12 +145,12 @@ ProgressBar.prototype.render = function(tokens){
*
* A ratio of 0.5 will attempt to set the progress to halfway.
*
* @param {Number} ratio The ratio (between 0 and 1 inclusive) to set the
* overall completion to.
* @param {number} ratio The ratio (between 0 and 1 inclusive) to set the
* overall completion to.
* @api public
*/

ProgressBar.prototype.update = function(ratio, tokens) {
ProgressBar.prototype.update = function (ratio, tokens) {
var goal = Math.floor(ratio * this.total);
var delta = goal - this.curr;

Expand All @@ -175,14 +163,9 @@ ProgressBar.prototype.update = function(ratio, tokens) {
* @api public
*/

ProgressBar.prototype.terminate = function() {
this.rl.resume();

ProgressBar.prototype.terminate = function () {
if (this.clear) {
this.rl.clearLine();
this.rl.close();
} else {
this.rl.close();
console.log();
}
this.stream.clearLine();
this.stream.cursorTo(0);
} else console.log();
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "progress"
, "version": "1.1.4"
, "version": "1.1.5"
, "description": "Flexible ascii progress bar"
, "keywords": ["cli", "progress"]
, "author": "TJ Holowaychuk <tj@vision-media.ca>"
Expand Down