Skip to content

Commit e135a3d

Browse files
Merge pull request #131 from martin770/add_interrupt
feature(interrupt): added feature to display message during progress
2 parents 971eef9 + 51ba872 commit e135a3d

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

Readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ The above example result in a progress bar like the one below.
121121
downloading [===== ] 39/bps 29% 3.7s
122122
```
123123

124+
### Interrupt
125+
126+
To display a message during progress bar execution, use `interupt()`
127+
```javascript
128+
var ProgressBar = require('progress');
129+
130+
var bar = new ProgressBar(':bar :current/:total', { total: 10 });
131+
var timer = setInterval(function () {
132+
bar.tick();
133+
if (bar.complete) {
134+
clearInterval(timer);
135+
} else if (bar.curr === 5) {
136+
bar.interrupt('this message appears above the progress bar\ncurrent progress is ' + bar.curr + '/' + bar.total);
137+
}
138+
}, 1000);
139+
```
140+
124141
You can see more examples in the `examples` folder.
125142

126143
## License

examples/interrupt.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* An example to show a progress bar being interrupted,
3+
* displaying messages above the bar while keeping the
4+
* progress intact
5+
*/
6+
7+
var ProgressBar = require('progress');
8+
9+
var bar = new ProgressBar(':bar :current/:total', { total: 10 });
10+
var timer = setInterval(function () {
11+
bar.tick();
12+
if (bar.complete) {
13+
clearInterval(timer);
14+
} else if (bar.curr === 5 || bar.curr === 8) {
15+
bar.interrupt('interrupt: current progress is ' + bar.curr + '/' + bar.total);
16+
}
17+
}, 1000);
18+

lib/node-progress.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ ProgressBar.prototype.update = function (ratio, tokens) {
188188
this.tick(delta, tokens);
189189
};
190190

191+
/**
192+
* "interrupt" the progress bar and write a message above it.
193+
* @param {string} message The message to write.
194+
* @api public
195+
*/
196+
197+
ProgressBar.prototype.interrupt = function (message) {
198+
// clear the current line
199+
this.stream.clearLine();
200+
// move the cursor to the start of the line
201+
this.stream.cursorTo(0);
202+
// write the message text
203+
this.stream.write(message);
204+
// terminate the line after writing the message
205+
this.stream.write('\n');
206+
// re-display the progress bar with its lastDraw
207+
this.stream.write(this.lastDraw);
208+
};
209+
191210
/**
192211
* Terminates a progress bar.
193212
*
@@ -198,5 +217,7 @@ ProgressBar.prototype.terminate = function () {
198217
if (this.clear) {
199218
this.stream.clearLine();
200219
this.stream.cursorTo(0);
201-
} else this.stream.write('\n');
220+
} else {
221+
this.stream.write('\n');
222+
}
202223
};

0 commit comments

Comments
 (0)