Skip to content

Commit

Permalink
feat: remove kleur dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fabnguess committed Jan 3, 2025
1 parent 984bb7c commit 5fd2109
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 14 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,62 @@ spinner.succeed("All done !");
Create a new Spinner. The **options** payload is described by the following TypeScript interface:

```ts
type ForegroundColors =
| "black"
| "blackBright"
| "blue"
| "blueBright"
| "cyan"
| "cyanBright"
| "gray"
| "green"
| "greenBright"
| "grey"
| "magenta"
| "magentaBright"
| "red"
| "redBright"
| "white"
| "whiteBright"
| "yellow"
| "yellowBright";

type BackgroundColors =
| "bgBlack"
| "bgBlackBright"
| "bgBlue"
| "bgBlueBright"
| "bgCyan"
| "bgCyanBright"
| "bgGray"
| "bgGreen"
| "bgGreenBright"
| "bgGrey"
| "bgMagenta"
| "bgMagentaBright"
| "bgRed"
| "bgRedBright"
| "bgWhite"
| "bgWhiteBright"
| "bgYellow"
| "bgYellowBright";

type Modifiers =
| "blink"
| "bold"
| "dim"
| "doubleunderline"
| "framed"
| "hidden"
| "inverse"
| "italic"
| "overlined"
| "reset"
| "strikethrough"
| "underline";

export type Color = ForegroundColors | BackgroundColors | Modifiers

export interface ISpinnerOptions {
/**
* Spinner name (from cli-spinners lib)
Expand All @@ -95,7 +151,7 @@ export interface ISpinnerOptions {
*
* @default "white"
*/
color?: string;
color?: Color;
/**
* Do not log anything when disabled
*
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"ansi-regex": "^6.0.1",
"cli-cursor": "^5.0.0",
"cli-spinners": "^3.1.0",
"kleur": "^4.1.5",
"strip-ansi": "^7.1.0"
},
"devDependencies": {
Expand Down
28 changes: 16 additions & 12 deletions src/Spinner.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Import Node.js Dependencies
import { EventEmitter } from "node:events";
import { performance } from "node:perf_hooks";
import { inspect, styleText } from "node:util";
import readline from "node:readline";
import * as TTY from "node:tty";

Expand All @@ -9,16 +10,19 @@ import * as cliSpinners from "cli-spinners";
import stripAnsi from "strip-ansi";
import ansiRegex from "ansi-regex";
import wcwidth from "@topcli/wcwidth";
import kleur from "kleur";

// Import Internal Dependencies
import type { Color } from "./types.js";

// VARS
let internalSpinnerCount = 0;

// CONSTANTS
const kDefaultSpinnerName = "dots" satisfies cliSpinners.SpinnerName;
const kLogSymbols = process.platform !== "win32" || process.env.CI || process.env.TERM === "xterm-256color" ?
{ success: kleur.bold().green("✔"), error: kleur.bold().red("✖") } :
{ success: kleur.bold().green("√"), error: kleur.bold().red("×") };
{ success: styleText(["green", "bold"], "✔"), error: styleText(["red", "bold"], "✖") } :
{ success: styleText(["green", "bold"], "✔"), error: styleText(["red", "bold"], "✖") };
const kAvailableColors = new Set(Object.keys(inspect.colors));

export interface ISpinnerOptions {
/**
Expand All @@ -32,7 +36,7 @@ export interface ISpinnerOptions {
*
* @default "white"
*/
color?: string;
color?: Color | Color[];
/**
* Do not log anything when disabled
*
Expand All @@ -57,7 +61,7 @@ export class Spinner extends EventEmitter {
#spinner: cliSpinners.Spinner;
#text = "";
#prefix = "";
#color: (stdout: string) => string;
#color: Color;

#interval: NodeJS.Timeout | null = null;
#frameIndex = 0;
Expand All @@ -71,14 +75,14 @@ export class Spinner extends EventEmitter {
return;
}

const { name = kDefaultSpinnerName, color = null } = options;
const { name = kDefaultSpinnerName, color = "white" } = options;

this.#spinner = name in cliSpinners ? cliSpinners[name] : cliSpinners[kDefaultSpinnerName];
if (color === null) {
this.#color = (str: string) => str;
}
else {
this.#color = color in kleur ? kleur[color] : kleur.white;

const colors = Array.isArray(color) ? color : [color];

if (colors.every((color) => kAvailableColors.has(color)) === false) {
throw new Error("Invalid color given");
}
}

Expand Down Expand Up @@ -117,7 +121,7 @@ export class Spinner extends EventEmitter {
const frame = frames[this.#frameIndex];
this.#frameIndex = ++this.#frameIndex < frames.length ? this.#frameIndex : 0;

return this.#color(frame);
return styleText(this.#color, frame);
}

#lineToRender(spinnerSymbol?: string) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Spinner.class.js";
export * from "./computeWithSpinner.js";
export type { Color } from "./types.js";
55 changes: 55 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
type ForegroundColors =
| "black"
| "blackBright"
| "blue"
| "blueBright"
| "cyan"
| "cyanBright"
| "gray"
| "green"
| "greenBright"
| "grey"
| "magenta"
| "magentaBright"
| "red"
| "redBright"
| "white"
| "whiteBright"
| "yellow"
| "yellowBright";

type BackgroundColors =
| "bgBlack"
| "bgBlackBright"
| "bgBlue"
| "bgBlueBright"
| "bgCyan"
| "bgCyanBright"
| "bgGray"
| "bgGreen"
| "bgGreenBright"
| "bgGrey"
| "bgMagenta"
| "bgMagentaBright"
| "bgRed"
| "bgRedBright"
| "bgWhite"
| "bgWhiteBright"
| "bgYellow"
| "bgYellowBright";

type Modifiers =
| "blink"
| "bold"
| "dim"
| "doubleunderline"
| "framed"
| "hidden"
| "inverse"
| "italic"
| "overlined"
| "reset"
| "strikethrough"
| "underline";

export type Color = ForegroundColors | BackgroundColors | Modifiers;

0 comments on commit 5fd2109

Please sign in to comment.