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 ability to change text when stopping and persisting #35

Merged
merged 3 commits into from
Jan 20, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,25 @@ class Ora {

return this;
}
succeed() {
return this.stopAndPersist(logSymbols.success);
succeed(text) {
return this.stopAndPersist({symbol: logSymbols.success, text});
}
fail() {
return this.stopAndPersist(logSymbols.error);
fail(text) {
return this.stopAndPersist({symbol: logSymbols.error, text});
}
stopAndPersist(symbol) {
stopAndPersist(options) {
// Legacy argument
// TODO: Deprecate sometime in the future
if (typeof options === 'string') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment above here with the following:

// Legacy argument
// TODO: Deprecate sometime in the future

options = {
symbol: options
};
}

options = options || {};

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

return this;
}
Expand Down
30 changes: 24 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,35 @@ Start the spinner. Returns the instance.

Stop and clear the spinner. Returns the instance.

### .succeed()
#### .succeed([text])

Stop the spinner, change it to a green `✔` and persist the `text`. Returns the instance. See the GIF below.
Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.

### .fail()
#### .fail([text])

Stop the spinner, change it to a red `✖` and persist the `text`. Returns the instance. See the GIF below.
Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.

### .stopAndPersist([symbol])
#### .stopAndPersist([options])

Stop the spinner, change it to `symbol` (or `' '` if `symbol` is not provided) and persist the `text`. Returns the instance. See the GIF below.
Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.

##### options

Type: `Object`

###### symbol

Type: `string`<br>
Default: `' '`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the default is an empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The effective default is options.symbol || ' ', or should we just say something like 'Clear the symbol'?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, it's good.


Symbol to replace the spinner with.

###### text

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

Text to be persisted.

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

Expand Down
76 changes: 76 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ test('succeed', async t => {
t.regex(stripColor(output), /✔|√ foo/);
});

test('succeed with new text', async t => {
const stream = getPassThroughStream();

const spinner = new Ora({
stream,
text: 'foo',
color: false,
enabled: true
});

spinner.start();
spinner.succeed('fooed');

stream.end();
const output = await getStream(stream);

t.regex(stripColor(output), /✔|√ fooed/);
});

test('fail', async t => {
const stream = getPassThroughStream();

Expand All @@ -117,6 +136,25 @@ test('fail', async t => {
t.regex(stripColor(output), /✖|× foo/);
});

test('fail with new text', async t => {
const stream = getPassThroughStream();

const spinner = new Ora({
stream,
text: 'foo',
color: false,
enabled: true
});

spinner.start();
spinner.fail('failed to foo');

stream.end();
const output = await getStream(stream);

t.regex(stripColor(output), /✖|× failed to foo/);
});

test('stopAndPersist', async t => {
const stream = getPassThroughStream();

Expand Down Expand Up @@ -155,6 +193,44 @@ test('stopAndPersist with no argument', async t => {
t.regex(output, /\s foo/);
});

test('stopAndPersist with new text', async t => {
const stream = getPassThroughStream();

const spinner = new Ora({
stream,
text: 'foo',
color: false,
enabled: true
});

spinner.start();
spinner.stopAndPersist({text: 'all done'});

stream.end();
const output = await getStream(stream);

t.regex(output, /\s all done/);
});

test('stopAndPersist with new symbol and text', async t => {
const stream = getPassThroughStream();

const spinner = new Ora({
stream,
text: 'foo',
color: false,
enabled: true
});

spinner.start();
spinner.stopAndPersist({symbol: '@', text: 'all done'});

stream.end();
const output = await getStream(stream);

t.regex(output, /@ all done/);
});

test('promise resolves', async t => {
const stream = getPassThroughStream();
const resolves = Promise.resolve(1);
Expand Down