Skip to content

Commit

Permalink
Add isSilent option (#155)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
brandon93s and sindresorhus committed Sep 6, 2020
1 parent 561bc85 commit 1bd812a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Expand Up @@ -94,6 +94,13 @@ declare namespace ora {
*/
readonly isEnabled?: boolean;

/**
Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
@default false
*/
readonly isSilent?: boolean;

/**
Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on `Enter` key presses, and prevents buffering of input while the spinner is running.
Expand Down
37 changes: 37 additions & 0 deletions index.js
Expand Up @@ -122,6 +122,7 @@ class Ora {
this.stream = this.options.stream;
this.id = undefined;
this.isEnabled = typeof this.options.isEnabled === 'boolean' ? this.options.isEnabled : isInteractive({stream: this.stream});
this.isSilent = typeof this.options.isSilent === 'boolean' ? this.options.isSilent : false;

// Set *after* `this.stream`
this.text = this.options.text;
Expand Down Expand Up @@ -219,6 +220,30 @@ class Ora {
this.updateLineCount();
}

get isEnabled() {
return this._isEnabled && !this.isSilent;
}

set isEnabled(value) {
if (typeof value !== 'boolean') {
throw new TypeError('The `isEnabled` option must be a boolean');
}

this._isEnabled = value;
}

get isSilent() {
return this._isSilent;
}

set isSilent(value) {
if (typeof value !== 'boolean') {
throw new TypeError('The `isSilent` option must be a boolean');
}

this._isSilent = value;
}

frame() {
const {frames} = this.spinner;
let frame = frames[this.frameIndex];
Expand Down Expand Up @@ -254,6 +279,10 @@ class Ora {
}

render() {
if (this.isSilent) {
return this;
}

this.clear();
this.stream.write(this.frame());
this.linesToClear = this.lineCount;
Expand All @@ -266,6 +295,10 @@ class Ora {
this.text = text;
}

if (this.isSilent) {
return this;
}

if (!this.isEnabled) {
if (this.text) {
this.stream.write(`- ${this.text}\n`);
Expand Down Expand Up @@ -331,6 +364,10 @@ class Ora {
}

stopAndPersist(options = {}) {
if (this.isSilent) {
return this;
}

const prefixText = options.prefixText || this.prefixText;
const text = options.text || this.text;
const fullText = (typeof text === 'string') ? ' ' + text : '';
Expand Down
4 changes: 3 additions & 1 deletion index.test-d.ts
Expand Up @@ -16,6 +16,7 @@ ora({indent: 1});
ora({interval: 80});
ora({stream: new PassThroughStream()});
ora({isEnabled: true});
ora({isSilent: false});
ora({discardStdin: true});

spinner.color = 'yellow';
Expand Down Expand Up @@ -49,5 +50,6 @@ promise(resolves, {
stream: new PassThroughStream(),
text: 'foo',
color: 'blue',
isEnabled: true
isEnabled: true,
isSilent: false
});
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -114,6 +114,13 @@ Force enable/disable the spinner. If not specified, the spinner will be enabled

Note that `{isEnabled: 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.

##### isSilent

Type: `boolean`\
Default: `false`

Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.

##### discardStdin

Type: `boolean`\
Expand Down
31 changes: 24 additions & 7 deletions test.js
Expand Up @@ -24,6 +24,7 @@ const doSpinner = async (fn, extraOptions = {}) => {
text: 'foo',
color: false,
isEnabled: true,
isSilent: false,
...extraOptions
});

Expand Down Expand Up @@ -130,6 +131,19 @@ test('.stopAndPersist() - isEnabled:false outputs text', macro, spinner => {
spinner.stopAndPersist({symbol: '@', text: 'all done'});
}, /- foo\n@ all done\n$/, {isEnabled: false});

test('.start() - isSilent:true no output', macro, spinner => {
spinner.stop();
}, /^(?![\s\S])/, {isSilent: true});

test('.stopAndPersist() - isSilent:true no output', macro, spinner => {
spinner.stopAndPersist({symbol: '@', text: 'all done'});
}, /^(?![\s\S])/, {isSilent: true});

test('.stopAndPersist() - isSilent:true can be disabled', macro, spinner => {
spinner.isSilent = false;
spinner.stopAndPersist({symbol: '@', text: 'all done'});
}, /@ all done\n$/, {isSilent: true});

test('.promise() - resolves', async t => {
const stream = getPassThroughStream();
const output = getStream(stream);
Expand Down Expand Up @@ -294,16 +308,19 @@ test('set the correct interval when changing spinner (string case)', t => {

spinner.spinner = 'layer';

t.is(spinner.interval, 150);
const expectedInterval = process.platform === 'win32' ? 130 : 150;
t.is(spinner.interval, expectedInterval);
});

test('throw when incorrect spinner', t => {
const ora = new Ora();
if (process.platform !== 'win32') {
test('throw when incorrect spinner', t => {
const ora = new Ora();

t.throws(() => {
ora.spinner = 'random-string-12345';
}, /no built-in spinner/);
});
t.throws(() => {
ora.spinner = 'random-string-12345';
}, /no built-in spinner/);
});
}

test('indent option', t => {
const stream = getPassThroughStream();
Expand Down

0 comments on commit 1bd812a

Please sign in to comment.