Skip to content

Commit 51ba872

Browse files
author
James Martin
committed
feature(interrupt): added feature to interrupt the bar and display a message
1 parent d479135 commit 51ba872

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
@@ -119,6 +119,23 @@ The above example result in a progress bar like the one below.
119119
downloading [===== ] 29% 3.7s
120120
```
121121

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

124141
## 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
@@ -180,6 +180,25 @@ ProgressBar.prototype.update = function (ratio, tokens) {
180180
this.tick(delta, tokens);
181181
};
182182

183+
/**
184+
* "interrupt" the progress bar and write a message above it.
185+
* @param {string} message The message to write.
186+
* @api public
187+
*/
188+
189+
ProgressBar.prototype.interrupt = function (message) {
190+
// clear the current line
191+
this.stream.clearLine();
192+
// move the cursor to the start of the line
193+
this.stream.cursorTo(0);
194+
// write the message text
195+
this.stream.write(message);
196+
// terminate the line after writing the message
197+
this.stream.write('\n');
198+
// re-display the progress bar with its lastDraw
199+
this.stream.write(this.lastDraw);
200+
};
201+
183202
/**
184203
* Terminates a progress bar.
185204
*
@@ -190,5 +209,7 @@ ProgressBar.prototype.terminate = function () {
190209
if (this.clear) {
191210
this.stream.clearLine();
192211
this.stream.cursorTo(0);
193-
} else this.stream.write('\n');
212+
} else {
213+
this.stream.write('\n');
214+
}
194215
};

0 commit comments

Comments
 (0)