What is ora?
The ora npm package is a terminal spinner for Node.js applications. It provides an easy way to display a spinner in the command line interface (CLI) during the execution of long-running tasks, giving users a visual cue that processing is taking place. It is highly customizable and can be used to enhance the user experience in CLI tools.
What are ora's main functionalities?
Basic spinner
This code sample demonstrates how to create a basic spinner that starts with a message, runs for a specified duration, and then marks the operation as successful.
const ora = require('ora');
const spinner = ora('Loading unicorns').start();
setTimeout(() => {
spinner.succeed('Unicorns loaded');
}, 1000);
Customize spinner color, text, and spinner
This code sample shows how to customize the spinner's appearance by setting the color, text, and spinner design. It also demonstrates how to update these properties while the spinner is running.
const ora = require('ora');
const spinner = ora({
text: 'Loading rainbows',
color: 'magenta',
spinner: 'moon'
}).start();
setTimeout(() => {
spinner.color = 'yellow';
spinner.text = 'Loading sun';
}, 1000);
Control spinner success and failure
This code sample illustrates how to control the spinner to indicate success or failure of an operation. In this case, the spinner is stopped with a failure message.
const ora = require('ora');
const spinner = ora('Loading with great anticipation').start();
setTimeout(() => {
spinner.fail('Something went wrong');
}, 1000);
Other packages similar to ora
cli-spinners
cli-spinners provides a collection of spinners for use in the terminal, similar to ora, but it does not manage the spinner state or provide methods to start, succeed, or fail. It is more of a raw collection of spinner designs.
clui
clui is a collection of Node.js command line UI components, which includes a spinner similar to ora. However, clui offers a broader set of UI components such as gauges, progress bars, and line charts, making it more versatile for building complex CLI interfaces.
log-update
log-update is a package that allows you to log by overwriting the previous output in the terminal. While it doesn't provide a built-in spinner, it can be used to create custom spinners or other dynamic terminal interfaces by manually controlling the output rendering.
ora
Elegant terminal spinner
Install
$ npm install --save ora
Usage
const ora = require('ora');
const spinner = ora('Loading unicorns').start();
setTimeout(() => {
spinner.color = 'yellow';
spinner.text = 'Loading rainbows';
}, 1000);
API
It will gracefully not do anything when there's no TTY or when in a CI.
ora([options|text])
If a string is provided, it is treated as a shortcut for options.text
.
options
Type: object
text
Type: string
Text to display after the spinner.
spinner
Type: string
object
Default: dots
Name of one of the provided spinners. See example.js
in this repo if you want to test out different spinners.
Or an object like:
{
interval: 80,
frames: ['-', '+', '-']
}
color
Type: string
Default: cyan
Values: black
red
green
yellow
blue
magenta
cyan
white
gray
Color of the spinner.
interval
Type: number
Default: Provided by the spinner or 100
Interval between each frame.
Spinners provide their own recommended interval, so you don't really need to specify this.
stream
Type: WritableStream
Default: process.stderr
Stream to write the output.
You could for example set this to process.stdout
instead.
enabled
Type: boolean
Default: false
Force enabling of the spinner regardless of the stream
not being run inside a TTY context and/or in a CI environment.
Instance
.start()
Start the spinner. Returns the instance.
.stop()
Stop and clear the spinner. Returns the instance.
.clear()
Clear the spinner. Returns the instance.
.render()
Manually render a new frame. Returns the instance.
.frame()
Get a new frame.
.text
Change the text.
.color
Change the spinner color.
Related
License
MIT © Sindre Sorhus