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
|
14 | npm install ora
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ```js
|
20 | import ora from '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 | () => string`
|
50 |
|
51 | Text 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 |
|
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/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 |
|
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 | The 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 | ##### isSilent
|
118 |
|
119 | Type: `boolean`\
|
120 | Default: `false`
|
121 |
|
122 | Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
|
123 |
|
124 | ##### discardStdin
|
125 |
|
126 | Type: `boolean`\
|
127 | Default: `true`
|
128 |
|
129 | 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.
|
130 |
|
131 | This 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 |
|
137 | Change the text after the spinner.
|
138 |
|
139 | #### .prefixText <sup>get/set</sup>
|
140 |
|
141 | Change the text before the spinner.
|
142 |
|
143 | No prefix text will be displayed if set to an empty string.
|
144 |
|
145 | #### .color <sup>get/set</sup>
|
146 |
|
147 | Change the spinner color.
|
148 |
|
149 | #### .spinner <sup>get/set</sup>
|
150 |
|
151 | Change the spinner.
|
152 |
|
153 | #### .indent <sup>get/set</sup>
|
154 |
|
155 | Change the spinner indent.
|
156 |
|
157 | #### .isSpinning <sup>get</sup>
|
158 |
|
159 | A boolean of whether the instance is currently spinning.
|
160 |
|
161 | #### .interval <sup>get</sup>
|
162 |
|
163 | The interval between each frame.
|
164 |
|
165 | The interval is decided by the chosen spinner.
|
166 |
|
167 | #### .start(text?)
|
168 |
|
169 | Start the spinner. Returns the instance. Set the current text if `text` is provided.
|
170 |
|
171 | #### .stop()
|
172 |
|
173 | Stop and clear the spinner. Returns the instance.
|
174 |
|
175 | #### .succeed(text?)
|
176 |
|
177 | Stop 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 |
|
181 | Stop 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 |
|
185 | Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
|
186 |
|
187 | #### .info(text?)
|
188 |
|
189 | Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
|
190 |
|
191 | #### .stopAndPersist(options?)
|
192 |
|
193 | Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
|
194 |
|
195 | ##### options
|
196 |
|
197 | Type: `object`
|
198 |
|
199 | ###### symbol
|
200 |
|
201 | Type: `string`\
|
202 | Default: `' '`
|
203 |
|
204 | Symbol to replace the spinner with.
|
205 |
|
206 | ###### text
|
207 |
|
208 | Type: `string`\
|
209 | Default: Current `'text'`
|
210 |
|
211 | Text to be persisted after the symbol
|
212 |
|
213 | ###### prefixText
|
214 |
|
215 | Type: `string`\
|
216 | Default: Current `prefixText`
|
217 |
|
218 | Text 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 |
|
224 | Clear the spinner. Returns the instance.
|
225 |
|
226 | #### .render()
|
227 |
|
228 | Manually render a new frame. Returns the instance.
|
229 |
|
230 | #### .frame()
|
231 |
|
232 | Get a new frame.
|
233 |
|
234 | ### oraPromise(action, text)
|
235 | ### oraPromise(action, options)
|
236 |
|
237 | Starts 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
|
240 | import {oraPromise} from 'ora';
|
241 |
|
242 | await oraPromise(somePromise);
|
243 | ```
|
244 |
|
245 | #### action
|
246 |
|
247 | Type: `Promise | ((spinner: ora.Ora) => Promise)`
|
248 |
|
249 | #### options
|
250 |
|
251 | Type: `object`
|
252 |
|
253 | All of the [options](#options) plus the following:
|
254 |
|
255 | ##### successText
|
256 |
|
257 | Type: `string | ((result: T) => string) | undefined`
|
258 |
|
259 | The new text of the spinner when the promise is resolved.
|
260 |
|
261 | Keeps the existing text if `undefined`.
|
262 |
|
263 | ##### failText
|
264 |
|
265 | Type: `string | ((error: Error) => string) | undefined`
|
266 |
|
267 | The new text of the spinner when the promise is rejected.
|
268 |
|
269 | Keeps the existing text if `undefined`.
|
270 |
|
271 | ## FAQ
|
272 |
|
273 | ### How do I change the color of the text?
|
274 |
|
275 | Use [Chalk](https://github.com/chalk/chalk):
|
276 |
|
277 | ```js
|
278 | import ora from 'ora';
|
279 | import chalk from 'chalk';
|
280 |
|
281 | const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
|
282 | ```
|
283 |
|
284 | ### Why does the spinner freeze?
|
285 |
|
286 | JavaScript 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 🦕
|