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
|
20 | const ora = require('ora');
|
21 |
|
22 | const spinner = ora('Loading unicorns').start();
|
23 |
|
24 | setTimeout(() => {
|
25 | spinner.color = 'yellow';
|
26 | spinner.text = 'Loading rainbows';
|
27 | }, 1000);
|
28 | ```
|
29 |
|
30 | ## API
|
31 |
|
32 | ### ora(text)
|
33 | ### ora(options)
|
34 |
|
35 | If a string is provided, it is treated as a shortcut for [`options.text`](#text).
|
36 |
|
37 | #### options
|
38 |
|
39 | Type: `object`
|
40 |
|
41 | ##### text
|
42 |
|
43 | Type: `string`
|
44 |
|
45 | Text to display after the spinner.
|
46 |
|
47 | ##### prefixText
|
48 |
|
49 | Type: `string`
|
50 |
|
51 | Text to display before the spinner. No prefix text will be displayed if set to an empty string.
|
52 |
|
53 | ##### spinner
|
54 |
|
55 | Type: `string | object`\
|
56 | Default: `'dots'` <img src="screenshot-spinner.gif" width="14">
|
57 |
|
58 | 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. On Windows, it will always use the `line` spinner as the Windows command-line doesn't have proper Unicode support.
|
59 |
|
60 | Or an object like:
|
61 |
|
62 | ```js
|
63 | {
|
64 | interval: 80, // Optional
|
65 | frames: ['-', '+', '-']
|
66 | }
|
67 | ```
|
68 |
|
69 | ##### color
|
70 |
|
71 | Type: `string`\
|
72 | Default: `'cyan'`\
|
73 | Values: `'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray'`
|
74 |
|
75 | Color of the spinner.
|
76 |
|
77 | ##### hideCursor
|
78 |
|
79 | Type: `boolean`\
|
80 | Default: `true`
|
81 |
|
82 | Set to `false` to stop Ora from hiding the cursor.
|
83 |
|
84 | ##### indent
|
85 |
|
86 | Type: `number`\
|
87 | Default: `0`
|
88 |
|
89 | Indent the spinner with the given number of spaces.
|
90 |
|
91 | ##### interval
|
92 |
|
93 | Type: `number`\
|
94 | Default: Provided by the spinner or `100`
|
95 |
|
96 | Interval between each frame.
|
97 |
|
98 | Spinners provide their own recommended interval, so you don't really need to specify this.
|
99 |
|
100 | ##### stream
|
101 |
|
102 | Type: `stream.Writable`\
|
103 | Default: `process.stderr`
|
104 |
|
105 | Stream to write the output.
|
106 |
|
107 | You could for example set this to `process.stdout` instead.
|
108 |
|
109 | ##### isEnabled
|
110 |
|
111 | Type: `boolean`
|
112 |
|
113 | Force 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 |
|
115 | Note 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 |
|
119 | Type: `boolean`\
|
120 | Default: `true`
|
121 |
|
122 | Discard 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 |
|
124 | This 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 |
|
130 | Start the spinner. Returns the instance. Set the current text if `text` is provided.
|
131 |
|
132 | #### .stop()
|
133 |
|
134 | Stop and clear the spinner. Returns the instance.
|
135 |
|
136 | #### .succeed(text?)
|
137 |
|
138 | Stop 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 |
|
142 | Stop 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 |
|
146 | Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
|
147 |
|
148 | #### .info(text?)
|
149 |
|
150 | Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
|
151 |
|
152 | #### .isSpinning
|
153 |
|
154 | A boolean of whether the instance is currently spinning.
|
155 |
|
156 | #### .stopAndPersist(options?)
|
157 |
|
158 | Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
|
159 |
|
160 | ##### options
|
161 |
|
162 | Type: `object`
|
163 |
|
164 | ###### symbol
|
165 |
|
166 | Type: `string`\
|
167 | Default: `' '`
|
168 |
|
169 | Symbol to replace the spinner with.
|
170 |
|
171 | ###### text
|
172 |
|
173 | Type: `string`\
|
174 | Default: Current `'text'`
|
175 |
|
176 | Text to be persisted after the symbol
|
177 |
|
178 | ###### prefixText
|
179 |
|
180 | Type: `string`\
|
181 | Default: Current `prefixText`
|
182 |
|
183 | Text 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 |
|
189 | Clear the spinner. Returns the instance.
|
190 |
|
191 | #### .render()
|
192 |
|
193 | Manually render a new frame. Returns the instance.
|
194 |
|
195 | #### .frame()
|
196 |
|
197 | Get a new frame.
|
198 |
|
199 | #### .text
|
200 |
|
201 | Change the text after the spinner.
|
202 |
|
203 | #### .prefixText
|
204 |
|
205 | Change the text before the spinner. No prefix text will be displayed if set to an empty string.
|
206 |
|
207 | #### .color
|
208 |
|
209 | Change the spinner color.
|
210 |
|
211 | #### .spinner
|
212 |
|
213 | Change the spinner.
|
214 |
|
215 | #### .indent
|
216 |
|
217 | Change the spinner indent.
|
218 |
|
219 | ### ora.promise(action, text)
|
220 | ### ora.promise(action, options)
|
221 |
|
222 | Starts 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 |
|
226 | Type: `Promise`
|
227 |
|
228 | ## FAQ
|
229 |
|
230 | ### How do I change the color of the text?
|
231 |
|
232 | Use [Chalk](https://github.com/chalk/chalk):
|
233 |
|
234 | ```js
|
235 | const ora = require('ora');
|
236 | const chalk = require('chalk');
|
237 |
|
238 | const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
|
239 | ```
|
240 |
|
241 | ### Why does the spinner freeze?
|
242 |
|
243 | JavaScript 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
|