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 1 commit
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
8 changes: 5 additions & 3 deletions index.d.ts
Expand Up @@ -18,6 +18,8 @@ declare namespace ora {
| 'white'
| 'gray';

type PrefixTextGenerator = () => string;

interface Options {
/**
Text to display after the spinner.
Expand All @@ -27,7 +29,7 @@ declare namespace ora {
/**
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 @@ -122,7 +124,7 @@ declare namespace ora {

Default: Current `prefixText`.
*/
readonly prefixText?: string;
readonly prefixText?: string | PrefixTextGenerator;
}

interface Ora {
Expand All @@ -139,7 +141,7 @@ declare namespace ora {
/**
Change the 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 text generator to display before the spinner. No prefix text will be displayed if set to an empty string.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

##### 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'});