What is spinnies?
The 'spinnies' npm package is a utility for managing multiple CLI spinners. It allows you to create, update, and stop multiple spinners simultaneously, which is useful for providing visual feedback in command-line applications.
What are spinnies's main functionalities?
Creating and Adding Spinners
This feature allows you to create and add a new spinner with a specific text. The spinner can be identified by a unique key, in this case, 'spinner1'.
const Spinnies = require('spinnies');
const spinnies = new Spinnies();
spinnies.add('spinner1', { text: 'Loading...' });
Updating Spinners
This feature allows you to update the text and status of an existing spinner. The spinner is identified by its key, and you can change its text and status.
spinnies.update('spinner1', { text: 'Still loading...', status: 'spinning' });
Succeeding and Failing Spinners
This feature allows you to mark a spinner as succeeded or failed. You can provide a final text message to indicate the outcome.
spinnies.succeed('spinner1', { text: 'Loaded successfully!' });
spinnies.fail('spinner2', { text: 'Failed to load.' });
Removing Spinners
This feature allows you to remove a spinner from the list of active spinners. The spinner is identified by its key.
spinnies.remove('spinner1');
Other packages similar to spinnies
ora
Ora is a lightweight CLI spinner with support for multiple spinners. It provides similar functionality to spinnies but is more focused on single spinner instances. Ora is simpler to use for single tasks but lacks the built-in management of multiple spinners that spinnies offers.
cli-spinners
Cli-spinners is a collection of various spinner animations for use in the terminal. While it does not manage spinners directly, it provides a variety of spinner styles that can be used with other spinner management libraries like spinnies or ora.
listr
Listr is a library for creating elegant CLI task lists with progress spinners. It is more task-oriented and provides a higher-level abstraction for managing tasks and their statuses, including spinners. It is more feature-rich compared to spinnies but also more complex.
Spinnies
Node.js module to create and manage multiple spinners in command-line interface programs
Installation
$ npm i spinnies
Usage & Example
const spinnies = new Spinnies();
spinnies.add('spinner-1', { text: 'I am a spinner' });
spinnies.add('spinner-2', { text: 'I am another spinner' });
setTimeout(() => {
spinnies.succeed('spinner-1', { text: 'Success!' });
spinnies.fail('spinner-2', { text: 'Fail :(' });
}, 2000);
API
This library follows a non-error-throwing philosophy. If you provide an invalid option or an invalid value for a valid option it will be ignored.
Initialization:
new Spinnies([options])
Parameters
- options -
object
:
-
color - string
: Any valid chalk color. The default value is white
.
-
succeedColor - string
: Any valid chalk color. The default value is green
.
-
failColor - string
: Any valid chalk color. The default value is red
.
-
spinnerColor - string
: Any valid chalk color. The default value is greenBright
.
-
succeedPrefix - string
: The default value is ✓.
-
failPrefix- string
: The default value is ✖.
-
spinner- object
:
- interval -
number
- frames -
string[]
You can see the already provided spinner here.
-
disableSpins - boolean
: Disable spins (will still print raw messages).
Note: If you are working in any win32
platform, the default spin animation will be overriden. You can get rid of this defining a different spinner animation manually, or by using the integrated VSCode terminal or Windows Terminal.
Example:
const spinner = { interval: 80, frames: ['🍇', '🍈', '🍉', '🍋'] }
const spinnies = new Spinnies({ color: 'blue', succeedColor: 'green', spinner });
Instance methods:
add(name, [options])
Adds a new spinner with the given name.
Parameters:
- name -
string
: spinner reference name. - options -
object
:
- text: -
string
: Optional text to show in the spinner. If none is provided, the name
field will be shown. - indent: -
number
: Optional, indent the spinner with the given number of spaces. - status -
string
: Initial status of the spinner. Valid statuses are: succeed
, fail
, spinning
, non-spinnable
and stopped
. - color -
string
: Any valid chalk color. - succeedColor -
string
: Any valid chalk color. - failColor -
string
: Any valid chalk color.
Return value: Returns the spinner's options.
Example:
const spinnies = new Spinnies();
spinnies.add('spinner-1');
spinnies.add('another-spinner', { text: 'Hello, I am a spinner!', color: 'greenBright' });
pick(name)
Picks a spinner.
Parameters:
- name -
string
: spinner reference name.
Return value: Returns the spinner's options.
remove(name)
Removes a spinner.
Parameters:
- name -
string
: spinner reference name.
Return value: Returns the spinner's options.
update(name, [options])
Updates the spinner with name name
with the provided options.
Parameters:
- name -
string
: spinner reference name. - options -
object
:
- text: -
string
: Optional text to show in the spinner. If none is provided, the name
field will be shown. - status -
string
: New status of the spinner. Valid statuses are: succeed
, fail
, spinning
, non-spinnable
and stopped
. - color -
string
: Any valid chalk color. - succeedColor -
string
: Any valid chalk color. - failColor -
string
: Any valid chalk color.
Return value: Returns the spinner's options.
Example:
const spinnies = new Spinnies();
spinnies.add('spinner-1', { text: 'Hello! I am the initial text', color: 'green' });
spinnies.update('spinner-1', { text: 'Hello, I am an updated text!', color: 'blue' });
succeed(name, [options])
Sets the specified spinner status as succeed
.
Parameters:
- name -
string
: spinner reference name. - options -
object
:
- text: -
string
: Optional text to show in the spinner. If none is provided, the name
field will be shown. - succeedColor -
string
: Any valid chalk color.
Return value: Returns the spinner's options.
Example:
const spinnies = new Spinnies();
spinnies.add('spinner-1', { text: 'Hello! I am the initial text', color: 'green' });
spinnies.succeed('spinner-1', { text: 'Success!', successColor: 'greenBright' });
fail(name, [options])
Sets the specified spinner status as fail
.
Parameters:
- name -
string
: spinner reference name. - options -
object
:
- text: -
string
: Optional text to show in the spinner. If none is provided, the name
field will be shown. - failColor -
string
: Any valid chalk color.
Return value: Returns the spinner's options.
Example:
const spinnies = new Spinnies();
spinnies.add('spinner-1', { text: 'Hello! I am the initial text', color: 'green' });
spinnies.fail('spinner-1', { text: 'I failed', failColor: 'redBright' });
stopAll([status])
Stops the spinners and sets the non-succeeded and non-failed ones to the provided status, which can be succeed
, fail
or stopped
. You can see an example here.
hasActiveSpinners()
Return value: returns false
if all spinners have succeeded, failed or have been stopped.
Contribute
Star it, fork it, improve it, PR it! :raised_hands:.
Acknowledgements
Thanks to chalk for helping making this lib colorful :rainbow: and to ora which was a great inspiration :unicorn:.
License
MIT