UNPKG

@teaui/core

Version:

A high-level terminal UI library for Node

54 lines 1.46 kB
import { View } from '../View.js'; import { Point, Size } from '../geometry.js'; export class Spinner extends View { #isAnimating = false; #tick = 0; #frame = 0; #frameLen = 1; constructor({ isAnimating, ...props } = {}) { super(props); this.#update({ isAnimating }); } update({ isAnimating, ...props }) { super.update(props); this.#update({ isAnimating }); } get isAnimating() { return this.#isAnimating; } set isAnimating(value) { if (value === this.#isAnimating) { return; } this.#isAnimating = value; this.invalidateRender(); } #update({ isAnimating }) { this.#isAnimating = isAnimating ?? true; } naturalSize(_available) { return new Size(1, 1); } receiveTick(dt) { this.#tick = this.#tick + dt; if (this.#tick > HZ) { this.#tick %= HZ; this.#frame = (this.#frame + 1) % this.#frameLen; } return true; } render(viewport) { if (viewport.isEmpty) { return; } if (this.#isAnimating) { viewport.registerTick(); } const char = ONE[this.#frame]; viewport.write(char, Point.zero); this.#frameLen = ONE.length; } } const ONE = ['⣾', '⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']; const HZ = 1000 / 10; //# sourceMappingURL=Spinner.js.map