From 4f8aa8fe9793d1c94c6da0c575ebeb2923ed6bf5 Mon Sep 17 00:00:00 2001 From: lyz122260 Date: Wed, 26 Aug 2020 18:55:35 +0800 Subject: [PATCH] add prefixTextGenerator for setting the prefixText dynamically --- index.d.ts | 8 +++++--- index.js | 17 ++++++++++++++--- index.test-d.ts | 1 + readme.md | 4 ++-- test.js | 4 ++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index 392deb2..731faac 100644 --- a/index.d.ts +++ b/index.d.ts @@ -18,6 +18,8 @@ declare namespace ora { | 'white' | 'gray'; + type PrefixTextGenerator = () => string; + interface Options { /** Text to display after the spinner. @@ -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. @@ -122,7 +124,7 @@ declare namespace ora { Default: Current `prefixText`. */ - readonly prefixText?: string; + readonly prefixText?: string | PrefixTextGenerator; } interface Ora { @@ -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. diff --git a/index.js b/index.js index c18218a..63c5d34 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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; } diff --git a/index.test-d.ts b/index.test-d.ts index 3ff2f99..3f3c4dd 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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: ['-', '+', '-']}}); diff --git a/readme.md b/readme.md index d2f0447..abc633b 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index bdb61d9..aa60734 100644 --- a/test.js +++ b/test.js @@ -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'});