Global settings and utilities for controlling color behavior, detecting ANSI codes, and managing color output in different environments.
Control global color behavior and output settings.
/**
* Enable or disable color output globally
* When false, all styling functions return plain text without ANSI codes
* @default true (based on terminal capability detection)
*/
let enabled: boolean;
/**
* Control visibility of output
* When false, all styling functions return empty strings
* @default true
*/
let visible: boolean;Regular expression and utilities for detecting and working with ANSI escape codes.
/**
* Regular expression for matching ANSI escape sequences
* Used internally and available for custom ANSI code detection
*/
const ansiRegex: RegExp;
/**
* Internal styles object containing all style definitions
* Maps style names to their ANSI code configurations
* Available for advanced usage and introspection
*/
const styles: Record<string, StyleDefinition>;
/**
* Check if a string contains ANSI color codes
* @param str - The string to check for ANSI codes
* @returns true if string contains ANSI codes, false otherwise
*/
function hasColor(str: string): boolean;
/**
* Check if a string contains ANSI codes (alias for hasColor)
* @param str - The string to check for ANSI codes
* @returns true if string contains ANSI codes, false otherwise
*/
function hasAnsi(str: string): boolean;Functions to remove ANSI codes and styling from text.
/**
* Remove all ANSI color codes and styling from a string
* @param str - The string to clean
* @returns Plain text with all ANSI codes removed
*/
function stripColor(str: string): string;
/**
* Remove all ANSI codes from a string (alias for stripColor)
* @param str - The string to clean
* @returns Plain text with all ANSI codes removed
*/
function unstyle(str: string): string;Utility functions that return input unchanged.
/**
* No-operation function that returns input unchanged
* Useful for conditional styling or as a fallback
* @param str - The input string
* @returns The input string unchanged
*/
function noop(str: string): string;
/**
* Alias for noop function
* @param str - The input string
* @returns The input string unchanged
*/
const none: typeof noop;
/**
* Alias for noop function
* @param str - The input string
* @returns The input string unchanged
*/
const clear: typeof noop;Usage Examples:
const colors = require('ansi-colors');
// Enable/disable colors globally
colors.enabled = false;
console.log(colors.red('This will be plain text')); // Output: "This will be plain text"
colors.enabled = true;
console.log(colors.red('This will be red')); // Output: styled red text
// Control visibility
colors.visible = false;
console.log(colors.blue('This will be empty')); // Output: ""
colors.visible = true;
console.log(colors.blue('This will show')); // Output: styled blue text
// Detect ANSI codes
const styledText = colors.bold.red('Error message');
console.log(colors.hasColor(styledText)); // true
console.log(colors.hasAnsi(styledText)); // true (alias)
console.log(colors.hasColor('plain text')); // false
// Clean text
const cleanText = colors.stripColor(styledText);
console.log(cleanText); // "Error message" (no styling)
// Alternative cleaning
const unstyledText = colors.unstyle(styledText);
console.log(unstyledText); // "Error message" (no styling)
// No-operation functions
const conditionalStyle = process.env.NODE_ENV === 'production' ? colors.noop : colors.red;
console.log(conditionalStyle('Debug message'));
// Using aliases
console.log(colors.none('plain text')); // "plain text"
console.log(colors.clear('plain text')); // "plain text"
// Environment-based color detection
if (require('tty').isatty(process.stdout.fd)) {
colors.enabled = true;
} else {
colors.enabled = false;
}
// Using the ANSI regex directly
const customText = 'Hello \u001b[31mworld\u001b[39m!';
colors.ansiRegex.lastIndex = 0; // Reset regex state
console.log(colors.ansiRegex.test(customText)); // true