Skip to content

Commit 561bc85

Browse files
authoredSep 6, 2020
Allow setting prefixText dynamically (#154)
1 parent 3dc7379 commit 561bc85

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed
 

‎index.d.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ declare namespace ora {
1818
| 'white'
1919
| 'gray';
2020

21+
type PrefixTextGenerator = () => string;
22+
2123
interface Options {
2224
/**
2325
Text to display after the spinner.
2426
*/
2527
readonly text?: string;
2628

2729
/**
28-
Text to display before the spinner. No prefix text will be displayed if set to an empty string.
30+
Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string.
2931
*/
30-
readonly prefixText?: string;
32+
readonly prefixText?: string | PrefixTextGenerator;
3133

3234
/**
3335
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.
@@ -118,11 +120,11 @@ declare namespace ora {
118120
readonly text?: string;
119121

120122
/**
121-
Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
123+
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.
122124
123125
Default: Current `prefixText`.
124126
*/
125-
readonly prefixText?: string;
127+
readonly prefixText?: string | PrefixTextGenerator;
126128
}
127129

128130
interface Ora {
@@ -137,9 +139,9 @@ declare namespace ora {
137139
text: string;
138140

139141
/**
140-
Change the text before the spinner. No prefix text will be displayed if set to an empty string.
142+
Change the text or function that returns text before the spinner. No prefix text will be displayed if set to an empty string.
141143
*/
142-
prefixText: string;
144+
prefixText: string | PrefixTextGenerator;
143145

144146
/**
145147
Change the spinner color.

‎index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,21 @@ class Ora {
189189
return this.id !== undefined;
190190
}
191191

192+
getFullPrefixText(prefixText = this[PREFIX_TEXT], postfix = ' ') {
193+
if (typeof prefixText === 'string') {
194+
return prefixText + postfix;
195+
}
196+
197+
if (typeof prefixText === 'function') {
198+
return prefixText() + postfix;
199+
}
200+
201+
return '';
202+
}
203+
192204
updateLineCount() {
193205
const columns = this.stream.columns || 80;
194-
const fullPrefixText = (typeof this[PREFIX_TEXT] === 'string') ? this[PREFIX_TEXT] + '-' : '';
206+
const fullPrefixText = this.getFullPrefixText(this.prefixText, '-');
195207
this.lineCount = stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n').reduce((count, line) => {
196208
return count + Math.max(1, Math.ceil(wcwidth(line) / columns));
197209
}, 0);
@@ -320,12 +332,11 @@ class Ora {
320332

321333
stopAndPersist(options = {}) {
322334
const prefixText = options.prefixText || this.prefixText;
323-
const fullPrefixText = (typeof prefixText === 'string' && prefixText !== '') ? prefixText + ' ' : '';
324335
const text = options.text || this.text;
325336
const fullText = (typeof text === 'string') ? ' ' + text : '';
326337

327338
this.stop();
328-
this.stream.write(`${fullPrefixText}${options.symbol || ' '}${fullText}\n`);
339+
this.stream.write(`${this.getFullPrefixText(prefixText, ' ')}${options.symbol || ' '}${fullText}\n`);
329340

330341
return this;
331342
}

‎index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {promise} from '.';
66
const spinner = ora('Loading unicorns');
77
ora({text: 'Loading unicorns'});
88
ora({prefixText: 'Loading unicorns'});
9+
ora({prefixText: () => 'Loading unicorns dynamically'});
910
ora({spinner: 'squish'});
1011
ora({spinner: {frames: ['-', '+', '-']}});
1112
ora({spinner: {interval: 80, frames: ['-', '+', '-']}});

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ Text to display after the spinner.
4646

4747
##### prefixText
4848

49-
Type: `string`
49+
Type: `string | () => string`
5050

51-
Text to display before the spinner. No prefix text will be displayed if set to an empty string.
51+
Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string.
5252

5353
##### spinner
5454

‎test.js

+4
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,7 @@ test('.stopAndPersist() with manual prefixText', macro, spinner => {
357357
test('.stopAndPersist() with manual empty prefixText', macro, spinner => {
358358
spinner.stopAndPersist({symbol: '@', prefixText: '', text: 'foo'});
359359
}, /@ foo\n$/, {prefixText: 'bar'});
360+
361+
test('.stopAndPersist() with dynamic prefixText', macro, spinner => {
362+
spinner.stopAndPersist({symbol: '&', prefixText: () => 'babeee', text: 'yorkie'});
363+
}, /babeee & yorkie\n$/, {prefixText: () => 'babeee'});

0 commit comments

Comments
 (0)
Please sign in to comment.