Lightweight, high-performance terminal string styling library that enables developers to add colors, text formatting, and visual effects to command-line interfaces.
npx @tessl/cli install tessl/npm-colorette@2.0.0Colorette is a lightweight, high-performance terminal string styling library that enables developers to add colors, text formatting, and visual effects to command-line interfaces. It provides comprehensive ANSI escape code support with automatic terminal color capability detection, zero dependencies, and performance optimization.
npm install coloretteimport { red, bold, blue, isColorSupported, createColors } from "colorette";For CommonJS:
const { red, bold, blue, isColorSupported, createColors } = require("colorette");Full namespace import:
import * as c from "colorette";import { blue, bold, underline, red, green } from "colorette";
// Basic text styling
console.log(red("Error: Something went wrong"));
console.log(green("Success: Operation completed"));
console.log(bold("Important message"));
// Chainable/nestable styling
console.log(bold(red("Critical Error")));
console.log(blue(`The ${underline("quick")} brown fox`));
// Complex nesting with proper escape sequence handling
console.log(bold(`I'm ${blue(`da ba ${underline("dee")} da ba`)} daa`));
// Custom color configuration
import { createColors } from "colorette";
const { blue: customBlue } = createColors({ useColor: false });
console.log(customBlue("This won't be colored"));Colorette is built around several key components:
createColors() function for creating custom color instances with overridden settingsCheck whether the current terminal environment supports colors.
/**
* Boolean constant indicating whether the current terminal supports colors
* Automatically detected based on environment variables, CLI flags, and terminal capabilities
*/
const isColorSupported: boolean;Create custom color instances with overridden color support settings.
/**
* Creates a colorette instance with custom color support configuration
* @param options - Configuration options
* @param options.useColor - Override automatic color detection
* @returns Object containing all color functions with applied configuration
*/
function createColors(options?: { useColor?: boolean }): Colorette;
interface Colorette {
reset: Color;
bold: Color;
dim: Color;
italic: Color;
underline: Color;
inverse: Color;
hidden: Color;
strikethrough: Color;
black: Color;
red: Color;
green: Color;
yellow: Color;
blue: Color;
magenta: Color;
cyan: Color;
white: Color;
gray: Color;
bgBlack: Color;
bgRed: Color;
bgGreen: Color;
bgYellow: Color;
bgBlue: Color;
bgMagenta: Color;
bgCyan: Color;
bgWhite: Color;
blackBright: Color;
redBright: Color;
greenBright: Color;
yellowBright: Color;
blueBright: Color;
magentaBright: Color;
cyanBright: Color;
whiteBright: Color;
bgBlackBright: Color;
bgRedBright: Color;
bgGreenBright: Color;
bgYellowBright: Color;
bgBlueBright: Color;
bgMagentaBright: Color;
bgCyanBright: Color;
bgWhiteBright: Color;
}Apply text formatting effects to strings.
/**
* Reset all formatting and colors
* @param text - Text to reset
* @returns Formatted string with reset codes
*/
const reset: Color;
/**
* Apply bold text styling
* @param text - Text to make bold
* @returns String with bold ANSI codes
*/
const bold: Color;
/**
* Apply dim/faint text styling
* @param text - Text to dim
* @returns String with dim ANSI codes
*/
const dim: Color;
/**
* Apply italic text styling
* @param text - Text to italicize
* @returns String with italic ANSI codes
*/
const italic: Color;
/**
* Apply underlined text styling
* @param text - Text to underline
* @returns String with underline ANSI codes
*/
const underline: Color;
/**
* Invert foreground and background colors
* @param text - Text to invert
* @returns String with inverse ANSI codes
*/
const inverse: Color;
/**
* Hide text (make invisible)
* @param text - Text to hide
* @returns String with hidden ANSI codes
*/
const hidden: Color;
/**
* Apply strikethrough text styling
* @param text - Text to strikethrough
* @returns String with strikethrough ANSI codes
*/
const strikethrough: Color;Apply foreground text colors.
/**
* Apply black text color
* @param text - Text to color
* @returns String with black color ANSI codes
*/
const black: Color;
/**
* Apply red text color
* @param text - Text to color
* @returns String with red color ANSI codes
*/
const red: Color;
/**
* Apply green text color
* @param text - Text to color
* @returns String with green color ANSI codes
*/
const green: Color;
/**
* Apply yellow text color
* @param text - Text to color
* @returns String with yellow color ANSI codes
*/
const yellow: Color;
/**
* Apply blue text color
* @param text - Text to color
* @returns String with blue color ANSI codes
*/
const blue: Color;
/**
* Apply magenta text color
* @param text - Text to color
* @returns String with magenta color ANSI codes
*/
const magenta: Color;
/**
* Apply cyan text color
* @param text - Text to color
* @returns String with cyan color ANSI codes
*/
const cyan: Color;
/**
* Apply white text color
* @param text - Text to color
* @returns String with white color ANSI codes
*/
const white: Color;
/**
* Apply gray text color
* @param text - Text to color
* @returns String with gray color ANSI codes
*/
const gray: Color;Apply background colors to text.
/**
* Apply black background color
* @param text - Text to apply background to
* @returns String with black background ANSI codes
*/
const bgBlack: Color;
/**
* Apply red background color
* @param text - Text to apply background to
* @returns String with red background ANSI codes
*/
const bgRed: Color;
/**
* Apply green background color
* @param text - Text to apply background to
* @returns String with green background ANSI codes
*/
const bgGreen: Color;
/**
* Apply yellow background color
* @param text - Text to apply background to
* @returns String with yellow background ANSI codes
*/
const bgYellow: Color;
/**
* Apply blue background color
* @param text - Text to apply background to
* @returns String with blue background ANSI codes
*/
const bgBlue: Color;
/**
* Apply magenta background color
* @param text - Text to apply background to
* @returns String with magenta background ANSI codes
*/
const bgMagenta: Color;
/**
* Apply cyan background color
* @param text - Text to apply background to
* @returns String with cyan background ANSI codes
*/
const bgCyan: Color;
/**
* Apply white background color
* @param text - Text to apply background to
* @returns String with white background ANSI codes
*/
const bgWhite: Color;Apply bright/intense foreground text colors.
/**
* Apply bright black text color
* @param text - Text to color
* @returns String with bright black color ANSI codes
*/
const blackBright: Color;
/**
* Apply bright red text color
* @param text - Text to color
* @returns String with bright red color ANSI codes
*/
const redBright: Color;
/**
* Apply bright green text color
* @param text - Text to color
* @returns String with bright green color ANSI codes
*/
const greenBright: Color;
/**
* Apply bright yellow text color
* @param text - Text to color
* @returns String with bright yellow color ANSI codes
*/
const yellowBright: Color;
/**
* Apply bright blue text color
* @param text - Text to color
* @returns String with bright blue color ANSI codes
*/
const blueBright: Color;
/**
* Apply bright magenta text color
* @param text - Text to color
* @returns String with bright magenta color ANSI codes
*/
const magentaBright: Color;
/**
* Apply bright cyan text color
* @param text - Text to color
* @returns String with bright cyan color ANSI codes
*/
const cyanBright: Color;
/**
* Apply bright white text color
* @param text - Text to color
* @returns String with bright white color ANSI codes
*/
const whiteBright: Color;Apply bright/intense background colors to text.
/**
* Apply bright black background color
* @param text - Text to apply background to
* @returns String with bright black background ANSI codes
*/
const bgBlackBright: Color;
/**
* Apply bright red background color
* @param text - Text to apply background to
* @returns String with bright red background ANSI codes
*/
const bgRedBright: Color;
/**
* Apply bright green background color
* @param text - Text to apply background to
* @returns String with bright green background ANSI codes
*/
const bgGreenBright: Color;
/**
* Apply bright yellow background color
* @param text - Text to apply background to
* @returns String with bright yellow background ANSI codes
*/
const bgYellowBright: Color;
/**
* Apply bright blue background color
* @param text - Text to apply background to
* @returns String with bright blue background ANSI codes
*/
const bgBlueBright: Color;
/**
* Apply bright magenta background color
* @param text - Text to apply background to
* @returns String with bright magenta background ANSI codes
*/
const bgMagentaBright: Color;
/**
* Apply bright cyan background color
* @param text - Text to apply background to
* @returns String with bright cyan background ANSI codes
*/
const bgCyanBright: Color;
/**
* Apply bright white background color
* @param text - Text to apply background to
* @returns String with bright white background ANSI codes
*/
const bgWhiteBright: Color;/**
* Color function type - all color and styling functions follow this signature
* @param text - String or number to style (numbers are converted to strings)
* @returns Styled string with ANSI codes (when colors supported) or plain string
*/
type Color = (text: string | number) => string;Colorette supports several environment variables and CLI flags for controlling color output:
Environment Variables:
NO_COLOR - Disable colors when set (any value)FORCE_COLOR - Force colors when set (any value)CI - Detected CI environments enable colors automatically (GitHub Actions, GitLab CI, CircleCI)TERM - Terminal type detection (avoids "dumb" terminals)CLI Flags:
--no-color - Disable colors--color - Force colorsProgrammatic Override:
createColors({ useColor: boolean }) to override detectionAll color functions accept both strings and numbers as input:
Colorette achieves high performance through several optimizations:
substring() and indexOf() for minimal memory allocationreplaceClose() function handles nested sequences without string rebuildingString constructor for zero processing cost