diff --git a/README.md b/README.md index 23ef77b..fa6837d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ > Simple terminal spinners for Deno 🦕 -`Version 0.1.0` +`Version 0.2.0` ![weather.ts example](https://user-images.githubusercontent.com/4750998/81313185-710ac900-907f-11ea-9735-d623559d08f6.gif) @@ -98,15 +98,15 @@ Whether or not to display a cursor when the spinner is active #### .start(text?) -Starts the spinner. Optionally sets the text at the same time. +Starts the spinner. Optionally sets the text at the same time. Returns Kia instance. #### .stop() -Stops the spinner and clears the line. +Stops the spinner and clears the line. Returns Kia instance. #### .set(options) -Allows you to change the spinners options. +Allows you to change the spinners options. Returns Kia instance. ```typescript const kia = new Kia("Hello"); @@ -121,8 +121,18 @@ await kia.set({ text: "Goodbye", color: "Red" }); #### .info(text?) -Stops the spinner, and returns a message with the current text or the provided `text` as well as an icon indicating status. Wraps around `stopWithFlair()` +Stops the spinner, and returns a message with the current text or the provided `text` as well as an icon indicating status. Wraps around `stopWithFlair()`. Returns Kia instance. #### .stopWithFlair(text, flair) -Stops the spinner, and returns a message with the current text or the provided `text` as well as the preceding flair/icon. +Stops the spinner, and returns a message with the current text or the provided `text` as well as the preceding flair/icon. Returns Kia instance. + +### forPromise(action, text) + +### forPromise(action, options) + +Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the spinner instance. + +#### action + +Type: Promise diff --git a/examples/promise.ts b/examples/promise.ts new file mode 100644 index 0000000..7537587 --- /dev/null +++ b/examples/promise.ts @@ -0,0 +1,15 @@ +import Kia, { forPromise } from "../mod.ts"; + +// Just a function to async sleep +function sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +let name: string = "test"; + +forPromise( + async () => { + await sleep(4000); + }, + { text: name } +); diff --git a/kia.ts b/kia.ts index 7db99d9..47c993f 100644 --- a/kia.ts +++ b/kia.ts @@ -19,7 +19,7 @@ export interface Options { } type InputOptions = Partial; -export class Kia { +export default class Kia { private options: Options = { text: "", color: "white", @@ -45,6 +45,7 @@ export class Kia { }; } Object.assign(this.options, options); + return this; } /** @@ -57,13 +58,14 @@ export class Kia { if (text) await this.set(text); - if (!this.options.cursor) hideCursor(this.textEncoder); + if (!this.options.cursor) await hideCursor(this.textEncoder); this.timeoutRef = setInterval(async () => { this.currentFrame = (this.currentFrame + 1) % this.options.spinner.frames.length; await this.render(); }, this.options.spinner.interval); + return this; } /** @@ -72,7 +74,8 @@ export class Kia { async stop() { clearInterval(this.timeoutRef); await clearLine(this.textEncoder); - if (!this.options.cursor) showCursor(this.textEncoder); + if (!this.options.cursor) await showCursor(this.textEncoder); + return this; } /** @@ -81,16 +84,14 @@ export class Kia { * @param flair The icon to prepend the message */ async stopWithFlair(text: string = this.options.text, flair: string) { - // clearInterval(this.timeoutRef); - // await clearLine(this.textEncoder); await this.stop(); await writeLine( this.textEncoder, `${flair} ${text}`, this.options.indent ); - console.log(); this.spinning = false; + return this; } /** @@ -100,7 +101,7 @@ export class Kia { * @param text The message to be shown when stopped */ async succeed(text: string = this.options.text) { - await this.stopWithFlair(text, Colors.bold(Colors.green("√"))); + return await this.stopWithFlair(text, Colors.bold(Colors.green("√"))); } /** @@ -110,7 +111,7 @@ export class Kia { * @param text The message to be shown when stopped */ async fail(text: string = this.options.text) { - await this.stopWithFlair(text, Colors.bold(Colors.red("X"))); + return await this.stopWithFlair(text, Colors.bold(Colors.red("X"))); } /** @@ -120,7 +121,7 @@ export class Kia { * @param text The message to be shown when stopped */ async warn(text: string = this.options.text) { - await this.stopWithFlair(text, Colors.bold(Colors.yellow("!"))); + return await this.stopWithFlair(text, Colors.bold(Colors.yellow("!"))); } /** @@ -130,7 +131,7 @@ export class Kia { * @param text The message to be shown when stopped */ async info(text: string = this.options.text) { - await this.stopWithFlair(text, Colors.bold(Colors.blue("i"))); + return await this.stopWithFlair(text, Colors.bold(Colors.blue("i"))); } /** @@ -153,3 +154,22 @@ export class Kia { ); } } + +/** + * Starts a spinner for a promise + */ +export const forPromise = (action: Function, options: InputOptions) => { + const kia = new Kia(options); + kia.start(); + + (async () => { + try { + await action(); + kia.succeed(); + } catch (_) { + kia.fail(); + } + })(); + + return kia; +}; diff --git a/mod.ts b/mod.ts index eb62b83..809ac20 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,6 @@ -import { Kia } from "./kia.ts"; +// import { Kia, Promise } from "./kia.ts"; +import Kia, { forPromise } from "./kia.ts"; import { Spinners } from "./spinners.ts"; -export { Spinners }; +export { Spinners, forPromise }; export default Kia;