From d1f8d8529b3f95a28a5b561d29870a2e3250b1c5 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 18 Jul 2018 03:56:22 +0700 Subject: [PATCH] Ora should still output text when not `enabled` You would still want to see the text for debugging purposes in CI, for example. Closes #78 --- index.js | 12 +++++++----- readme.md | 4 ++-- test.js | 34 +++++++++++++--------------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 3522c07..f0a5b57 100644 --- a/index.js +++ b/index.js @@ -101,13 +101,19 @@ class Ora { this.text = text; } - if (!this.enabled || this.isSpinning) { + if (!this.enabled) { + this.stream.write(`- ${this.text}\n`); + return this; + } + + if (this.isSpinning) { return this; } if (this.hideCursor) { cliCursor.hide(this.stream); } + this.render(); this.id = setInterval(this.render.bind(this), this.interval); @@ -147,10 +153,6 @@ class Ora { } stopAndPersist(options = {}) { - if (!this.enabled) { - return this; - } - this.stop(); this.stream.write(`${options.symbol || ' '} ${options.text || this.text}\n`); diff --git a/readme.md b/readme.md index bcdc845..5f32082 100644 --- a/readme.md +++ b/readme.md @@ -36,8 +36,6 @@ setTimeout(() => { ## API -It will gracefully not do anything when there's no TTY or when running in a CI. - ### ora([options|text]) If a string is provided, it is treated as a shortcut for [`options.text`](#text). @@ -107,6 +105,8 @@ Type: `boolean` Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment. +Note that `{enabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text. + ### Instance #### .start([text]) diff --git a/test.js b/test.js index af8f06a..e9fa8e8 100644 --- a/test.js +++ b/test.js @@ -15,7 +15,7 @@ const getPassThroughStream = () => { return stream; }; -const doSpinner = async fn => { +const doSpinner = async (fn, extraOptions = {}) => { const stream = getPassThroughStream(); const output = getStream(stream); @@ -23,7 +23,8 @@ const doSpinner = async fn => { stream, text: 'foo', color: false, - enabled: true + enabled: true, + ...extraOptions }); spinner.start(); @@ -33,8 +34,8 @@ const doSpinner = async fn => { return stripAnsi(await output); }; -const macro = async (t, fn, expected) => { - t.regex(await doSpinner(fn), expected); +const macro = async (t, fn, expected, extraOptions = {}) => { + t.regex(await doSpinner(fn, extraOptions), expected); }; test('main', macro, spinner => { @@ -116,28 +117,19 @@ test('.stopAndPersist() - with new symbol and text', macro, spinner => { spinner.stopAndPersist({symbol: '@', text: 'all done'}); }, /@ all done/); -test('.stopAndPersist() - enabled false - no output', async t => { - const stream = getPassThroughStream(); - const output = getStream(stream); - - const spinner = new Ora({ - stream, - text: 'foo', - color: false, - enabled: false - }); - - spinner.start(); - spinner.stopAndPersist(); - stream.end(); - t.is(await output, ''); -}); - test('.start(text)', macro, spinner => { spinner.start('Test text'); spinner.stopAndPersist(); }, /Test text/); +test('.start() - enabled:false outputs text', macro, spinner => { + spinner.stop(); +}, /- foo/, {enabled: false}); + +test('.stopAndPersist() - enabled:false outputs text', macro, spinner => { + spinner.stopAndPersist({symbol: '@', text: 'all done'}); +}, /- foo\n@ all done/, {enabled: false}); + test('.promise() - resolves', async t => { const stream = getPassThroughStream(); const output = getStream(stream);