What is nanospinner?
The nanospinner npm package is a lightweight and simple terminal spinner for Node.js applications. It is used to display a spinner in the terminal to indicate that a process is ongoing, which can enhance the user experience by providing visual feedback during long-running operations.
What are nanospinner's main functionalities?
Basic Spinner
This feature allows you to create a basic spinner that starts immediately and stops after a specified duration, indicating success.
const { createSpinner } = require('nanospinner');
const spinner = createSpinner('Loading...').start();
setTimeout(() => {
spinner.success({ text: 'Done!' });
}, 2000);
Custom Spinner Styles
This feature allows you to customize the spinner style. In this example, the 'dots' style is used, and the spinner indicates failure after a specified duration.
const { createSpinner } = require('nanospinner');
const spinner = createSpinner('Loading...', { spinner: 'dots' }).start();
setTimeout(() => {
spinner.error({ text: 'Failed!' });
}, 2000);
Spinner with Custom Interval
This feature allows you to set a custom interval for the spinner's animation. The spinner text is updated midway through the process, and it indicates success at the end.
const { createSpinner } = require('nanospinner');
const spinner = createSpinner('Loading...', { interval: 100 }).start();
setTimeout(() => {
spinner.update({ text: 'Almost there...' });
}, 1000);
setTimeout(() => {
spinner.success({ text: 'Done!' });
}, 2000);
Other packages similar to nanospinner
ora
Ora is a more feature-rich terminal spinner library for Node.js. It offers a wide range of spinner styles, color customization, and promises support. Compared to nanospinner, Ora provides more customization options and is suitable for more complex use cases.
cli-spinners
Cli-spinners is a collection of various spinner animations for use in the terminal. It is often used in conjunction with other libraries like Ora to provide a wide range of spinner styles. While cli-spinners itself does not manage the spinner state, it offers a large selection of animations that can be used with other spinner libraries.
listr
Listr is a library for creating elegant CLI task lists with progress spinners. It is designed for managing multiple tasks with nested subtasks and provides a more structured approach to displaying progress in the terminal. Compared to nanospinner, Listr is more suitable for complex task management scenarios.
Nano Spinner
Simple and tiny spinner library for Node.js
- It takes 18 times less space in node_modules than
ora
. - It is 6 times faster than
mico-spinner
.
import { createSpinner } from 'nanospinner'
let spinner = createSpinner('Run test')
spinner.start()
setTimeout(() => {
spinner.success()
}, 1000)
Benchmarks
The space in node_modules
including sub-dependencies:
$ ./test/size.js
Data from packagephobia.com
ora 597 kB
cli-spinners 28 kB
mico-spinner 28 kB
nanospinner 25 kB
Library loading time:
$ ./test/loading.js
mico-spinner 13.014 ms
nanospinner 1.930 ms
API
spin()
Looping over spin
method will animate a given spinner.
setInterval(() => {
spinner.spin()
}, 25)
start()
In order to start the spinner call start
. This will perform drawing the spinning animation
spinner.start()
stop()
In order to stop the spinner call stop
. This will finish drawing the spinning animation and return to new line.
spinner.stop()
spinner.stop('Done!')
success()
Use success
call to stop the spinning animation and replace the spinning symbol with check mark character to indicate successful completion.
spinner.success()
spinner.success('Successful!')
error()
Use error
call to stop the spinning animation and replace the spinning symbol with cross character to indicate error completion.
spinner.error()
spinner.error('Error!')
reset()
In order to reset the spinner to its initial frame do:
spinner.reset()
Roadmap