Comparing version 5.0.0 to 5.1.0
@@ -21,2 +21,4 @@ /// <reference types="node"/> | ||
type PrefixTextGenerator = () => string; | ||
interface Options { | ||
@@ -29,5 +31,5 @@ /** | ||
/** | ||
Text to display before the spinner. No prefix text will be displayed if set to an empty string. | ||
Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string. | ||
*/ | ||
readonly prefixText?: string; | ||
readonly prefixText?: string | PrefixTextGenerator; | ||
@@ -98,2 +100,9 @@ /** | ||
/** | ||
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. | ||
@@ -124,7 +133,7 @@ | ||
/** | ||
Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string. | ||
Text or a function that returns text to be persisted before the symbol. No prefix text will be displayed if set to an empty string. | ||
Default: Current `prefixText`. | ||
*/ | ||
readonly prefixText?: string; | ||
readonly prefixText?: string | PrefixTextGenerator; | ||
} | ||
@@ -144,5 +153,5 @@ | ||
/** | ||
Change the text before the spinner. No prefix text will be displayed if set to an empty string. | ||
Change the text or function that returns text before the spinner. No prefix text will be displayed if set to an empty string. | ||
*/ | ||
prefixText: string; | ||
prefixText: string | PrefixTextGenerator; | ||
@@ -149,0 +158,0 @@ /** |
54
index.js
@@ -125,2 +125,3 @@ 'use strict'; | ||
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; | ||
@@ -193,5 +194,17 @@ // Set *after* `this.stream` | ||
getFullPrefixText(prefixText = this[PREFIX_TEXT], postfix = ' ') { | ||
if (typeof prefixText === 'string') { | ||
return prefixText + postfix; | ||
} | ||
if (typeof prefixText === 'function') { | ||
return prefixText() + postfix; | ||
} | ||
return ''; | ||
} | ||
updateLineCount() { | ||
const columns = this.stream.columns || 80; | ||
const fullPrefixText = (typeof this[PREFIX_TEXT] === 'string') ? this[PREFIX_TEXT] + '-' : ''; | ||
const fullPrefixText = this.getFullPrefixText(this.prefixText, '-'); | ||
this.lineCount = stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n').reduce((count, line) => { | ||
@@ -212,2 +225,26 @@ return count + Math.max(1, Math.ceil(wcwidth(line) / columns)); | ||
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() { | ||
@@ -248,2 +285,6 @@ const {frames} = this.spinner; | ||
render() { | ||
if (this.isSilent) { | ||
return this; | ||
} | ||
this.clear(); | ||
@@ -261,2 +302,6 @@ this.stream.write(this.frame()); | ||
if (this.isSilent) { | ||
return this; | ||
} | ||
if (!this.isEnabled) { | ||
@@ -327,4 +372,7 @@ if (this.text) { | ||
stopAndPersist(options = {}) { | ||
if (this.isSilent) { | ||
return this; | ||
} | ||
const prefixText = options.prefixText || this.prefixText; | ||
const fullPrefixText = (typeof prefixText === 'string' && prefixText !== '') ? prefixText + ' ' : ''; | ||
const text = options.text || this.text; | ||
@@ -334,3 +382,3 @@ const fullText = (typeof text === 'string') ? ' ' + text : ''; | ||
this.stop(); | ||
this.stream.write(`${fullPrefixText}${options.symbol || ' '}${fullText}\n`); | ||
this.stream.write(`${this.getFullPrefixText(prefixText, ' ')}${options.symbol || ' '}${fullText}\n`); | ||
@@ -337,0 +385,0 @@ return this; |
{ | ||
"name": "ora", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Elegant terminal spinner", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -49,5 +49,5 @@ # ora [![Build Status](https://travis-ci.com/sindresorhus/ora.svg?branch=master)](https://travis-ci.com/github/sindresorhus/ora) | ||
Type: `string` | ||
Type: `string | () => string` | ||
Text to display before the spinner. No prefix text will be displayed if set to an empty string. | ||
Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string. | ||
@@ -118,2 +118,9 @@ ##### spinner | ||
##### isSilent | ||
Type: `boolean`\ | ||
Default: `false` | ||
Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`. | ||
##### discardStdin | ||
@@ -120,0 +127,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23193
530
264