From 330b2501bfa87f6d8d6dccb64a728b8c39f5ef62 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Jun 2017 18:25:42 +0200 Subject: [PATCH] Add interruptBegin and interruptEnd methods This allows one to employ custom logging facilities during an interrupt, like for example when one wants to log both to a file and to lines above the bar. --- Readme.md | 9 +++++++++ lib/node-progress.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Readme.md b/Readme.md index 6d4271a..a73c2f4 100644 --- a/Readme.md +++ b/Readme.md @@ -139,6 +139,15 @@ var timer = setInterval(function () { }, 1000); ``` +It's also possible to start and stop interrupts manually, allowing synchronous +logging between through the `interruptBegin` and `interruptEnd` methods: + +```js +bar.interruptBegin(); +console.log('message'); +bar.interruptEnd(); +``` + You can see more examples in the `examples` folder. ## License diff --git a/lib/node-progress.js b/lib/node-progress.js index 2b62641..d0cd76c 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -215,6 +215,27 @@ ProgressBar.prototype.interrupt = function (message) { this.stream.write(this.lastDraw); }; +/** + * Begin a interrupt so the user can manually write messages above the bar. + * + * @api public + */ + +ProgressBar.prototype.interruptBegin = function () { + this.stream.clearLine(); + this.stream.cursorTo(0); +}; + +/** + * End a interrupt, rendering the last draw again. + * + * @api public + */ + +ProgressBar.prototype.interruptEnd = function () { + this.stream.write(this.lastDraw); +}; + /** * Terminates a progress bar. *