Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorted Files #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/externalSpinners.ts → Examples/externalSpinners.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Kia from "../mod.ts";
import Kia from '../mod.ts';
// Import some spinners from cli-spinners by ameerthehacker
import { SPINNERS } from "https://raw.githubusercontent.com/ameerthehacker/cli-spinners/master/spinners.ts";
import { SPINNERS } from 'https://raw.githubusercontent.com/ameerthehacker/cli-spinners/master/spinners.ts';

// Just a function to async sleep
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

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

// Switch the spinner to "dots8"
kia.set({ text: "Another spinner!", spinner: SPINNERS.dots8 });
kia.set({ text: 'Another spinner!', spinner: SPINNERS.dots8 });
await sleep(2000);
kia.stop();
4 changes: 2 additions & 2 deletions examples/promise.ts → Examples/promise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { forPromise } from "../mod.ts";
import { forPromise } from '../mod.ts';

// Just a function to async sleep
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const text = "test";
const text = 'test';

forPromise(
async () => {
Expand Down
22 changes: 11 additions & 11 deletions examples/weather.ts → Examples/weather.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Kia, { Spinners } from "../mod.ts";
import Kia, { Spinners } from '../mod.ts';

// Just a function to async sleep
function sleep(ms: number) {
Expand All @@ -7,8 +7,8 @@ function sleep(ms: number) {

// Create a spinner instance
const kia = new Kia({
text: "Loading sun",
color: "cyan",
text: 'Loading sun',
color: 'cyan',
indent: 2,
spinner: Spinners.dots,
});
Expand All @@ -18,25 +18,25 @@ kia.start();
await sleep(2000);

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

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

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

kia.start("Loading rain");
kia.start('Loading rain');
await sleep(2000);
// Finish spinning with a failure message
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");
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
## Usage

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

// Just a function to async sleep
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const kia: any = new Kia("I will be back in about 3 seconds");
const kia: any = new Kia('I will be back in about 3 seconds');
kia.start();
await sleep(3000); // or any other async action that'll take some time
kia.succeed("Action completed");
kia.succeed('Action completed');
```

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

## API

Expand All @@ -44,11 +43,11 @@ Kia can be created with a string or the Options interface. A string is simply
mapped to `Options.text`

```typescript
const kia = new Kia("Hello");
const kia = new Kia('Hello');
// or
const kia = new Kia({
text: "Hello",
color: "Red",
text: 'Hello',
color: 'Red',
});
```

Expand All @@ -66,8 +65,7 @@ The text to display after the spinner

##### color

Type:
[Color](https://github.com/HarryPeach/kia/blob/8fb27cbd0bb4ef08ad26124d4a6e4f2ba2dc0c5c/util.ts#L6)
Type: [Color](Source/util.ts)

Default: "white"

Expand All @@ -76,8 +74,7 @@ the Deno standard colors.

##### spinner

Type:
[Spinner](https://github.com/HarryPeach/kia/blob/8fb27cbd0bb4ef08ad26124d4a6e4f2ba2dc0c5c/spinners.ts#L1)
Type: [Spinner](Source/spinners.ts)

Default: Dependent on OS (See below)

Expand Down Expand Up @@ -140,8 +137,8 @@ Stops the spinner and clears the line. Returns Kia instance.
Allows you to change the spinners options. Returns Kia instance.

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

#### .succeed(text?)
Expand Down Expand Up @@ -183,7 +180,7 @@ Returns the current spinner frame
### forPromise(action, options)

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

forPromise(
async () => {
Expand Down
4 changes: 4 additions & 0 deletions Source/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as Colors from 'https://deno.land/[email protected]/fmt/colors.ts';
export * from 'https://deno.land/[email protected]/testing/asserts.ts';
export * from 'https://deno.land/x/[email protected]/mod.ts';
export * from 'https://deno.land/[email protected]/streams/conversion.ts';
28 changes: 14 additions & 14 deletions kia.ts → Source/kia.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Spinner, Spinners } from "./spinners.ts";
import { Spinner, Spinners } from './spinners.ts';
import {
clearLine,
Color,
colorise,
hideCursor,
showCursor,
writeLine,
} from "./util.ts";
import { Colors } from "./deps.ts";
} from './util.ts';
import { Colors } from './deps.ts';

export interface Options {
text: string;
Expand All @@ -22,10 +22,10 @@ type InputOptions = Partial<Options>;

export default class Kia {
private options: Options = {
text: "",
color: "white",
spinner: Deno.build.os === "windows" ? Spinners.windows : Spinners.dots,
prefixText: "",
text: '',
color: 'white',
spinner: Deno.build.os === 'windows' ? Spinners.windows : Spinners.dots,
prefixText: '',
indent: 0,
cursor: false,
writer: Deno.stdout,
Expand All @@ -39,7 +39,7 @@ export default class Kia {

constructor(options?: InputOptions | string) {
if (!options) return;
if (typeof options === "string") {
if (typeof options === 'string') {
options = {
text: options,
};
Expand All @@ -49,7 +49,7 @@ export default class Kia {
}

public set(options: InputOptions | string) {
if (typeof options === "string") {
if (typeof options === 'string') {
options = {
text: options,
};
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class Kia {
renderNextFrame() {
if (this.spinning) {
throw new Error(
"You cannot manually render frames when the spinner is running, run stopAndPersist() first.",
'You cannot manually render frames when the spinner is running, run stopAndPersist() first.',
);
}
this.currentFrame = (this.currentFrame + 1) %
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class Kia {
text,
Colors.bold(
Colors.green(
Deno.build.os === "windows" ? String.fromCharCode(30) : "√",
Deno.build.os === 'windows' ? String.fromCharCode(30) : '√',
),
),
);
Expand All @@ -163,7 +163,7 @@ export default class Kia {
* @param text The message to be shown when stopped
*/
fail(text: string = this.options.text) {
return this.stopWithFlair(text, Colors.bold(Colors.red("X")));
return this.stopWithFlair(text, Colors.bold(Colors.red('X')));
}

/**
Expand All @@ -173,7 +173,7 @@ export default class Kia {
* @param text The message to be shown when stopped
*/
warn(text: string = this.options.text) {
return this.stopWithFlair(text, Colors.bold(Colors.yellow("!")));
return this.stopWithFlair(text, Colors.bold(Colors.yellow('!')));
}

/**
Expand All @@ -183,7 +183,7 @@ export default class Kia {
* @param text The message to be shown when stopped
*/
info(text: string = this.options.text) {
return this.stopWithFlair(text, Colors.bold(Colors.blue("i")));
return this.stopWithFlair(text, Colors.bold(Colors.blue('i')));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions spinners.ts → Source/spinners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export interface Spinner {
export const Spinners = {
windows: {
interval: 80,
frames: ["/", "-", "\\", "|"],
frames: ['/', '-', '\\', '|'],
},
dots: {
interval: 80,
frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
},
arc: {
interval: 100,
frames: ["◜", "◠", "◝", "◞", "◡", "◟"],
frames: ['◜', '◠', '◝', '◞', '◡', '◟'],
},
};
30 changes: 15 additions & 15 deletions util.ts → Source/util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Colors, writeAllSync } from "./deps.ts";
import { Colors, writeAllSync } from './deps.ts';

// Terminal escape sequences
const ESC = "\x1b[";
const ESC = '\x1b[';

/**
* The colors to be used with the Kia spinner
*/
export type Color =
| "black"
| "red"
| "green"
| "yellow"
| "blue"
| "magenta"
| "cyan"
| "white"
| "gray";
| 'black'
| 'red'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'cyan'
| 'white'
| 'gray';

/**
* Converts the Kia color type to the Deno color functions
Expand All @@ -38,27 +38,27 @@ export function writeLine(
) {
writeAllSync(
writer,
encoder.encode(`\r${indent ? ESC + indent + "C" : ""}${text}`),
encoder.encode(`\r${indent ? ESC + indent + 'C' : ''}${text}`),
);
}

/**
* Clears the line and performs a carriage return
*/
export function clearLine(writer: Deno.WriterSync, encoder: TextEncoder) {
writeAllSync(writer, encoder.encode(ESC + "2K\r"));
writeAllSync(writer, encoder.encode(ESC + '2K\r'));
}

/**
* Hides the terminal cursor
*/
export function hideCursor(writer: Deno.WriterSync, encoder: TextEncoder) {
writeAllSync(writer, encoder.encode(ESC + "?25l"));
writeAllSync(writer, encoder.encode(ESC + '?25l'));
}

/**
* Shows the terminal cursor
*/
export function showCursor(writer: Deno.WriterSync, encoder: TextEncoder) {
writeAllSync(writer, encoder.encode(ESC + "?25h"));
writeAllSync(writer, encoder.encode(ESC + '?25h'));
}
Loading