Skip to content

Commit 3467596

Browse files
committed
init
0 parents  commit 3467596

12 files changed

+337
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,*.yml}]
11+
indent_style = space
12+
indent_size = 2

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- '5'
4+
- '4'
5+
- '0.12'
6+
- '0.10'

example.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
var Ora = require('./');
3+
4+
var spinner = new Ora({
5+
text: 'Loading unicorns',
6+
spinner: process.argv[2]
7+
});
8+
9+
spinner.start();
10+
11+
setTimeout(() => {
12+
spinner.color = 'yellow';
13+
spinner.text = 'Loading rainbows';
14+
}, 1000);
15+
16+
// $ node example.js nameOfSpinner

index.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
var chalk = require('chalk');
3+
var cliCursor = require('cli-cursor');
4+
var cliSpinners = require('cli-spinners');
5+
var objectAssign = require('object-assign');
6+
7+
function Ora(options) {
8+
this.options = objectAssign({
9+
text: '',
10+
color: 'cyan',
11+
stream: process.stderr
12+
}, options);
13+
14+
var sp = options.spinner;
15+
this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line
16+
17+
if (this.spinner.frames === undefined) {
18+
throw new Error('Spinner must define `frames`');
19+
}
20+
21+
this.text = this.options.text;
22+
this.color = this.options.color;
23+
this.interval = this.options.interval || this.spinner.interval || 100;
24+
this.stream = this.options.stream;
25+
this.id = null;
26+
this.frameIndex = 0;
27+
this.enabled = (this.stream && this.stream.isTTY) && !process.env.CI;
28+
}
29+
30+
Ora.prototype.frame = function () {
31+
var frames = this.spinner.frames;
32+
var frame = frames[this.frameIndex];
33+
34+
if (this.color) {
35+
frame = chalk[this.color](frame);
36+
}
37+
38+
this.frameIndex = ++this.frameIndex % frames.length;
39+
40+
return frame + ' ' + this.text;
41+
};
42+
43+
Ora.prototype.clear = function () {
44+
if (!this.enabled) {
45+
return;
46+
}
47+
48+
this.stream.clearLine();
49+
this.stream.cursorTo(0);
50+
};
51+
52+
Ora.prototype.render = function () {
53+
this.clear();
54+
this.stream.write(this.frame());
55+
};
56+
57+
Ora.prototype.start = function () {
58+
if (!this.enabled) {
59+
return;
60+
}
61+
62+
cliCursor.hide();
63+
this.render();
64+
this.id = setInterval(this.render.bind(this), this.interval);
65+
};
66+
67+
Ora.prototype.stop = function () {
68+
if (!this.enabled) {
69+
return;
70+
}
71+
72+
clearInterval(this.id);
73+
this.id = null;
74+
this.frameIndex = 0;
75+
this.clear();
76+
cliCursor.show();
77+
};
78+
79+
module.exports = Ora;

license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "ora",
3+
"version": "0.0.0",
4+
"description": "Elegant terminal spinner",
5+
"license": "MIT",
6+
"repository": "sindresorhus/ora",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "[email protected]",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=0.10.0"
14+
},
15+
"scripts": {
16+
"test": "xo && ava"
17+
},
18+
"files": [
19+
"index.js"
20+
],
21+
"keywords": [
22+
"cli",
23+
"spinner",
24+
"spinners",
25+
"terminal",
26+
"term",
27+
"console",
28+
"ascii",
29+
"unicode",
30+
"loading",
31+
"indicator",
32+
"progress",
33+
"busy",
34+
"wait",
35+
"idle"
36+
],
37+
"dependencies": {
38+
"chalk": "^1.1.1",
39+
"cli-cursor": "^1.0.2",
40+
"cli-spinners": "^0.1.2",
41+
"object-assign": "^4.0.1"
42+
},
43+
"devDependencies": {
44+
"ava": "*",
45+
"hook-std": "^0.2.0",
46+
"xo": "*"
47+
}
48+
}

readme.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# ora [![Build Status](https://travis-ci.org/sindresorhus/ora.svg?branch=master)](https://travis-ci.org/sindresorhus/ora)
2+
3+
> Elegant terminal spinner
4+
5+
<img src="screenshot.gif" width="629">
6+
7+
8+
## Install
9+
10+
```
11+
$ npm install --save ora
12+
```
13+
14+
15+
## Usage
16+
17+
```js
18+
const Ora = require('ora');
19+
20+
const spinner = new Ora({text: 'Loading unicorns'});
21+
22+
spinner.start();
23+
24+
setTimeout(() => {
25+
spinner.color = 'yellow';
26+
spinner.text = 'Loading rainbows';
27+
}, 1000);
28+
```
29+
30+
31+
## API
32+
33+
It will gracefully not do anything when there's no TTY or when in a CI.
34+
35+
### Ora([options])
36+
37+
#### options
38+
39+
##### text
40+
41+
Type: `string`
42+
43+
Text to display after the spinner.
44+
45+
##### spinner
46+
47+
Type: `string` `object`<br>
48+
Default: `dots` <img src="screenshot-spinner.gif" width="14">
49+
50+
Name of one of the [provided spinners](https://github.com/sindresorhus/cli-spinners/blob/master/spinners.json). See `example.js` in this repo if you want to test out different spinners.
51+
52+
Or an object like:
53+
54+
```js
55+
{
56+
interval: 80, // optional
57+
frames: ['-', '+', '-']
58+
}
59+
```
60+
61+
##### color
62+
63+
Type: `string`<br>
64+
Default: `cyan`<br>
65+
Values: `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `gray`
66+
67+
Color of the spinner.
68+
69+
##### interval
70+
71+
Type: `number`<br>
72+
Default: Provided by the spinner or `100`
73+
74+
Interval between each frame.
75+
76+
Spinners provide their own recommended interval, so you don't really need to specify this.
77+
78+
##### stream
79+
80+
Type: `WritableStream`<br>
81+
Default: `process.stderr`
82+
83+
Stream to write the output.
84+
85+
You could for example set this to `process.stdout` instead.
86+
87+
### Instance
88+
89+
#### .start()
90+
91+
Start the spinner.
92+
93+
#### .stop()
94+
95+
Stop and clear the spinner.
96+
97+
#### .clear()
98+
99+
Clear the spinner.
100+
101+
#### .frame()
102+
103+
Get a new frame.
104+
105+
#### .render()
106+
107+
Manually render a new frame.
108+
109+
#### .text
110+
111+
Change the text.
112+
113+
#### .color
114+
115+
Change the spinner color.
116+
117+
118+
## Related
119+
120+
- [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal
121+
122+
123+
## License
124+
125+
MIT © [Sindre Sorhus](https://sindresorhus.com)

screenshot-spinner.gif

5.46 KB
Loading

screenshot.gif

184 KB
Loading

test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import test from 'ava';
2+
import hookStd from 'hook-std';
3+
import Ora from './';
4+
5+
test(t => {
6+
// TODO: use the the `stream` option instead of hooking stderr
7+
8+
process.stderr.isTTY = true;
9+
process.stderr.clearLine = function () {};
10+
process.stderr.cursorTo = function () {};
11+
12+
const spinner = new Ora({text: 'foo', color: false});
13+
14+
spinner.enabled = true;
15+
16+
let out = '';
17+
18+
const unhook = hookStd.stderr({silent: true}, output => {
19+
out += output;
20+
});
21+
22+
spinner.start();
23+
24+
t.is(out, '⠋ foo');
25+
26+
spinner.stop();
27+
unhook();
28+
});

0 commit comments

Comments
 (0)