From 7ddb7a33862059370ea208f98c2b036d26c7b224 Mon Sep 17 00:00:00 2001 From: Qu4k Date: Tue, 25 Aug 2020 22:26:29 +0200 Subject: [PATCH] fix: reset animation when spinner is changed --- examples/hello.ts | 35 ++++++++++++++++++++++++++--------- mod.ts | 2 ++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/hello.ts b/examples/hello.ts index bb9b5aa..e72a3db 100644 --- a/examples/hello.ts +++ b/examples/hello.ts @@ -1,13 +1,30 @@ import { wait } from "../mod.ts"; +import spinners from "../spinners.ts"; -const spinner = wait("Loading mesozoic").start(); +const spinner = wait("Loading something really really heavy").start(); -setTimeout(() => { - spinner.color = "yellow"; - spinner.spinner = "shark"; - spinner.text = "Loading meteorite"; -}, 1000); +let colorIdx = 0; +const colors: string[] = [ + "black", + "red", + "green", + "yellow", + "blue", + "magenta", + "cyan", + "white", + "gray", +]; + +let spinIdx = 0; +const spins = Object.keys(spinners); -setTimeout(() => { - spinner.succeed("Started human race"); -}, 2000); +setInterval(() => { + colorIdx = ++colorIdx % colors.length; + spinner.color = colors[colorIdx]; +}, 500); + +setInterval(() => { + spinIdx = ++spinIdx % spins.length; + spinner.spinner = spins[spinIdx]; +}, 1000); diff --git a/mod.ts b/mod.ts index 769a830..a2a8734 100644 --- a/mod.ts +++ b/mod.ts @@ -4,6 +4,7 @@ import spinners from "./spinners.ts"; import { symbols } from "./log_symbols.ts"; import { onExit } from "https://deno.land/x/exit@0.0.1/mod.ts"; + type ColorFunction = (message: string) => string; const colormap: { [key: string]: ColorFunction } = { black: colors.black, @@ -104,6 +105,7 @@ class Spinner { #prefix: string = ""; set spinner(spin: string | SpinnerAnimation) { + this.#frameIndex = 0; if (Deno.build.os === "windows") this.#spinner = spinners.line; else if (typeof spin === "string") this.#spinner = spinners[spin]; else this.#spinner = spin;