UNPKG

7.19 kBMarkdownView Raw
1# 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```sh
14npm install ora
15```
16
17## Usage
18
19```js
20import ora from '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 | () => string`
50
51Text or a function that returns text 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/main/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
75The color 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##### isSilent
118
119Type: `boolean`\
120Default: `false`
121
122Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
123
124##### discardStdin
125
126Type: `boolean`\
127Default: `true`
128
129Discard 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.
130
131This has no effect on Windows as there's no good way to implement discarding stdin properly there.
132
133### Instance
134
135#### .text <sup>get/set</sup>
136
137Change the text after the spinner.
138
139#### .prefixText <sup>get/set</sup>
140
141Change the text before the spinner.
142
143No prefix text will be displayed if set to an empty string.
144
145#### .color <sup>get/set</sup>
146
147Change the spinner color.
148
149#### .spinner <sup>get/set</sup>
150
151Change the spinner.
152
153#### .indent <sup>get/set</sup>
154
155Change the spinner indent.
156
157#### .isSpinning <sup>get</sup>
158
159A boolean of whether the instance is currently spinning.
160
161#### .interval <sup>get</sup>
162
163The interval between each frame.
164
165The interval is decided by the chosen spinner.
166
167#### .start(text?)
168
169Start the spinner. Returns the instance. Set the current text if `text` is provided.
170
171#### .stop()
172
173Stop and clear the spinner. Returns the instance.
174
175#### .succeed(text?)
176
177Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
178
179#### .fail(text?)
180
181Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
182
183#### .warn(text?)
184
185Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
186
187#### .info(text?)
188
189Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
190
191#### .stopAndPersist(options?)
192
193Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
194
195##### options
196
197Type: `object`
198
199###### symbol
200
201Type: `string`\
202Default: `' '`
203
204Symbol to replace the spinner with.
205
206###### text
207
208Type: `string`\
209Default: Current `'text'`
210
211Text to be persisted after the symbol
212
213###### prefixText
214
215Type: `string`\
216Default: Current `prefixText`
217
218Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
219
220<img src="screenshot-2.gif" width="480">
221
222#### .clear()
223
224Clear the spinner. Returns the instance.
225
226#### .render()
227
228Manually render a new frame. Returns the instance.
229
230#### .frame()
231
232Get a new frame.
233
234### oraPromise(action, text)
235### oraPromise(action, options)
236
237Starts a spinner for a promise or promise-returning function. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the promise.
238
239```js
240import {oraPromise} from 'ora';
241
242await oraPromise(somePromise);
243```
244
245#### action
246
247Type: `Promise | ((spinner: ora.Ora) => Promise)`
248
249#### options
250
251Type: `object`
252
253All of the [options](#options) plus the following:
254
255##### successText
256
257Type: `string | ((result: T) => string) | undefined`
258
259The new text of the spinner when the promise is resolved.
260
261Keeps the existing text if `undefined`.
262
263##### failText
264
265Type: `string | ((error: Error) => string) | undefined`
266
267The new text of the spinner when the promise is rejected.
268
269Keeps the existing text if `undefined`.
270
271## FAQ
272
273### How do I change the color of the text?
274
275Use [Chalk](https://github.com/chalk/chalk):
276
277```js
278import ora from 'ora';
279import chalk from 'chalk';
280
281const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
282```
283
284### Why does the spinner freeze?
285
286JavaScript is single-threaded, so synchronous operations blocks the thread, including the spinner animation. Prefer asynchronous operations whenever possible.
287
288## Related
289
290- [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal
291- [listr](https://github.com/SamVerschueren/listr) - Terminal task list
292- [CLISpinner](https://github.com/kiliankoe/CLISpinner) - Terminal spinner library for Swift
293- [halo](https://github.com/ManrajGrover/halo) - Python port
294- [spinners](https://github.com/FGRibreau/spinners) - Terminal spinners for Rust
295- [marquee-ora](https://github.com/joeycozza/marquee-ora) - Scrolling marquee spinner for Ora
296- [briandowns/spinner](https://github.com/briandowns/spinner) - Terminal spinner/progress indicator for Go
297- [tj/go-spin](https://github.com/tj/go-spin) - Terminal spinner package for Go
298- [observablehq.com/@victordidenko/ora](https://observablehq.com/@victordidenko/ora) - Ora port to Observable notebooks
299- [spinnies](https://github.com/jcarpanelli/spinnies) - Terminal multi-spinner library for Node.js
300- [kia](https://github.com/HarryPeach/kia) - Simple terminal spinners for Deno 🦕