Skip to content

Latest commit

 

History

History
83 lines (64 loc) · 1.92 KB

usage.md

File metadata and controls

83 lines (64 loc) · 1.92 KB

⬅️ to README.md

Usage

Usage with event loop - Asynchronous mode(default)

use AlecRabbit\Spinner\Factory\Factory;

require_once __DIR__ . '/../bootstrap.php';

$spinner = Factory::createSpinner();

Usage without event loop - Synchronous mode

In synchronous mode usage is a bit more complicated. Simply speaking, you need to periodically call render() method of IDriver implementation.

$driver = \AlecRabbit\Spinner\Facade::getDriver();

while (true) {
    $driver->render();
    // do some work 
}

Custom palettes

There are four palettes supplied with the package:

  • Rainbow (style)
  • NoStylePalette
  • Snake (characters)
  • NoCharPalette

How to create your own character palette

class Dots extends ACharPalette {
    protected function createFrame(string $element): ICharFrame
    {
        return new CharFrame($element, 3); // note the width is 3
    }

    /** @inheritDoc */
    protected function sequence(): Traversable
    {
        // note the width of each element
        yield from ['   ', '.  ', '.. ', '...', ' ..', '  .', '   ']; 
    }
}

How to create your own style palette

class Greeny extends AStylePalette {
    protected function ansi4StyleFrames(): Traversable
    {
        yield from [
            $this->createFrame("\e[92m%s\e[39m"), // note the ANSI codes
        ];
    }

    protected function ansi8StyleFrames(): Traversable
    {
        return $this->ansi4StyleFrames();
    }

    protected function ansi24StyleFrames(): Traversable
    {
        return $this->ansi4StyleFrames();
    }

    protected function getInterval(StylingMethodMode $stylingMode): ?int
    {
        return null; // due to single style frame
    }
}