Core utility functions for color management, terminal detection, text processing, and theme customization.
Functions to enable, disable, and control color output globally.
/**
* Enable color output globally
* Sets colors.enabled to true, allowing all color functions to apply styling
*/
function enable(): void;
/**
* Disable color output globally
* Sets colors.enabled to false, making all color functions return plain text
*/
function disable(): void;
/**
* Global enabled state property
* Controls whether colors are applied or stripped from output
*/
var enabled: boolean;Functions for removing color codes and processing styled text.
/**
* Remove all ANSI color codes from a string
* @param str - String potentially containing ANSI escape sequences
* @returns Plain text with all ANSI codes removed
*/
function stripColors(str: string): string;
/**
* Alias for stripColors - remove all ANSI color codes from a string
* @param str - String potentially containing ANSI escape sequences
* @returns Plain text with all ANSI codes removed
*/
function strip(str: string): string;
/**
* Apply a specific style to text (internal styling function)
* @param str - Text to style
* @param style - Style name to apply
* @returns Styled text with ANSI codes
*/
function stylize(str: string, style: string): string;Functions for creating and applying custom color themes.
/**
* Set a custom theme with named color mappings
* @param theme - Object mapping theme names to color/style names or arrays
*/
function setTheme(theme: ThemeDefinition): void;
/**
* Theme storage object containing registered themes
*/
var themes: { [key: string]: any };
/**
* Style mapping objects containing ANSI codes for colors and effects
*/
var styles: { [key: string]: { open: string; close: string } };
/**
* Map objects for special effects (rainbow, zebra, america, random)
*/
var maps: { [key: string]: (char: string, index: number) => string };Functions and properties for detecting terminal color support.
/**
* Detect color support level for the current terminal
* @param stream - Optional stream to check (defaults to process.stdout)
* @returns Color support information or false if no support
*/
function supportsColor(stream?: NodeJS.WriteStream): ColorSupport | false;
/**
* Color support information for stdout stream
*/
var stdout: ColorSupport | false;
/**
* Color support information for stderr stream
*/
var stderr: ColorSupport | false;interface ThemeDefinition {
[themeName: string]: string | string[];
}
interface ColorSupport {
/** Color support level (0-3) */
level: number;
/** Basic 16 colors supported */
hasBasic: boolean;
/** 256 colors supported */
has256: boolean;
/** 16 million colors supported */
has16m: boolean;
}Color Control:
const colors = require('colors/safe');
// Disable colors globally
colors.disable();
console.log(colors.red('This will be plain text'));
// Re-enable colors
colors.enable();
console.log(colors.red('This will be red'));
// Check current state
console.log('Colors enabled:', colors.enabled);Text Processing:
const colors = require('colors/safe');
const coloredText = colors.red.bold('Important message');
console.log('With colors:', coloredText);
console.log('Plain text:', colors.strip(coloredText));
// Alternative strip function
console.log('Plain text:', colors.stripColors(coloredText));Custom Themes:
const colors = require('colors/safe');
// Define a logging theme
colors.setTheme({
error: 'red',
warning: 'yellow',
info: 'blue',
success: 'green',
debug: 'gray'
});
// Use themed colors
console.log(colors.error('Error message'));
console.log(colors.warning('Warning message'));
console.log(colors.info('Info message'));
console.log(colors.success('Success message'));
console.log(colors.debug('Debug message'));
// Complex theme with style combinations
colors.setTheme({
critical: ['red', 'bold', 'underline'],
highlight: ['yellow', 'inverse'],
subtle: ['dim', 'gray']
});
console.log(colors.critical('Critical alert'));
console.log(colors.highlight('Highlighted text'));
console.log(colors.subtle('Subtle information'));Terminal Detection:
const colors = require('colors/safe');
// Check color support
const support = colors.supportsColor();
if (support) {
console.log('Color support level:', support.level);
console.log('Basic colors:', support.hasBasic);
console.log('256 colors:', support.has256);
console.log('16M colors:', support.has16m);
} else {
console.log('No color support detected');
}
// Check specific streams
console.log('Stdout support:', colors.stdout);
console.log('Stderr support:', colors.stderr);File-based Themes:
const colors = require('colors/safe');
// Load theme from external file
try {
const loggingTheme = require('./themes/logging-theme.js');
colors.setTheme(loggingTheme);
} catch (err) {
console.log('Could not load theme:', err.message);
}When using the default import, strip functions are available as getters on String.prototype:
interface String {
strip: string;
stripColors: string;
}The color support detection system respects several environment variables:
FORCE_COLOR: Force color output regardless of terminal detectionNO_COLOR: Disable color outputTERM: Terminal type for capability detectionCOLORTERM: Color terminal indicatorCI: Continuous integration environment detection