Code Monkey home page Code Monkey logo

spinnies's Introduction

spin Spinnies spin

Node.js module to create and manage multiple spinners in command-line interface programs

npm CircleCI standard-readme compliant License: MIT


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-spinnableand 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-spinnableand 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' });
// some code
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' });
// some code
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' });
// some code
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! ๐Ÿ™Œ.

Acknowledgements

Thanks to chalk for helping making this lib colorful ๐ŸŒˆ and to ora which was a great inspiration ๐Ÿฆ„.

License

MIT

spinnies's People

Contributors

dependabot[bot] avatar jbcarpanelli avatar jonbarrow avatar mloughry avatar noriyotcp avatar rap2hpoutre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

spinnies's Issues

Add war and info to api

Hi,
it would be great to have info and warn api methods like Ora spinner.

thanks in advance.

Bug: Process doesn't exit when `remove` is called on an active spinner

Expected Behavior

The code below should exit cleanly after the timeout callback is fired.

const Spinnies = require("spinnies");

const spinners = new Spinnies();

spinners.add("spinner-1", { text: "some spinner" });

setTimeout(() => {
  spinners.remove("spinner-1");
  console.log("I should exit now!");
},1000);

Actual Behavior

The spinner is removed as expected, but the node process hangs after printing the text I should exit now.

Workaround

The following code exits as expected (note the addition of spinners.checkIfActiveSpinners() after the call to spinners.remove:

const Spinnies = require("spinnies");

const spinners = new Spinnies();

spinners.add("spinner-1", { text: "some spinner" });

setTimeout(() => {
  spinners.remove("spinner-1");
  spinners.checkIfActiveSpinners();
  console.log("I should exit now!");
},1000);

Potential fix

I think the fix proposed in SweetMNM#1 (a PR to a fork of this project) would also work here, though the patch itself likely wouldn't apply cleanly.

Use kleur instead of chalk?

I recently saw on bundlephobia that the package kleur is way smaller than chalk and has a similiar syntax. Kleur is also dependency free. What are your thoughts about replacing it with chalk to reduce the bundle size?

Allow custom non-spinnable prefix text

This should include a nonSpinnablePrefix prefix, a successPrefix and a failPrefix, that should override the defaults. If any of these attributes are deliberately set to 'null', non prefix should be shown.

Add the ability to send a promise as an option to spinnies

Instead of setting a spinner as succeed/fail using the spinners#fail and spinner#succeed methods, it would be great if spinnies could do this automatically when sending a promise as an option:

spinnies.add('spinner-1', { 
  text: 'Doing X..',
  onPromise: {
    promise: someFunction(arg1, arg2), 
    succeed: { 
      text: 'X succeeded',
      color: 'greenBright'
    },
    fail: {
      ...
    } 
});

disable forcing dashes on win32

Hi,
really awesome spinner and options, thanks alot for it :)

but can you please disable the enforcement of dashes spinners on win32, i am using windows 10 and it is perfectly capable of showing all symbols, i tested by manually commenting the if condition and it wokred.

thanks in advace :)

Add ability to remove a spinner from the spinners list

The idea is to implement a new method called remove that receives a spinner reference name as argument and removes that spinner from the list, and thus should not be printed again in the terminal If no reference is given, the method call does nothing.

Example usage:

const spinnies = new Spinnies();
spinnies.add('spinner-1');
spinnies.add('spinner-2');
spinnies.add('spinner-3');
setTimeout(() => {
  spinnies.remove('spinner-2');
}, 3000); // => should remove the second spinner and not print it again. 

Add typescript typings

hi,

it would be great if we have typings so we get IntelliSense in IDE and stop typescript complaining.

Indent

Thank you for maintaining this lib! ora has an indent property, could this be implemented here ?

Ability to update success/failure prefixes

I'm using spinnies to display the status of various stack deployments in makeshift/terragrunt-status, and it's working pretty great.

However, due to the binary nature of success/failure, it's sometimes hard to convey at a glance the status of a command that's been ran. Currently I'm using 'failed' as 'the whole command failed' and success for everything else. For anything in-between I have the text become yellow, but the successPrefix is still a green tick as it's defined when the spinny is created and can't (currently) be updated later.

I'd like to be able to update the successPrefix later in the spinnies lifecycle, so I can emulate statuses that are neither success nor failure by simply having a different prefix + text colour.

text is leaking

Hi,
i don't know what is happening exactly, but i think the picture below explains it better than words.

image

ability to spit out update status at desired location in terminal

Hi, what is awesome about this spinner, is that it updates the spinner at the same location in terminal it started. but sometimes after the spinner starts I want to log some text while the task is processing, this means if there is a lot of text output, the user will have to scroll back to the spinner start position in terminal to check if it succeeded or failed.

so could we have an option in this case, where we start the spinner, then at some point in future when we decide to do an update with success or fail, we can spit that to the terminal at the desired location?

hope i made it clear enough, thanks in advance.

Callback functions not being called on process signals

const Spinnies = require('spinnies');
const spinnies = new Spinnies();

spinnies.add('foo');
setTimeout(() => spinnies.succeed('foo'), 5000);

process.on('SIGINT', () => {
  process.stdout.write('^C received\n');
  process.exit(0);
});

The custom callback function is never launched.

Spinner success message dublication on CI

I got strange dublication in the CLI log and locally with disableSpins option:

- Downloading https://github.com/logux/logux/archive/master.zip
- Downloading https://github.com/logux/logux/archive/master.zip
- Downloading https://github.com/logux/core/archive/master.zip
- Downloading https://github.com/logux/logux/archive/master.zip
- Downloading https://github.com/logux/core/archive/master.zip
- Downloading https://github.com/logux/server/archive/master.zip
- logux downloaded
- Downloading https://github.com/logux/core/archive/master.zip
- Downloading https://github.com/logux/server/archive/master.zip
- logux downloaded
- logux-core downloaded
- Downloading https://github.com/logux/server/archive/master.zip
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Converting guides
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Converting guides
- Generating JSDoc
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Guide converted
- Generating JSDoc
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Guide converted
- JSDoc generated
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Guide converted
- JSDoc generated
- Building guides HTML
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Guide converted
- JSDoc generated
- Building guides HTML
- Building API HTML
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Guide converted
- JSDoc generated
- Building guides HTML
- API HTML generated
- logux downloaded
- logux-core downloaded
- logux-server downloaded
- Guide converted
- JSDoc generated
- Guides HTML generated
- API HTML generated

But with spinners (locally without disableSpins) everything works good.

I strarted this spinners only (pseudocode):

await Promise.all(projects.map(async ([url, name]) => {
  spin.add(`download-${ name }`, { text: `Downloading ${ url }` })
  await donwload(name, url)
  spin.succeed(`download-${ name }`, { text: `${ name } downloaded` })
})
Promise.all([
  async () => {
    spin.add('process-guides', { text: 'Converting guides' })
    await process()
    spin.succeed('process-guides', { text: 'Guide converted' })
  }),
  async () => {
    spin.add(`jsdoc${ projects.join() }`, { text: 'Generating JSDoc' })
    await jsdoc()
    spin.succeed(`jsdoc${ projects.join() }`, { text: 'JSDoc generated' })
  })
])
Promise.all([
  async () => {
    spin.add('build-api', { text: 'Building API HTML' })
    await buildHTML('guide')
    spin.succeed('build-api', { text: 'API HTML generated' })
  }),
  async () => {
    spin.add('build-pages', { text: 'Building guides HTML' })
    await buildHTML('api')
    spin.succeed('build-pages', { text: 'Guides HTML generated' })
  })
])

Could be an dublicate for #13

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.