[6a3a178] | 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 | ```
|
---|
| 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 | () => 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 | 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 | #### .start(text?)
|
---|
| 136 |
|
---|
| 137 | Start the spinner. Returns the instance. Set the current text if `text` is provided.
|
---|
| 138 |
|
---|
| 139 | #### .stop()
|
---|
| 140 |
|
---|
| 141 | Stop and clear the spinner. Returns the instance.
|
---|
| 142 |
|
---|
| 143 | #### .succeed(text?)
|
---|
| 144 |
|
---|
| 145 | Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
|
---|
| 146 |
|
---|
| 147 | #### .fail(text?)
|
---|
| 148 |
|
---|
| 149 | Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
|
---|
| 150 |
|
---|
| 151 | #### .warn(text?)
|
---|
| 152 |
|
---|
| 153 | Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
|
---|
| 154 |
|
---|
| 155 | #### .info(text?)
|
---|
| 156 |
|
---|
| 157 | Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
|
---|
| 158 |
|
---|
| 159 | #### .isSpinning
|
---|
| 160 |
|
---|
| 161 | A boolean of whether the instance is currently spinning.
|
---|
| 162 |
|
---|
| 163 | #### .stopAndPersist(options?)
|
---|
| 164 |
|
---|
| 165 | Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
|
---|
| 166 |
|
---|
| 167 | ##### options
|
---|
| 168 |
|
---|
| 169 | Type: `object`
|
---|
| 170 |
|
---|
| 171 | ###### symbol
|
---|
| 172 |
|
---|
| 173 | Type: `string`\
|
---|
| 174 | Default: `' '`
|
---|
| 175 |
|
---|
| 176 | Symbol to replace the spinner with.
|
---|
| 177 |
|
---|
| 178 | ###### text
|
---|
| 179 |
|
---|
| 180 | Type: `string`\
|
---|
| 181 | Default: Current `'text'`
|
---|
| 182 |
|
---|
| 183 | Text to be persisted after the symbol
|
---|
| 184 |
|
---|
| 185 | ###### prefixText
|
---|
| 186 |
|
---|
| 187 | Type: `string`\
|
---|
| 188 | Default: Current `prefixText`
|
---|
| 189 |
|
---|
| 190 | Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
|
---|
| 191 |
|
---|
| 192 | <img src="screenshot-2.gif" width="480">
|
---|
| 193 |
|
---|
| 194 | #### .clear()
|
---|
| 195 |
|
---|
| 196 | Clear the spinner. Returns the instance.
|
---|
| 197 |
|
---|
| 198 | #### .render()
|
---|
| 199 |
|
---|
| 200 | Manually render a new frame. Returns the instance.
|
---|
| 201 |
|
---|
| 202 | #### .frame()
|
---|
| 203 |
|
---|
| 204 | Get a new frame.
|
---|
| 205 |
|
---|
| 206 | #### .text
|
---|
| 207 |
|
---|
| 208 | Change the text after the spinner.
|
---|
| 209 |
|
---|
| 210 | #### .prefixText
|
---|
| 211 |
|
---|
| 212 | Change the text before the spinner. No prefix text will be displayed if set to an empty string.
|
---|
| 213 |
|
---|
| 214 | #### .color
|
---|
| 215 |
|
---|
| 216 | Change the spinner color.
|
---|
| 217 |
|
---|
| 218 | #### .spinner
|
---|
| 219 |
|
---|
| 220 | Change the spinner.
|
---|
| 221 |
|
---|
| 222 | #### .indent
|
---|
| 223 |
|
---|
| 224 | Change the spinner indent.
|
---|
| 225 |
|
---|
| 226 | ### ora.promise(action, text)
|
---|
| 227 | ### ora.promise(action, options)
|
---|
| 228 |
|
---|
| 229 | 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.
|
---|
| 230 |
|
---|
| 231 | #### action
|
---|
| 232 |
|
---|
| 233 | Type: `Promise`
|
---|
| 234 |
|
---|
| 235 | ## FAQ
|
---|
| 236 |
|
---|
| 237 | ### How do I change the color of the text?
|
---|
| 238 |
|
---|
| 239 | Use [Chalk](https://github.com/chalk/chalk):
|
---|
| 240 |
|
---|
| 241 | ```js
|
---|
| 242 | const ora = require('ora');
|
---|
| 243 | const chalk = require('chalk');
|
---|
| 244 |
|
---|
| 245 | const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
|
---|
| 246 | ```
|
---|
| 247 |
|
---|
| 248 | ### Why does the spinner freeze?
|
---|
| 249 |
|
---|
| 250 | JavaScript is single-threaded, so synchronous operations blocks the thread, including the spinner animation. Prefer asynchronous operations whenever possible.
|
---|
| 251 |
|
---|
| 252 | ## Related
|
---|
| 253 |
|
---|
| 254 | - [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal
|
---|
| 255 | - [listr](https://github.com/SamVerschueren/listr) - Terminal task list
|
---|
| 256 | - [CLISpinner](https://github.com/kiliankoe/CLISpinner) - Terminal spinner library for Swift
|
---|
| 257 | - [halo](https://github.com/ManrajGrover/halo) - Python port
|
---|
| 258 | - [spinners](https://github.com/FGRibreau/spinners) - Terminal spinners for Rust
|
---|
| 259 | - [marquee-ora](https://github.com/joeycozza/marquee-ora) - Scrolling marquee spinner for Ora
|
---|
| 260 | - [briandowns/spinner](https://github.com/briandowns/spinner) - Terminal spinner/progress indicator for Go
|
---|
| 261 | - [tj/go-spin](https://github.com/tj/go-spin) - Terminal spinner package for Go
|
---|
| 262 | - [observablehq.com/@victordidenko/ora](https://observablehq.com/@victordidenko/ora) - Ora port to Observable notebooks
|
---|
| 263 | - [spinnies](https://github.com/jcarpanelli/spinnies) - Terminal multi-spinner library for Node.js
|
---|
| 264 | - [kia](https://github.com/HarryPeach/kia) - Simple terminal spinners for Deno 🦕
|
---|