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

Add prefixText option #104

Merged
merged 6 commits into from
Apr 2, 2019
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
19 changes: 17 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type Options = Readonly<{
*/
text?: string;

/**
* Text to display before the spinner.
*/
prefixText?: string;

/**
* 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 @@ -114,9 +119,14 @@ export type PersistOptions = Readonly<{
symbol?: string;

/**
* Text to be persisted. Default: Current text.
* Text to be persisted after the symbol. Default: Current `text`.
*/
text?: string;

/**
* Text to be persisted before the symbol. Default: Current `prefixText`.
*/
prefixText?: string;
}>;

export interface Ora {
Expand All @@ -126,9 +136,14 @@ export interface Ora {
readonly isSpinning: boolean;

/**
* Change the text.
* Change the text after the spinner.
*/
text: string;

/**
* Change the text before the spinner.
*/
prefixText: string;

/**
* Change the spinner color.
Expand Down
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const stripAnsi = require('strip-ansi');
const wcwidth = require('wcwidth');

const TEXT = Symbol('text');
const PREFIX_TEXT = Symbol('prefixText');

class Ora {
constructor(options) {
Expand All @@ -33,6 +34,7 @@ class Ora {

// Set *after* `this.stream`
this.text = this.options.text;
this.prefixText = this.options.prefixText;
this.linesToClear = 0;
this.indent = this.options.indent;
}
Expand Down Expand Up @@ -78,18 +80,32 @@ class Ora {
return this[TEXT];
}

get prefixText() {
return this[PREFIX_TEXT];
}

get isSpinning() {
return this.id !== null;
}

set text(value) {
this[TEXT] = value;
updateLineCount() {
const columns = this.stream.columns || 80;
this.lineCount = stripAnsi('--' + value).split('\n').reduce((count, line) => {
const fullPrefixText = (typeof this[PREFIX_TEXT] === 'string') ? this[PREFIX_TEXT] + '-' : '';
this.lineCount = stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n').reduce((count, line) => {
return count + Math.max(1, Math.ceil(wcwidth(line) / columns));
}, 0);
}

set text(value) {
this[TEXT] = value;
this.updateLineCount();
}

set prefixText(value) {
this[PREFIX_TEXT] = value;
this.updateLineCount();
}

frame() {
const {frames} = this.spinner;
let frame = frames[this.frameIndex];
Expand All @@ -99,8 +115,10 @@ class Ora {
}

this.frameIndex = ++this.frameIndex % frames.length;
const fullPrefixText = typeof this.prefixText === 'string' ? this.prefixText + ' ' : '';
const fullText = typeof this.text === 'string' ? ' ' + this.text : '';

return frame + ' ' + this.text;
return fullPrefixText + frame + fullText;
}

clear() {
Expand Down Expand Up @@ -187,8 +205,13 @@ class Ora {
}

stopAndPersist(options = {}) {
const prefixText = options.prefixText || this.prefixText;
const fullPrefixText = (typeof prefixText === 'string') ? prefixText + ' ' : '';
const text = options.text || this.text;
const fullText = (typeof text === 'string') ? ' ' + text : '';

this.stop();
this.stream.write(`${options.symbol || ' '} ${options.text || this.text}\n`);
this.stream.write(`${fullPrefixText}${options.symbol || ' '}${fullText}n`);

return this;
}
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ora, {promise} from '.';

const spinner = ora('Loading unicorns');
ora({text: 'Loading unicorns'});
ora({prefixText: 'Loading unicorns'});
ora({spinner: 'squish'});
ora({spinner: {frames: ['-', '+', '-']}});
ora({spinner: {interval: 80, frames: ['-', '+', '-']}});
Expand Down Expand Up @@ -34,6 +35,7 @@ spinner.info('info foo');
spinner.stopAndPersist();
spinner.stopAndPersist({text: 'all done'});
spinner.stopAndPersist({symbol: '@', text: 'all done'});
spinner.stopAndPersist({prefixText: 'all done'});
spinner.clear();
spinner.render();
spinner.frame();
Expand Down
23 changes: 20 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Type: `string`

Text to display after the spinner.

##### prefixText

Type: `string`

Text to display before the spinner.

##### spinner

Type: `string` `Object`<br>
Expand Down Expand Up @@ -162,9 +168,16 @@ Symbol to replace the spinner with.
###### text

Type: `string`<br>
Default: Current text
Default: Current `text`

Text to be persisted after the symbol

###### prefixText

Type: `string`<br>
Default: Current `prefixText`

Text to be persisted.
Text to be persisted before the symbol.

<img src="screenshot-2.gif" width="480">

Expand All @@ -182,7 +195,11 @@ Get a new frame.

#### .text

Change the text.
Change the text after the spinner.

#### .prefixText

Change the text before the spinner.

#### .color

Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ test('erases wrapped lines', t => {
t.is(clearedLines, 3);
t.is(cursorAtRow, -2);

spinner.clear();
reset();
spinner.prefixText = 'foo\n';
spinner.text = '\nbar';
spinner.render();
spinner.render();
t.is(clearedLines, 3); // Cleared 'foo\n\nbar'
t.is(cursorAtRow, -2);

spinner.stop();
});

Expand Down Expand Up @@ -304,3 +313,11 @@ test('indent option throws', t => {
spinner.indent = -1;
}, 'The `indent` option must be an integer from 0 and up');
});

test('.stopAndPersist() with prefixText', macro, spinner => {
spinner.stopAndPersist({symbol: '@', text: 'foo'});
}, /bar @ foo/, {prefixText: 'bar'});

test('.stopAndPersist() with manual prefixText', macro, spinner => {
spinner.stopAndPersist({symbol: '@', prefixText: 'baz', text: 'foo'});
}, /baz @ foo/, {prefixText: 'bar'});