Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sindresorhus committed Jul 17, 2018
1 parent ab3d77f commit d1f8d85
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
12 changes: 7 additions & 5 deletions index.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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`);

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -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).
Expand Down Expand Up @@ -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])
Expand Down
34 changes: 13 additions & 21 deletions test.js
Expand Up @@ -15,15 +15,16 @@ const getPassThroughStream = () => {
return stream;
};

const doSpinner = async fn => {
const doSpinner = async (fn, extraOptions = {}) => {
const stream = getPassThroughStream();
const output = getStream(stream);

const spinner = new Ora({
stream,
text: 'foo',
color: false,
enabled: true
enabled: true,
...extraOptions
});

spinner.start();
Expand All @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d1f8d85

Please sign in to comment.