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 @@ -119,6 +119,23 @@ The above example result in a progress bar like the one below.
119119downloading [===== ] 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\n current progress is ' + bar .curr + ' /' + bar .total );
135+ }
136+ }, 1000 );
137+ ```
138+
122139You can see more examples in the ` examples ` folder.
123140
124141## 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 @@ -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} ;
You can’t perform that action at this time.
0 commit comments