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 2, 2025
1 parent 984bb7c commit 04220e7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
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
26 changes: 13 additions & 13 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 "./colorTypes.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;
/**
* 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,15 +75,11 @@ 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;
}

this.#color = kAvailableColors.has(color!) ? color : "white";
}

get started() {
Expand Down Expand Up @@ -117,7 +117,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
55 changes: 55 additions & 0 deletions src/colorTypes.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 04220e7

Please sign in to comment.