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 kleur/colors API provides individual color functions that are fully tree-shakeable, allowing you to import only the colors you need for minimal bundle size. Functions cannot be chained but can be nested for complex styling.
type Colorize = {
(input: string | boolean | number): string;
(input: undefined | void): undefined;
(input: null): null;
};
// Configuration object
declare const $: { enabled: boolean };Individual functions for applying foreground colors.
declare const black: Colorize;
declare const red: Colorize;
declare const green: Colorize;
declare const yellow: Colorize;
declare const blue: Colorize;
declare const magenta: Colorize;
declare const cyan: Colorize;
declare const white: Colorize;
declare const gray: Colorize;
declare const grey: Colorize;import { red, green, yellow } from "kleur/colors";
// Direct application
console.log(red("Error message"));
console.log(green("Success message"));
console.log(yellow("Warning message"));
// Handle different input types
console.log(red(404)); // "404" (number converted to string)
console.log(red(true)); // "true" (boolean converted to string)
console.log(red(null)); // null (preserved)
console.log(red(undefined)); // undefined (preserved)Individual functions for applying background colors.
declare const bgBlack: Colorize;
declare const bgRed: Colorize;
declare const bgGreen: Colorize;
declare const bgYellow: Colorize;
declare const bgBlue: Colorize;
declare const bgMagenta: Colorize;
declare const bgCyan: Colorize;
declare const bgWhite: Colorize;import { bgRed, bgGreen, white } from "kleur/colors";
// Background colors
console.log(bgRed("Alert"));
console.log(bgGreen("Success"));
// Combine with text colors using nesting
console.log(bgRed(white("High contrast text")));Individual functions for applying text formatting styles.
declare const reset: Colorize; // Reset all styling
declare const bold: Colorize; // Bold text
declare const dim: Colorize; // Dim/faint text
declare const italic: Colorize; // Italic text (not widely supported)
declare const underline: Colorize; // Underlined text
declare const inverse: Colorize; // Inverse/reverse colors
declare const hidden: Colorize; // Hidden text
declare const strikethrough: Colorize; // Strikethrough text (not widely supported)import { bold, underline, dim } from "kleur/colors";
// Text modifiers
console.log(bold("Bold text"));
console.log(underline("Underlined text"));
console.log(dim("Dimmed text"));Combine multiple colors and modifiers using nested function calls.
import { red, bold, underline, bgWhite } from "kleur/colors";
// Simple nesting
console.log(bold(red("Bold red text")));
console.log(underline(red("Underlined red text")));
// Complex nesting
console.log(bgWhite(underline(red("Red underlined text with white background"))));
// Multiple combinations
console.log(bold(underline(red("Bold, underlined, red text"))));Create strings with multiple colors by concatenating function results.
import { red, green, yellow, blue, bold } from "kleur/colors";
// Concatenation
console.log(red("Error: ") + "Something went " + bold("wrong"));
// Template literals
const status = "active";
const count = 5;
console.log(`Status: ${green(status)}, Users: ${blue(count)}`);
// Complex messages
console.log(
yellow("Warning: ") +
"Found " +
red(bold("3")) +
" critical issues and " +
yellow("12") +
" warnings"
);Import only the colors you need to minimize bundle size.
// Import only what you need
import { red, bold } from "kleur/colors";
// Instead of importing everything
// import kleur from "kleur"; // This imports all colors
// Your bundler will only include red() and bold() functions
console.log(red("Error"));
console.log(bold("Important"));Control color output for the colors API.
// Configuration object
declare const $: { enabled: boolean };import { $, red, green } from "kleur/colors";
// Check current color support
console.log("Colors enabled:", $.enabled);
// Manually disable colors
$.enabled = false;
console.log(red("This won't be colored"));
// Re-enable colors
$.enabled = true;
console.log(red("This will be colored"));
// Conditional coloring
$.enabled = require("color-support").level > 0;Different ways to import the colors API.
// Named imports (recommended)
import { red, bold, bgBlue } from "kleur/colors";
// Namespace import
import * as colors from "kleur/colors";
console.log(colors.red("Red text"));
console.log(colors.$.enabled);
// CommonJS named destructuring
const { red, bold, bgBlue } = require("kleur/colors");
// CommonJS namespace
const colors = require("kleur/colors");
console.log(colors.red("Red text"));