Skip to content

Commit

Permalink
add prefixTextGenerator for setting the prefixText dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkie committed Aug 26, 2020
1 parent 3dc7379 commit 28d65f3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -50,7 +50,7 @@
"@types/node": "^14.0.27",
"ava": "^2.4.0",
"get-stream": "^5.1.0",
"tsd": "^0.13.1",
"tsd": "^0.13.0",
"xo": "^0.25.0"
}
}
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.

##### 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 28d65f3

Please sign in to comment.