Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting prefixText dynamically #154

Merged
merged 3 commits into from Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'});