File tree Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -121,6 +121,23 @@ The above example result in a progress bar like the one below.
121121downloading [===== ] 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\n current progress is ' + bar .curr + ' /' + bar .total );
137+ }
138+ }, 1000 );
139+ ```
140+
124141You can see more examples in the ` examples ` folder.
125142
126143## License
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff 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} ;
You can’t perform that action at this time.
0 commit comments