Skip to content

Commit

Permalink
fix: reset animation when spinner is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed Aug 25, 2020
1 parent d9f4d20 commit 7ddb7a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
35 changes: 26 additions & 9 deletions examples/hello.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7ddb7a3

Please sign in to comment.