@pcs/spinner
v1.0.2
Published
Loading spinner for console applications
Downloads
7
Readme
PCS-Spinner
Installation
NPM:
npm install @pcs/spinner
Yarn:
yarn add @pcs/spinner
Usage
There are two ways in which the spinner can be used and they are:
createSpinner
import { createSpinner, Spinner } from '@pcs/spinner'
const spinner: Spinner = createSpinner('Import', 'Initialising')
try {
await initialize()
spinner.setText('Importing data')
const result = await importData()
if (result.success) {
spinner.setComplete('Finished import')
} else {
spinner.setError('Failed to import')
}
} catch (err) {
spinner.setError(err.message)
throw err
}
runWithSpinner
You can use runWithSpinner
to run a function with an attached spinner. If the function errors the spinner will automatically display a failed state with the text being the error.message
.
Example:
import { runWithSpinner, Spinner } from '@pcs/spinner'
const result = await runWithSpinner(
'Import',
'Initialising',
async (spinner: Spinner) => {
await initialize()
spinner.setText('Importing data')
const result = await importData()
spinner.setComplete('Finished import')
return result
}
)