Skip to content

Commit

Permalink
Allow setting prefixText dynamically (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkie committed Sep 6, 2020
1 parent 3dc7379 commit 561bc85
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
14 changes: 8 additions & 6 deletions index.d.ts
Expand Up @@ -18,16 +18,18 @@ declare namespace ora {
| 'white'
| 'gray';

type PrefixTextGenerator = () => string;

interface Options {
/**
Text to display after the spinner.
*/
readonly text?: 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.
*/
readonly prefixText?: string;
readonly prefixText?: string | PrefixTextGenerator;

/**
Name of one of the provided spinners. See [`example.js`](https://github.com/BendingBender/ora/blob/master/example.js) in this repo if you want to test out different spinners. On Windows, it will always use the line spinner as the Windows command-line doesn't have proper Unicode support.
Expand Down Expand Up @@ -118,11 +120,11 @@ declare namespace ora {
readonly text?: string;

/**
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;
}

interface Ora {
Expand All @@ -137,9 +139,9 @@ declare namespace ora {
text: string;

/**
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;

/**
Change the spinner color.
Expand Down
17 changes: 14 additions & 3 deletions index.js
Expand Up @@ -189,9 +189,21 @@ class Ora {
return this.id !== undefined;
}

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) => {
return count + Math.max(1, Math.ceil(wcwidth(line) / columns));
}, 0);
Expand Down Expand Up @@ -320,12 +332,11 @@ class Ora {

stopAndPersist(options = {}) {
const prefixText = options.prefixText || this.prefixText;
const fullPrefixText = (typeof prefixText === 'string' && prefixText !== '') ? prefixText + ' ' : '';
const text = options.text || this.text;
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`);

return this;
}
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Expand Up @@ -6,6 +6,7 @@ import {promise} from '.';
const spinner = ora('Loading unicorns');
ora({text: 'Loading unicorns'});
ora({prefixText: 'Loading unicorns'});
ora({prefixText: () => 'Loading unicorns dynamically'});
ora({spinner: 'squish'});
ora({spinner: {frames: ['-', '+', '-']}});
ora({spinner: {interval: 80, frames: ['-', '+', '-']}});
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -46,9 +46,9 @@ Text to display after the spinner.

##### prefixText

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.

##### spinner

Expand Down
4 changes: 4 additions & 0 deletions test.js
Expand Up @@ -357,3 +357,7 @@ test('.stopAndPersist() with manual prefixText', macro, spinner => {
test('.stopAndPersist() with manual empty prefixText', macro, spinner => {
spinner.stopAndPersist({symbol: '@', prefixText: '', text: 'foo'});
}, /@ foo\n$/, {prefixText: 'bar'});

test('.stopAndPersist() with dynamic prefixText', macro, spinner => {
spinner.stopAndPersist({symbol: '&', prefixText: () => 'babeee', text: 'yorkie'});
}, /babeee & yorkie\n$/, {prefixText: () => 'babeee'});

0 comments on commit 561bc85

Please sign in to comment.