nanospinner

repository·master·Indexed 42 days ago

https://github.com/usmanyunusov/nanospinner

A minimal, high-performance terminal spinner for Node.js version 1.2.2 designed to minimize dependency size and footprint. It provides a simple API via createSpinner to manage terminal animations with methods for starting, stopping, and updating status using .success(), .warn(), .error(), and .info().

Tokens
2K
Snippets
12
Records
12
Agent score
3%

What's inside nanospinner

  1. Quickstart with Nano Spinner

    master

    To use Nano Spinner, import createSpinner from nanospinner. You can initialize a spinner with a text label and immediately start the animation. Use the .success() method to stop the animation and show a completion mark after your task is finished.

    import { createSpinner } from 'nanospinner'
    
    const spinner = createSpinner('Run test').start()
    
    setTimeout(() => {
      spinner.success()
    }, 1000)
  2. Use createSpinner to initialize a spinner

    master

    The createSpinner function is the entry point for creating a new spinner instance. It is designed to be lightweight and supports both ESM and CJS projects with built-in TypeScript definitions.

    import { createSpinner } from 'nanospinner'
    const spinner = createSpinner('Initial text')
  3. Stop the spinner with .stop()

    master

    Call .stop(options?) to finish the spinning animation and return to a new line. You can provide a final message or custom styling.

    spinner.stop()
    spinner.stop('Done!')
    spinner.stop({ text: 'Done!', mark: ':O', color: 'magenta' })
  4. Indicate status with .success(), .warn(), .error(), and .info()

    master

    These methods stop the spinning animation and replace the spinning symbol with a specific mark character to indicate the outcome of a task.

    • .success(options?): Shows a check mark.
    • .warn(options?): Shows a warning mark.
    • .error(options?): Shows a cross character.
    • .info(options?): Shows an info mark.
    spinner.success('Successful!')
    spinner.warn('Warning!')
    spinner.error('Error!')
    spinner.info('Info!')
  5. Start the spinner animation with .start()

    master

    Call .start(options?) to begin drawing the spinning animation in the terminal. You can pass a string directly or an options object.

    spinner.start()
    spinner.start('Start')
    spinner.start({ text: 'Start', color: 'yellow' })
  6. Dynamically update the spinner with .update()

    master

    Use .update(options?) to change the spinner's properties while it is running. You can update the text, color, output stream, animation frames, or the animation interval.

    spinner.update({
      text: 'Run test',
      color: 'white',
      stream: process.stdout,
      frames: ['.', 'o', '0', '@', '*'],
      interval: 100,
    })
  7. Control the Spinner lifecycle

    master

    The Spinner interface provides methods to manage the animation state and terminal output:

    • start(opts?): Begins the animation. opts can be a string or an object { text?: string; color?: Color }.
    • stop(opts?): Stops the animation. opts can be a string or an object { text?: string; mark?: string; color?: Color; update?: boolean }.
    • update(opts): Updates the spinner's configuration (text, frames, interval, or color). Accepts a string or an Options object.
    • reset(): Resets the spinner state (current frame, lines, spinning status) and clears timers.
    • clear(): Clears the lines previously written by the spinner in the terminal.
    • isSpinning(): Returns a boolean indicating if the spinner is currently active.
    const spinner = createSpinner('Working...');
    
    spinner.start();
    // ... do work
    spinner.update('Almost done...');
    // ... do work
    spinner.stop();
  8. Create a spinner with createSpinner()

    master

    Use createSpinner(text, opts) to initialize a new terminal animation instance. You can provide an initial text string and an optional Options object to configure the behavior.

    Options:

    • stream: A NodeJS.WriteStream to write to (defaults to process.stderr).
    • frames: An array of strings representing the animation frames.
    • interval: The delay between frames in milliseconds (defaults to 50).
    • text: The initial text displayed next to the spinner.
    • color: A color name (compatible with picocolors) for the spinner frame (defaults to 'yellow').
    import { createSpinner } from 'nanospinner';
    
    const spinner = createSpinner('Loading...', { interval: 100, color: 'cyan' });
  9. Use status methods: success, error, warn, and info

    master

    Nano Spinner provides semantic shorthand methods to stop the spinner and display a final status message with a specific color and icon (mark).

    Each method accepts either a string (for the message) or an object { text?: string; mark?: string; update?: boolean }.

    MethodColorDefault MarkDescription
    success()greensymbols.tickIndicates successful completion
    error()redsymbols.crossIndicates a failure
    warn()yellowsymbols.warnIndicates a warning
    info()bluesymbols.infoIndicates informational status

    Note: If update: true is passed in the options object, the spinner will update the current line instead of appending a new line.

    const spinner = createSpinner('Processing data...');
    
    try {
      await performTask();
      spinner.success('Data processed successfully!');
    } catch (err) {
      spinner.error('Failed to process data');
    }
  10. The Spinner interface reference

    master

    The Spinner interface defines the public API for controlling terminal animations. All methods return the Spinner instance to allow for method chaining.

    export interface Spinner {
      success(opts?: { text?: string; mark?: string; update?: boolean } | string): Spinner
      error(opts?: { text?: string; mark?: string; update?: boolean } | string): Spinner
      warn(opts?: { text?: string; mark?: string; update?: boolean } | string): Spinner
      info(opts?: { text?: string; mark?: string; update?: boolean } | string): Spinner
      stop(opts?: { text?: string; mark?: string; color?: Color; update?: boolean } | string): Spinner
      start(opts?: { text?: string; color?: Color } | string): Spinner
      update(opts: Options | string): Spinner
      reset(): Spinner
      clear(): Spinner
      spin(): Spinner
      write(str: string, clear?: boolean): Spinner
      render(): Spinner
      loop(): Spinner
      isSpinning(): boolean
    }