Skip to content

Commit 1bd812a

Browse files
brandon93ssindresorhus
andauthoredSep 6, 2020
Add isSilent option (#155)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 561bc85 commit 1bd812a

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed
 

‎index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ declare namespace ora {
9494
*/
9595
readonly isEnabled?: boolean;
9696

97+
/**
98+
Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
99+
100+
@default false
101+
*/
102+
readonly isSilent?: boolean;
103+
97104
/**
98105
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.
99106

‎index.js

+37
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class Ora {
122122
this.stream = this.options.stream;
123123
this.id = undefined;
124124
this.isEnabled = typeof this.options.isEnabled === 'boolean' ? this.options.isEnabled : isInteractive({stream: this.stream});
125+
this.isSilent = typeof this.options.isSilent === 'boolean' ? this.options.isSilent : false;
125126

126127
// Set *after* `this.stream`
127128
this.text = this.options.text;
@@ -219,6 +220,30 @@ class Ora {
219220
this.updateLineCount();
220221
}
221222

223+
get isEnabled() {
224+
return this._isEnabled && !this.isSilent;
225+
}
226+
227+
set isEnabled(value) {
228+
if (typeof value !== 'boolean') {
229+
throw new TypeError('The `isEnabled` option must be a boolean');
230+
}
231+
232+
this._isEnabled = value;
233+
}
234+
235+
get isSilent() {
236+
return this._isSilent;
237+
}
238+
239+
set isSilent(value) {
240+
if (typeof value !== 'boolean') {
241+
throw new TypeError('The `isSilent` option must be a boolean');
242+
}
243+
244+
this._isSilent = value;
245+
}
246+
222247
frame() {
223248
const {frames} = this.spinner;
224249
let frame = frames[this.frameIndex];
@@ -254,6 +279,10 @@ class Ora {
254279
}
255280

256281
render() {
282+
if (this.isSilent) {
283+
return this;
284+
}
285+
257286
this.clear();
258287
this.stream.write(this.frame());
259288
this.linesToClear = this.lineCount;
@@ -266,6 +295,10 @@ class Ora {
266295
this.text = text;
267296
}
268297

298+
if (this.isSilent) {
299+
return this;
300+
}
301+
269302
if (!this.isEnabled) {
270303
if (this.text) {
271304
this.stream.write(`- ${this.text}\n`);
@@ -331,6 +364,10 @@ class Ora {
331364
}
332365

333366
stopAndPersist(options = {}) {
367+
if (this.isSilent) {
368+
return this;
369+
}
370+
334371
const prefixText = options.prefixText || this.prefixText;
335372
const text = options.text || this.text;
336373
const fullText = (typeof text === 'string') ? ' ' + text : '';

‎index.test-d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ora({indent: 1});
1616
ora({interval: 80});
1717
ora({stream: new PassThroughStream()});
1818
ora({isEnabled: true});
19+
ora({isSilent: false});
1920
ora({discardStdin: true});
2021

2122
spinner.color = 'yellow';
@@ -49,5 +50,6 @@ promise(resolves, {
4950
stream: new PassThroughStream(),
5051
text: 'foo',
5152
color: 'blue',
52-
isEnabled: true
53+
isEnabled: true,
54+
isSilent: false
5355
});

‎readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ Force enable/disable the spinner. If not specified, the spinner will be enabled
114114

115115
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.
116116

117+
##### isSilent
118+
119+
Type: `boolean`\
120+
Default: `false`
121+
122+
Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
123+
117124
##### discardStdin
118125

119126
Type: `boolean`\

‎test.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const doSpinner = async (fn, extraOptions = {}) => {
2424
text: 'foo',
2525
color: false,
2626
isEnabled: true,
27+
isSilent: false,
2728
...extraOptions
2829
});
2930

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

134+
test('.start() - isSilent:true no output', macro, spinner => {
135+
spinner.stop();
136+
}, /^(?![\s\S])/, {isSilent: true});
137+
138+
test('.stopAndPersist() - isSilent:true no output', macro, spinner => {
139+
spinner.stopAndPersist({symbol: '@', text: 'all done'});
140+
}, /^(?![\s\S])/, {isSilent: true});
141+
142+
test('.stopAndPersist() - isSilent:true can be disabled', macro, spinner => {
143+
spinner.isSilent = false;
144+
spinner.stopAndPersist({symbol: '@', text: 'all done'});
145+
}, /@ all done\n$/, {isSilent: true});
146+
133147
test('.promise() - resolves', async t => {
134148
const stream = getPassThroughStream();
135149
const output = getStream(stream);
@@ -294,16 +308,19 @@ test('set the correct interval when changing spinner (string case)', t => {
294308

295309
spinner.spinner = 'layer';
296310

297-
t.is(spinner.interval, 150);
311+
const expectedInterval = process.platform === 'win32' ? 130 : 150;
312+
t.is(spinner.interval, expectedInterval);
298313
});
299314

300-
test('throw when incorrect spinner', t => {
301-
const ora = new Ora();
315+
if (process.platform !== 'win32') {
316+
test('throw when incorrect spinner', t => {
317+
const ora = new Ora();
302318

303-
t.throws(() => {
304-
ora.spinner = 'random-string-12345';
305-
}, /no built-in spinner/);
306-
});
319+
t.throws(() => {
320+
ora.spinner = 'random-string-12345';
321+
}, /no built-in spinner/);
322+
});
323+
}
307324

308325
test('indent option', t => {
309326
const stream = getPassThroughStream();

0 commit comments

Comments
 (0)
Please sign in to comment.