The fastest Node.js library for formatting terminal text with ANSI colors
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The main kleur API provides chainable methods for applying colors, backgrounds, and text modifiers. Each method can be called directly with text or chained together for complex styling combinations.
declare const kleur: Kleur & { enabled: boolean };
interface Kleur {
// Colors
black: Color;
red: Color;
green: Color;
yellow: Color;
blue: Color;
magenta: Color;
cyan: Color;
white: Color;
gray: Color;
grey: Color;
// Backgrounds
bgBlack: Color;
bgRed: Color;
bgGreen: Color;
bgYellow: Color;
bgBlue: Color;
bgMagenta: Color;
bgCyan: Color;
bgWhite: Color;
// Modifiers
reset: Color;
bold: Color;
dim: Color;
italic: Color;
underline: Color;
inverse: Color;
hidden: Color;
strikethrough: Color;
}
interface Color {
(input: string | number): string;
(): Kleur;
}Apply foreground colors to text.
kleur.black: Color;
kleur.red: Color;
kleur.green: Color;
kleur.yellow: Color;
kleur.blue: Color;
kleur.magenta: Color;
kleur.cyan: Color;
kleur.white: Color;
kleur.gray: Color;
kleur.grey: Color;import kleur from "kleur";
// Direct application
console.log(kleur.red("Error message"));
console.log(kleur.green("Success message"));
console.log(kleur.yellow("Warning message"));
// Numbers are automatically converted to strings
console.log(kleur.blue(404));
console.log(kleur.magenta(3.14159));Apply background colors to text.
kleur.bgBlack: Color;
kleur.bgRed: Color;
kleur.bgGreen: Color;
kleur.bgYellow: Color;
kleur.bgBlue: Color;
kleur.bgMagenta: Color;
kleur.bgCyan: Color;
kleur.bgWhite: Color;import kleur from "kleur";
// Background colors
console.log(kleur.bgRed("Alert"));
console.log(kleur.bgGreen("Success"));
// Combine with text colors
console.log(kleur.white().bgRed("High contrast text"));Apply various text formatting styles.
kleur.reset: Color; // Reset all styling
kleur.bold: Color; // Bold text
kleur.dim: Color; // Dim/faint text
kleur.italic: Color; // Italic text (not widely supported)
kleur.underline: Color; // Underlined text
kleur.inverse: Color; // Inverse/reverse colors
kleur.hidden: Color; // Hidden text
kleur.strikethrough: Color; // Strikethrough text (not widely supported)import kleur from "kleur";
// Text modifiers
console.log(kleur.bold("Bold text"));
console.log(kleur.underline("Underlined text"));
console.log(kleur.dim("Dimmed text"));
// Note: italic and strikethrough are not widely supported across terminals
console.log(kleur.italic("Italic text"));
console.log(kleur.strikethrough("Strikethrough text"));Chain multiple styles together for complex formatting.
import kleur from "kleur";
// Chain colors and modifiers
console.log(kleur.red().bold("Bold red text"));
console.log(kleur.white().bgBlue().underline("White text on blue background"));
// Complex chaining
console.log(kleur.yellow().bold().bgRed().italic("Multi-styled text"));
// Chain order doesn't matter for styling
console.log(kleur.bold().red("Same as"));
console.log(kleur.red().bold("this text"));Apply colors within already colored text using nested method calls.
import kleur from "kleur";
// Nested colors
console.log(kleur.red(`Error: ${kleur.yellow("warning")} occurred`));
// Complex nesting
console.log(kleur.bold(`
Status: ${kleur.green("OK")}
Errors: ${kleur.red().bold("3")}
Warnings: ${kleur.yellow("12")}
`));
// Template literals work well with nesting
const status = "connected";
const count = 42;
console.log(kleur.cyan(`Server ${kleur.green().bold(status)} with ${kleur.white().bgBlue(count)} users`));Control color output globally.
kleur.enabled: boolean;import kleur from "kleur";
// Check current color support
console.log("Colors enabled:", kleur.enabled);
// Manually disable colors
kleur.enabled = false;
console.log(kleur.red("This won't be colored"));
// Re-enable colors
kleur.enabled = true;
console.log(kleur.red("This will be colored"));
// Conditional coloring based on environment
kleur.enabled = process.env.NODE_ENV !== "test";