npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@slimio/async-cli-spinner

v0.5.2

Published

Asynchronous CLI Spinner. Allow to create and manage simultaneous/multiple spinners at a time.

Downloads

244

Readme

Async-cli-spinner

version Maintenance MIT dep size Known Vulnerabilities Build Status Greenkeeper badge

Asynchronous CLI Spinner. This package has been created to handle simultaneous/multiple spinner at a time. The package has been inspired by Ora but in Asynchronous.

All available spinners are part of cli-spinners package.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/async-cli-spinner
# or
$ yarn add @slimio/async-cli-spinner

Usage example

Create and wait multiple spinner at a time.

const { promisify } = require("util");
const Spinner = require("@slimio/async-cli-spinner");

const sleep = promisify(setTimeout);

async function fnWithSpinner(prefixText, succeed = true) {
    const spinner = new Spinner({ prefixText }).start("Start working!");

    await sleep(1000);
    spinner.text = "Work in progress...";
    await sleep(1000);

    if (succeed) {
        spinner.succeed(`All done in ${spinner.elapsedTime.toFixed(2)}ms !`);
    }
    else {
        spinner.failed("Something wrong happened !");
    }
}

Spinner.startAll([
    fnWithSpinner,
    Spinner.create(fnWithSpinner),
    Spinner.create(fnWithSpinner, "Item 1"),
    Spinner.create(fnWithSpinner, "Item 2", false)
])
.then(() => console.log("All spinners finished!"))
.catch(console.error);

If you want to only achieve one Spinner by one Spinner, use it like Ora (it will work)

const spinner = new Spinner().start("Start working!");

await sleep(1000);
spinner.text = "Work in progress...";

await sleep(1000);
spinner.succeed("All done !");

👀 When you are working on a CLI that can be used as an API too, the verbose option allow you to disable the Spinner.

API

Spinner line structure : ${spinner} ${prefixText} - ${text}

Properties :

declare namespace Spinner {
    public spinner: cliSpinners.Spinner;
    public prefixText: string;
    public text: string;
    public color: string;
    public started: boolean;
    public startTime: number;
    public stream: TTY.WriteStream;
    public readonly elapsedTime: number;
}
  • spinner: spinner type (default: "dots")
  • prefixText: mostly used to differentiate each spinner
  • text: you can change text at any moment.
  • color: spinner color
  • elapsedTime: time elapsed since start() call

Create a new Spinner object. options is described by the following TypeScript interface:

declare namespace Spinner {
    interface spinnerObj {
        frames: string[];
        interval: number;
    }

    interface options {
        spinner: SpinnerObj | Spinner.spinners;
        text: string;
        prefixText: string;
        color: string;
        verbose: boolean;
    }
}

👀 Look cli-spinners for all kind of available spinners.

Example:

const Spinner = require("@slimio/async-cli-spinner");

const spinner = new Spinner();
const dotsSpinner = new Spinner({ spinner: "dots" });

⚠️ Only accept functions that return a Promise.

Options is described by the following TypeScript interface:

declare namespace Spinner {
    type RecapSet = "none" | "error" | "always";

    interface startOpt {
        recap: RecapSet;
        rejects: boolean;
    }
}

Default recap : always

async function fnWithSpinner(prefixText) {
    const spinner = new Spinner({ prefixText }).start("Start working!");

    await new Promise((resolve) => setTimeout(resolve, 1000));
    spinner.text = "Work in progress...";

    await new Promise((resolve) => setTimeout(resolve, 1000));
    spinner.succeed("All done !");
}

Spinner.startAll([
    fnWithSpinner("Item 1"), // <-- Wrong, it's executed directly, not in startAll
    Spinner.create(fnWithSpinner, "Item 2") // <-- What you should do
])
.then(() => console.log("All spinners finished!"))
.catch(console.error);

Start the spinner in the CLI and write the text passed in param.

const Spinner = require("@slimio/async-cli-spinner");

async function fnWithSpinner() {
    const spinner = new Spinner().start("Start working!");
}

Spinner.startAll([
    fnWithSpinner
])
.then(() => console.log("All spinners finished!"))
.catch(console.error);

Stop the spinner in the CLI, write the text passed in param and mark it as succeed with a symbol.

const Spinner = require("@slimio/async-cli-spinner");

async function fnWithSpinner() {
    const spinner = new Spinner().start("Start working!");

    await new Promise((resolve) => setTimeout(resolve, 1000));
    spinner.succeed("All done !");
}

Spinner.startAll([
    fnWithSpinner
])
.then(() => console.log("All spinners finished!"))
.catch(console.error);

Stop the spinner in the CLI, write the text passed in param and mark it as failed with a symbol.

const Spinner = require("@slimio/async-cli-spinner");

async function fnWithSpinner() {
    const spinner = new Spinner().start("Start working!");

    await new Promise((resolve) => setTimeout(resolve, 1000));
    spinner.failed("Something wrong happened !");
}

Spinner.startAll([
    fnWithSpinner
])
.then(() => console.log("All spinners finished!"))
.catch(console.error);

⚠️ Functions start(), succeed() and failed() are supposed to be executed in a function which return a promise and will be called by Spinner.startAll().

Dependencies

|Name|Refactoring|Security Risk|Usage| |---|---|---|---| |@slimio/is|Minor|Low|Type checker| |@slimio/wcwidth|⚠️Major|Low|Get CLI columns for special characters| |ansi-regex|Minor|Low|Get ANSI code| |cli-cursor|⚠️Major|High|Show/Hide CLI cursor| |cli-spinners|Minor|Low|Bunch of spinner| |kleur|Minor|Low|CLI color| |strip-ansi|Minor|Low|ANSI escape codes|

License

MIT