Skip to content

Commit

Permalink
Merge pull request #1 from HarryPeach/dev
Browse files Browse the repository at this point in the history
Version 0.2.0
  • Loading branch information
HarryPeach authored May 9, 2020
2 parents 07f459e + 9388bc6 commit 5fbacff
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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");
Expand All @@ -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
15 changes: 15 additions & 0 deletions examples/promise.ts
Original file line number Diff line number Diff line change
@@ -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 }
);
40 changes: 30 additions & 10 deletions kia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface Options {
}
type InputOptions = Partial<Options>;

export class Kia {
export default class Kia {
private options: Options = {
text: "",
color: "white",
Expand All @@ -45,6 +45,7 @@ export class Kia {
};
}
Object.assign(this.options, options);
return this;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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("√")));
}

/**
Expand All @@ -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")));
}

/**
Expand All @@ -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("!")));
}

/**
Expand All @@ -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")));
}

/**
Expand All @@ -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;
};
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 5fbacff

Please sign in to comment.