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

Issue #63 implementation #69

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var green = '\u001b[42m \u001b[0m';
var red = '\u001b[41m \u001b[0m';

var ProgressBar = require('../');

var bar = new ProgressBar(' [:bar]', {
complete: green,
incomplete: red,
total: 20
});

var id = setInterval(function (){
bar.tick();
if (bar.complete) {
clearInterval(id);
}
}, 100);
2 changes: 1 addition & 1 deletion examples/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ProgressBar = require('../');

var contentLength = 128 * 1024;

var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
var bar = new ProgressBar(' downloading [:bar] :percent :eta', {
complete: '='
, incomplete: ' '
, width: 20
Expand Down
2 changes: 1 addition & 1 deletion examples/exact.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var ProgressBar = require('../');

var bar = new ProgressBar(' progress [:bar] :percent :etas', {
var bar = new ProgressBar(' progress [:bar] :percent :eta', {
complete: '='
, incomplete: ' '
, width: 40
Expand Down
2 changes: 1 addition & 1 deletion examples/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function bar4() {
}

function bar5() {
var bar = new ProgressBar(' [:bar] :elapseds elapsed, eta :etas', {
var bar = new ProgressBar(' [:bar] :elapsed elapsed, eta :eta', {
width: 8
, total: 50
});
Expand Down
4 changes: 2 additions & 2 deletions examples/toolong.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var ProgressBar = require('../');

// simulated download, passing the chunk lengths to tick()

var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
var bar = new ProgressBar(' downloading [:bar] :percent :eta', {
complete: '='
, incomplete: ' '
, width: 1024 /* something longer than the terminal width */
Expand All @@ -18,6 +18,6 @@ var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
bar.tick(1);

if (!bar.complete) {
setTimeout(next, 10);
setTimeout(next, 100);
}
})();
17 changes: 11 additions & 6 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* MIT Licensed
*/

/**
Require ms.js Module: https://github.com/guille/ms.js
*/
var ms = require('ms');

/**
* Expose `ProgressBar`.
*/
Expand Down Expand Up @@ -109,26 +114,26 @@ ProgressBar.prototype.render = function (tokens) {
ratio = Math.min(Math.max(ratio, 0), 1);

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

/* 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(':elapsed', ms((isNaN(elapsed) ? '0.0' : elapsed).toFixed(5)*10))
.replace(':eta', ms(((isNaN(eta) || !isFinite(eta)) ? '0.0' : eta).toFixed(5)*10))
.replace(':percent', percent.toFixed(0) + '%');

/* compute the available space (non-zero) for the bar */
var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
var width = Math.min(this.width, availableSpace);

/* TODO: the following assumes the user has one ':bar' token */
complete = Array(Math.round(width * ratio)).join(this.chars.complete);
incomplete = Array(width - complete.length).join(this.chars.incomplete);
completeLength = Math.round(width * ratio);
complete = Array(completeLength + 1).join(this.chars.complete);
incomplete = Array(width - completeLength + 1).join(this.chars.incomplete);

/* fill in the actual progress bar */
str = str.replace(':bar', complete + incomplete);
Expand Down
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
{
"name": "progress"
, "version": "1.1.7"
, "version": "1.1.8"
, "description": "Flexible ascii progress bar"
, "keywords": ["cli", "progress"]
, "author": "TJ Holowaychuk <tj@vision-media.ca>"
, "dependencies": {}
, "contributors": [
{
"name": "Christoffer Hallas"
, "email": "christoffer.hallas@gmail.com"
}
, {
"name": "Jordan Scales"
, "email": "scalesjordan@gmail.com"
}
, {
"name": "Marcelo Boeira"
, "email": "me@marceloboeira.com"
}
]
, "dependencies": {"ms": ">=0.6.2"}
, "main": "index"
, "engines": { "node": ">=0.4.0" }
, "repository": "git://github.com/visionmedia/node-progress"
Expand Down