Skip to content

Commit f96b0e1

Browse files
matheusssindresorhus
authored andcommitted
Add succeed βœ”, fail βœ– and stopAndPersist πŸ¦„ (#20)
1 parent 86f747e commit f96b0e1

File tree

6 files changed

+110
-1
lines changed

6 files changed

+110
-1
lines changed

β€Žexample.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ setTimeout(() => {
1313
spinner.text = 'Loading rainbows';
1414
}, 1000);
1515

16+
setTimeout(() => {
17+
spinner.succeed();
18+
}, 2000);
19+
1620
// $ node example.js nameOfSpinner

β€Žindex.js

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const chalk = require('chalk');
33
const cliCursor = require('cli-cursor');
44
const cliSpinners = require('cli-spinners');
5+
const logSymbols = require('log-symbols');
56

67
class Ora {
78
constructor(options) {
@@ -82,6 +83,18 @@ class Ora {
8283
this.clear();
8384
cliCursor.show();
8485

86+
return this;
87+
}
88+
succeed() {
89+
return this.stopAndPersist(logSymbols.success);
90+
}
91+
fail() {
92+
return this.stopAndPersist(logSymbols.error);
93+
}
94+
stopAndPersist(symbol) {
95+
this.stop();
96+
this.stream.write(`${symbol || ' '} ${this.text}\n`);
97+
8598
return this;
8699
}
87100
}

β€Žpackage.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"dependencies": {
3838
"chalk": "^1.1.1",
3939
"cli-cursor": "^1.0.2",
40-
"cli-spinners": "^0.1.2"
40+
"cli-spinners": "^0.1.2",
41+
"log-symbols": "^1.0.2"
4142
},
4243
"devDependencies": {
4344
"ava": "*",

β€Žreadme.md

+14
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ Start the spinner. Returns the instance.
103103

104104
Stop and clear the spinner. Returns the instance.
105105

106+
### .succeed()
107+
108+
Stop the spinner, change it to a green `βœ”` and persist the `text`. Returns the instance. See the below GIF below.
109+
110+
### .fail()
111+
112+
Stop the spinner, change it to a red `βœ–` and persist the `text`. Returns the instance. See the below GIF below.
113+
114+
### .stopAndPersist([symbol])
115+
116+
Stop the spinner, change it to `symbol` (or `' '` if `symbol` is not provided) and persist the `text`. Returns the instance. See the below GIF below.
117+
118+
<img src="screenshot-2.gif" width="480">
119+
106120
#### .clear()
107121

108122
Clear the spinner. Returns the instance.

β€Žscreenshot-2.gif

386 KB
Loading

β€Žtest.js

+77
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {PassThrough as PassThroughStream} from 'stream';
22
import getStream from 'get-stream';
33
import test from 'ava';
4+
import {stripColor} from 'chalk';
45
import Ora from './';
56

67
const spinnerChar = process.platform === 'win32' ? '-' : 'β ‹';
@@ -78,3 +79,79 @@ test('chain call to `.start()` with constructor', t => {
7879
t.truthy(spinner.id);
7980
t.true(spinner.enabled);
8081
});
82+
83+
test('succeed', async t => {
84+
const stream = getPassThroughStream();
85+
86+
const spinner = new Ora({
87+
stream,
88+
text: 'foo',
89+
color: false,
90+
enabled: true
91+
});
92+
93+
spinner.start();
94+
spinner.succeed();
95+
96+
stream.end();
97+
const output = await getStream(stream);
98+
99+
t.regex(stripColor(output), /βœ”|√ foo/);
100+
});
101+
102+
test('fail', async t => {
103+
const stream = getPassThroughStream();
104+
105+
const spinner = new Ora({
106+
stream,
107+
text: 'foo',
108+
color: false,
109+
enabled: true
110+
});
111+
112+
spinner.start();
113+
spinner.fail();
114+
115+
stream.end();
116+
const output = await getStream(stream);
117+
118+
t.regex(stripColor(output), /βœ–|Γ— foo/);
119+
});
120+
121+
test('stopAndPersist', async t => {
122+
const stream = getPassThroughStream();
123+
124+
const spinner = new Ora({
125+
stream,
126+
text: 'foo',
127+
color: false,
128+
enabled: true
129+
});
130+
131+
spinner.start();
132+
spinner.stopAndPersist('@');
133+
134+
stream.end();
135+
const output = await getStream(stream);
136+
137+
t.regex(output, /@ foo/);
138+
});
139+
140+
test('stopAndPersist with no argument', async t => {
141+
const stream = getPassThroughStream();
142+
143+
const spinner = new Ora({
144+
stream,
145+
text: 'foo',
146+
color: false,
147+
enabled: true
148+
});
149+
150+
spinner.start();
151+
spinner.stopAndPersist(' ');
152+
153+
stream.end();
154+
const output = await getStream(stream);
155+
156+
t.regex(output, /\s foo/);
157+
});

0 commit comments

Comments
Β (0)