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
17 changes: 17 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ The above example result in a progress bar like the one below.
downloading [===== ] 29% 3.7s
```

### Interrupt

To display a message during progress bar execution, use `interupt()`
```javascript
var ProgressBar = require('progress');

var bar = new ProgressBar(':bar :current/:total', { total: 10 });
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
clearInterval(timer);
} else if (bar.curr === 5) {
bar.interrupt('this message appears above the progress bar\ncurrent progress is ' + bar.curr + '/' + bar.total);
}
}, 1000);
```

You can see more examples in the `examples` folder.

## License
Expand Down
18 changes: 18 additions & 0 deletions examples/interrupt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* An example to show a progress bar being interrupted,
* displaying messages above the bar while keeping the
* progress intact
*/

var ProgressBar = require('progress');

var bar = new ProgressBar(':bar :current/:total', { total: 10 });
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
clearInterval(timer);
} else if (bar.curr === 5 || bar.curr === 8) {
bar.interrupt('interrupt: current progress is ' + bar.curr + '/' + bar.total);
}
}, 1000);

23 changes: 22 additions & 1 deletion lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ ProgressBar.prototype.update = function (ratio, tokens) {
this.tick(delta, tokens);
};

/**
* "interrupt" the progress bar and write a message above it.
* @param {string} message The message to write.
* @api public
*/

ProgressBar.prototype.interrupt = function (message) {
// clear the current line
this.stream.clearLine();
// move the cursor to the start of the line
this.stream.cursorTo(0);
// write the message text
this.stream.write(message);
// terminate the line after writing the message
this.stream.write('\n');
// re-display the progress bar with its lastDraw
this.stream.write(this.lastDraw);
};

/**
* Terminates a progress bar.
*
Expand All @@ -190,5 +209,7 @@ ProgressBar.prototype.terminate = function () {
if (this.clear) {
this.stream.clearLine();
this.stream.cursorTo(0);
} else this.stream.write('\n');
} else {
this.stream.write('\n');
}
};