Skip to content

Commit

Permalink
Merge pull request #2 from HarryPeach/dev
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
HarryPeach authored May 22, 2020
2 parents efdb5b9 + f68e562 commit d711748
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 55 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [dev]
pull_request:
branches: [master]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Setup Deno environment
uses: denolib/[email protected]

- name: Run Deno Tests
run: deno test -A
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

> Simple terminal spinners for Deno 🦕
`Version 0.2.0`
`Version 0.3.0`

![](https://github.com/HarryPeach/kia/workflows/CI/badge.svg)

![weather.ts example](https://user-images.githubusercontent.com/4750998/81313185-710ac900-907f-11ea-9735-d623559d08f6.gif)

Expand All @@ -12,18 +14,20 @@
## Usage

```typescript
import Kia from "./mod.ts";
import Kia from "https://deno.land/x/[email protected]/mod.ts";

const kia = new Kia("Hello");
await kia.start();
kia.start();
// Some async action that'll take some time
await kia.success("Action completed");
kia.success("Action completed");
```

More thorough examples are available in the [examples folder](https://github.com/HarryPeach/kia/tree/master/examples)

## API

### kia()

### kia(text)

### kia(options)
Expand Down Expand Up @@ -94,6 +98,14 @@ Default: false

Whether or not to display a cursor when the spinner is active

##### writer

Type: Deno.Writer

Default: Deno.stdout

The resource to output to. This can be anything that uses the Writer interface including stdout, stderr, and files.

### Instance

#### .start(text?)
Expand All @@ -104,13 +116,15 @@ Starts the spinner. Optionally sets the text at the same time. Returns Kia insta

Stops the spinner and clears the line. Returns Kia instance.

#### .set(text)

#### .set(options)

Allows you to change the spinners options. Returns Kia instance.

```typescript
const kia = new Kia("Hello");
await kia.set({ text: "Goodbye", color: "Red" });
kia.set({ text: "Goodbye", color: "Red" });
```

#### .succeed(text?)
Expand All @@ -127,12 +141,20 @@ Stops the spinner, and returns a message with the current text or the provided `

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.

#### .stopAndPersist(options)

Stops the spinner and holds it in a static state. Returns the instance.

#### .renderNextFrame()

Renders the next frame of the spinner when it is stopped (i.e. can only be run after .stopAndPersist()).

### forPromise(action, text)

### forPromise(action, options)

```typescript
import { forPromise } from "./mod.ts";
import { forPromise } from "https://deno.land/x/[email protected]/mod.ts";

forPromise(
async () => {
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as Colors from "https://deno.land/std/fmt/colors.ts";
export * as Colors from "https://deno.land/std@0.52.0/fmt/colors.ts";
6 changes: 3 additions & 3 deletions examples/externalSpinners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ function sleep(ms: number) {

// Use the spinner "flip"
const kia = new Kia({ text: "A spinner", spinner: SPINNERS.flip });
await kia.start();
kia.start();
await sleep(2000);

// Switch the spinner to "dots8"
await kia.set({ text: "Another spinner!", spinner: SPINNERS.dots8 });
kia.set({ text: "Another spinner!", spinner: SPINNERS.dots8 });
await sleep(2000);
await kia.stop();
kia.stop();
13 changes: 7 additions & 6 deletions examples/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,34 @@ function sleep(ms: number) {
const kia = new Kia({
text: "Loading sun",
color: "cyan",
spinner: Spinners.windows,
indent: 2,
spinner: Spinners.dots,
});

// Start the spinner
kia.start();
await sleep(2000);

// Change the spinner options
await kia.set({ text: "Loading some more" });
kia.set({ text: "Loading some more" });
await sleep(1000);
// Finish spinning successfully
await kia.succeed("Loaded sun");
kia.succeed("Loaded sun");

kia.start("Loading clouds");
await sleep(2000);
// Finish spinning with a warning
await kia.warn("Some clouds loaded");
kia.warn("Some clouds loaded");

kia.start("Getting the temperature");
await sleep(2000);
// Finish spinning with an info message
await kia.info("Nice and warm!");
kia.info("Nice and warm!");

kia.start("Loading rain");
await sleep(2000);
// Finish spinning with a failure message
await kia.fail("Rain could not be loaded!");
kia.fail("Rain could not be loaded!");
// Since success, fail, warn, and info are all wrappers around stopWithFlair: you could also do this manually like so:
// import {bold, red} from "https://deno.land/std/fmt/colors.ts";
// await kia.stopWithFlair(bold(red("X")), "Rain could not be loaded");
96 changes: 96 additions & 0 deletions kia.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Kia from "./mod.ts";
import {
assertThrowsAsync,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { expect } from "https://deno.land/x/expect/mod.ts";
class TestWriter implements Deno.WriterSync {
buffer: string[] = [];
writeSync(p: Uint8Array): number {
this.buffer.push(new TextDecoder().decode(p));
return p.length;
}
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

Deno.test("spinner isSpinning when running", () => {
const kia = new Kia();
kia.start();
expect(kia.isSpinning()).toEqual(true);
kia.stop();
});

Deno.test("spinner !isSpinning when not running", () => {
const kia = new Kia().start();
kia.stop();
expect(kia.isSpinning()).toEqual(false);
});

Deno.test("stopAndPersist stops the spinner output", async () => {
const testWriter = new TestWriter();
const kia = new Kia({ text: "", writer: testWriter }).start();
kia?.stopAndPersist();

// Wait and check that there are no extra prints
const sizeAfterStop = testWriter.buffer.length;
await sleep(1000);
expect(kia?.isSpinning()).toEqual(false);
expect(sizeAfterStop).toEqual(testWriter.buffer.length);
});

Deno.test("renderNextFrame() advances the spinner", () => {
const testWriter = new TestWriter();
const kia = new Kia({
text: "",
writer: testWriter,
}).start();
kia.stopAndPersist();

const sizeAfterStop = testWriter.buffer.length;
kia.renderNextFrame();

// Check that the frame is advancing
const sizeAfterNextStop = testWriter.buffer.length;
expect(sizeAfterStop).toBeLessThan(sizeAfterNextStop);

// Check that each frame is only advancing once
kia.renderNextFrame();
expect(sizeAfterNextStop - sizeAfterStop).toEqual(
testWriter.buffer.length - sizeAfterNextStop
);
});

Deno.test("check renderNextFrame can't be called if spinner is running", () => {
const kia = new Kia().start();
assertThrows(() => {
kia.renderNextFrame();
}, Error);
kia.stop();
});

Deno.test("set() changes the kia options", () => {
const testWriter = new TestWriter();
const SEARCH_KEY = "XXX";

const kia = new Kia({
text: "sample",
writer: testWriter,
}).start();

// Change the text to the search key and then check if it has actually changed
kia.stopAndPersist();
kia.set({ text: SEARCH_KEY });
kia.renderNextFrame();

let inArray = false;
testWriter.buffer.forEach((item) => {
if (item.includes(SEARCH_KEY)) {
inArray = true;
}
});

expect(inArray).toBe(true);
});
Loading

0 comments on commit d711748

Please sign in to comment.