UNPKG

6.25 kBMarkdownView Raw
1# ora [![Build Status](https://travis-ci.com/sindresorhus/ora.svg?branch=master)](https://travis-ci.com/sindresorhus/ora)
2
3> Elegant terminal spinner
4
5<p align="center">
6 <br>
7 <img src="screenshot.svg" width="500">
8 <br>
9</p>
10
11## Install
12
13```
14$ npm install ora
15```
16
17## Usage
18
19```js
20const ora = require('ora');
21
22const spinner = ora('Loading unicorns').start();
23
24setTimeout(() => {
25 spinner.color = 'yellow';
26 spinner.text = 'Loading rainbows';
27}, 1000);
28```
29
30## API
31
32### ora(text)
33### ora(options)
34
35If a string is provided, it is treated as a shortcut for [`options.text`](#text).
36
37#### options
38
39Type: `object`
40
41##### text
42
43Type: `string`
44
45Text to display after the spinner.
46
47##### prefixText
48
49Type: `string`
50
51Text to display before the spinner. No prefix text will be displayed if set to an empty string.
52
53##### spinner
54
55Type: `string | object`\
56Default: `'dots'` <img src="screenshot-spinner.gif" width="14">
57
58Name 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. On Windows, it will always use the `line` spinner as the Windows command-line doesn't have proper Unicode support.
59
60Or an object like:
61
62```js
63{
64 interval: 80, // Optional
65 frames: ['-', '+', '-']
66}
67```
68
69##### color
70
71Type: `string`\
72Default: `'cyan'`\
73Values: `'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray'`
74
75Color of the spinner.
76
77##### hideCursor
78
79Type: `boolean`\
80Default: `true`
81
82Set to `false` to stop Ora from hiding the cursor.
83
84##### indent
85
86Type: `number`\
87Default: `0`
88
89Indent the spinner with the given number of spaces.
90
91##### interval
92
93Type: `number`\
94Default: Provided by the spinner or `100`
95
96Interval between each frame.
97
98Spinners provide their own recommended interval, so you don't really need to specify this.
99
100##### stream
101
102Type: `stream.Writable`\
103Default: `process.stderr`
104
105Stream to write the output.
106
107You could for example set this to `process.stdout` instead.
108
109##### isEnabled
110
111Type: `boolean`
112
113Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment.
114
115Note that `{isEnabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text.
116
117##### discardStdin
118
119Type: `boolean`\
120Default: `true`
121
122Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on <kbd>Enter</kbd> key presses, and prevents buffering of input while the spinner is running.
123
124This has no effect on Windows as there's no good way to implement discarding stdin properly there.
125
126### Instance
127
128#### .start(text?)
129
130Start the spinner. Returns the instance. Set the current text if `text` is provided.
131
132#### .stop()
133
134Stop and clear the spinner. Returns the instance.
135
136#### .succeed(text?)
137
138Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
139
140#### .fail(text?)
141
142Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
143
144#### .warn(text?)
145
146Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
147
148#### .info(text?)
149
150Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
151
152#### .isSpinning
153
154A boolean of whether the instance is currently spinning.
155
156#### .stopAndPersist(options?)
157
158Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
159
160##### options
161
162Type: `object`
163
164###### symbol
165
166Type: `string`\
167Default: `' '`
168
169Symbol to replace the spinner with.
170
171###### text
172
173Type: `string`\
174Default: Current `'text'`
175
176Text to be persisted after the symbol
177
178###### prefixText
179
180Type: `string`\
181Default: Current `prefixText`
182
183Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
184
185<img src="screenshot-2.gif" width="480">
186
187#### .clear()
188
189Clear the spinner. Returns the instance.
190
191#### .render()
192
193Manually render a new frame. Returns the instance.
194
195#### .frame()
196
197Get a new frame.
198
199#### .text
200
201Change the text after the spinner.
202
203#### .prefixText
204
205Change the text before the spinner. No prefix text will be displayed if set to an empty string.
206
207#### .color
208
209Change the spinner color.
210
211#### .spinner
212
213Change the spinner.
214
215#### .indent
216
217Change the spinner indent.
218
219### ora.promise(action, text)
220### ora.promise(action, options)
221
222Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the spinner instance.
223
224#### action
225
226Type: `Promise`
227
228## FAQ
229
230### How do I change the color of the text?
231
232Use [Chalk](https://github.com/chalk/chalk):
233
234```js
235const ora = require('ora');
236const chalk = require('chalk');
237
238const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
239```
240
241### Why does the spinner freeze?
242
243JavaScript is single-threaded, so synchronous operations blocks the thread, including the spinner animation. Prefer asynchronous operations whenever possible.
244
245## Related
246
247- [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal
248- [listr](https://github.com/SamVerschueren/listr) - Terminal task list
249- [CLISpinner](https://github.com/kiliankoe/CLISpinner) - Terminal spinner library for Swift
250- [halo](https://github.com/ManrajGrover/halo) - Python port
251- [spinners](https://github.com/FGRibreau/spinners) - Terminal spinners for Rust
252- [marquee-ora](https://github.com/joeycozza/marquee-ora) - Scrolling marquee spinner for Ora
253- [briandowns/spinner](https://github.com/briandowns/spinner) - Terminal spinner/progress indicator for Go
254- [tj/go-spin](https://github.com/tj/go-spin) - Terminal spinner package for Go
255- [observablehq.com/@victordidenko/ora](https://observablehq.com/@victordidenko/ora) - Ora port to Observable notebooks
256- [spinnies](https://github.com/jcarpanelli/spinnies) - Terminal multi-spinner library for Node.js