Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ Supported Options:
* `total` - The total number of operations that _YOUR_ script will execute.
* `maxBurden` - The maximum 'burden' that the progress bar should incur. See more about burden below.
* `showBurden` - Mostly for debugging. Show the current burden / skipped steps with the other metrics.
* `texts` - Customize shown texts for your own need. Available are
* `info`
* `unit`
* `elapsed`
* `burden`
* `remaining`
* `finished`

### pace.op([count]) ###
Signal to pace that an operation was completed in your script by calling
Expand Down
68 changes: 46 additions & 22 deletions pace.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ function Pace(options) {
}
this.total = options.total;

// Update texts if needed
this.texts = {
info : "Processing: ",
unit : "",
elapsed : "Elapsed: ",
burden : "Burden: ",
remaining : "Remaining: ",
finished : "Finished!\n\n"
};
if (!!options.texts) {
for (var key in options.texts) {
if (options.texts.hasOwnProperty(key)) {
this.texts[key] = options.texts[key];
}
}
}

this.indent = this.texts.info.length;

// Current item number.
this.current = 0;

Expand All @@ -57,13 +76,14 @@ function Pace(options) {
this.skip_steps = 0;
this.skipped = 0;
this.aborted = false;
this.finished = false;

// Setup charm.
this.charm = charm();
this.charm.pipe(process.stdout);

// Prepare the output.
this.charm.write("\n\n\n");
this.charm.write("\n\n");
}

/**
Expand All @@ -81,15 +101,15 @@ module.exports = function(options) {
/**
* An operation has been emitted.
*/
Pace.prototype.op = function op(count) {
Pace.prototype.op = function (count) {
if (count) {
this.current = count;
}
else {
this.current++;
}

if (this.burdenReached()) {
if (this.burdenReached() && this.current < this.total || this.finished) {
return;
}

Expand All @@ -109,7 +129,7 @@ Pace.prototype.op = function op(count) {

// The task is complete.
if (this.current >= this.total) {
this.finished();
this.finish();
}

// Record end time.
Expand All @@ -120,7 +140,7 @@ Pace.prototype.op = function op(count) {
/**
* Update times.
*/
Pace.prototype.updateTimes = function updateTimes() {
Pace.prototype.updateTimes = function () {
this.elapsed = this.time_start - this.started;
if (this.time_end > 0) {
this.outer_time = this.time_start - this.time_end;
Expand All @@ -143,15 +163,15 @@ Pace.prototype.updateTimes = function updateTimes() {
/**
* Move the cursor back to the beginning and clear old output.
*/
Pace.prototype.clear = function clear() {
this.charm.erase('line').up(1).erase('line').up(1).erase('line').write("\r");
Pace.prototype.clear = function () {
this.charm.erase('line').up(1).erase('line').up(1).erase('line').left(100);
};

/**
* Output the progress bar.
*/
Pace.prototype.outputProgress = function outputProgress() {
this.charm.write('Processing: ');
Pace.prototype.outputProgress = function () {
this.charm.write(this.texts.info);
this.charm.foreground('green').background('green');
for (var i = 0; i < ((this.current / this.total) * this.size) - 1 ; i++) {
this.charm.write(' ');
Expand All @@ -167,17 +187,17 @@ Pace.prototype.outputProgress = function outputProgress() {
/**
* Output numerical progress stats.
*/
Pace.prototype.outputStats = function outputStats() {
Pace.prototype.outputStats = function () {
this.perc = (this.current/this.total)*100;
this.perc = padLeft(this.perc.toFixed(2), 2);
this.charm.write(' ').display('bright').write(this.perc + '%').display('reset');
this.charm.write(spaces(this.indent)).display('bright').write(this.perc + '%').display('reset');
this.total_len = formatNumber(this.total).length;
this.charm.write(' ').display('bright').write(padLeft(formatNumber(this.current), this.total_len)).display('reset');
this.charm.write('/' + formatNumber(this.total));
this.charm.write('/' + formatNumber(this.total) + this.texts.unit);

// Output burden.
if (this.show_burden) {
this.charm.write(' ').display('bright').write('Burden: ').display('reset');
this.charm.write(' ').display('bright').write(this.texts.burden).display('reset');
this.charm.write(this.time_burden.toFixed(2) + '% / ' + this.skip_steps);
}

Expand All @@ -187,38 +207,37 @@ Pace.prototype.outputStats = function outputStats() {
/**
* Output times.
*/
Pace.prototype.outputTimes = function outputTimes() {
Pace.prototype.outputTimes = function () {
// Output times.
var hours = Math.floor(this.elapsed / (1000 * 60 * 60));
var min = Math.floor(((this.elapsed / 1000) % (60 * 60)) / 60);
var sec = Math.floor((this.elapsed / 1000) % 60);

this.charm.write(' ').display('bright').write('Elapsed: ').display('reset');
this.charm.write(spaces(this.indent)).display('bright').write(this.texts.elapsed).display('reset');
this.charm.write(hours + 'h ' + min + 'm ' + sec + 's');

if (this.time_left){
hours = Math.floor(this.time_left / (1000 * 60 * 60));
min = Math.floor(((this.time_left / 1000) % (60 * 60)) / 60);
sec = Math.ceil((this.time_left / 1000) % 60);

this.charm.write(' ').display('bright').write('Remaining: ').display('reset');
this.charm.write(' ').display('bright').write(this.texts.remaining).display('reset');
this.charm.write(hours + 'h ' + min + 'm ' + sec + 's');
}
};

/**
* The progress has finished.
* The progress has finish.
*/
Pace.prototype.finished = function finished() {
this.charm.write("\n\n");
this.charm.write('Finished!');
this.charm.write("\n\n");
Pace.prototype.finish = function () {
this.finished = true;
this.charm.write('\n').left(100).write(this.texts.finished);
};

/**
* Check if the burden threshold has been reached.
*/
Pace.prototype.burdenReached = function burdenReached() {
Pace.prototype.burdenReached = function () {
// Skip this cycle if the burden has determined we should.
if ((this.skip_steps > 0) && (this.current < this.total)) {
if (this.skipped < this.skip_steps) {
Expand All @@ -245,6 +264,11 @@ function padLeft(str, length, pad) {
return str;
}

// just the spaces
function spaces(length) {
return padLeft('', length, ' ');
}

// Ported from php.js. Same has php's number_format().
function formatNumber(number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Brian Link <[email protected]>",
"name": "pace",
"description": "Command-line progress bar and progress metrics. Helps you measure the 'pace' of a long-running script.",
"version": "0.0.4",
"version": "0.0.5",
"homepage": "http://cantina.github.com",
"repository": {
"type": "git",
Expand Down
17 changes: 12 additions & 5 deletions test/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
* Set the current position in op() and also randomly increase the total.
*/

var total = 50000,
var total = 100,
current = 0,
pace = require('../')(total);
pace = require('../')({
total : total,
texts : {
info : "Downloading: ",
unit : " bytes",
finished : "Woohoo!"
}
});

while (current++ < total) {
if (Math.random() > 0.9) {
while (current++ <= total) {
if (Math.random() > 0.9 || current === total) {
pace.op(current);
}

Expand All @@ -19,7 +26,7 @@ while (current++ < total) {
}

// Cause some work to be done.
for (var i = 0; i < 1000000; i++) {
for (var i = 0; i < 100000; i++) {
current = current;
}
}