From 5fd2109c61ac111d6a095135fdfdca9c2babb8b0 Mon Sep 17 00:00:00 2001 From: Kouadio Fabrice Nguessan Date: Thu, 2 Jan 2025 15:52:05 +0000 Subject: [PATCH] feat: remove kleur dependency --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++- package.json | 1 - src/Spinner.class.ts | 28 ++++++++++++--------- src/index.ts | 1 + src/types.ts | 55 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 src/types.ts diff --git a/README.md b/README.md index 615fdf2..138a193 100644 --- a/README.md +++ b/README.md @@ -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) @@ -95,7 +151,7 @@ export interface ISpinnerOptions { * * @default "white" */ - color?: string; + color?: Color; /** * Do not log anything when disabled * diff --git a/package.json b/package.json index a8cab5f..27ccc7f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/Spinner.class.ts b/src/Spinner.class.ts index 31d8edd..e79f6a6 100644 --- a/src/Spinner.class.ts +++ b/src/Spinner.class.ts @@ -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"; @@ -9,7 +10,9 @@ 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; @@ -17,8 +20,9 @@ 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 { /** @@ -32,7 +36,7 @@ export interface ISpinnerOptions { * * @default "white" */ - color?: string; + color?: Color | Color[]; /** * Do not log anything when disabled * @@ -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; @@ -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"); } } @@ -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) { diff --git a/src/index.ts b/src/index.ts index 3ea7898..f97b3ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from "./Spinner.class.js"; export * from "./computeWithSpinner.js"; +export type { Color } from "./types.js"; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..a2c88cf --- /dev/null +++ b/src/types.ts @@ -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;