Custom styling, theming, and instance creation capabilities for extending ANSI Colors with custom colors, aliases, and independent instances.
Create custom color aliases that reference existing colors or functions.
/**
* Create a custom color alias
* @param name - The name for the new alias
* @param style - Either a string referencing an existing color/style or a StyleFunction
* @throws TypeError if style is not a valid color name or function
*/
function alias(name: string, style: string | StyleFunction): void;Apply multiple custom color aliases from a theme object.
/**
* Apply a theme object containing multiple color aliases
* @param themeObject - Object mapping alias names to color names or functions
* @returns The colors instance for chaining
* @throws TypeError if themeObject is not an object
*/
function theme(themeObject: Record<string, string | StyleFunction>): typeof colors;Create independent instances of ANSI Colors with separate configuration.
/**
* Create a new independent instance of ansi-colors
* Each instance has its own enabled/visible settings and can have different themes
* @returns New ANSI Colors instance with default settings
*/
function create(): typeof colors;Usage Examples:
const colors = require('ansi-colors');
// Create custom aliases
colors.alias('primary', colors.yellow);
colors.alias('danger', colors.red);
colors.alias('success', 'green'); // String reference
console.log(colors.primary('Primary message'));
console.log(colors.danger('Danger message'));
console.log(colors.success('Success message'));
// Chaining works with aliases
console.log(colors.primary.bold('Bold primary message'));
// Apply themes
colors.theme({
error: 'red',
warning: 'yellow',
info: 'blue',
debug: colors.gray,
highlight: colors.bold.cyan
});
console.log(colors.error('Error occurred'));
console.log(colors.warning('Warning message'));
console.log(colors.info('Information'));
console.log(colors.debug('Debug output'));
console.log(colors.highlight('Important highlight'));
// Nested theme references
colors.theme({
primary: 'blue',
secondary: 'primary', // References the 'primary' alias
tertiary: 'secondary' // References the 'secondary' alias
});
console.log(colors.secondary('Secondary color')); // Will be blue
console.log(colors.tertiary('Tertiary color')); // Will also be blue
// Create independent instances
const logger = colors.create();
const debugger = colors.create();
// Configure instances independently
logger.enabled = true;
debugger.enabled = false;
logger.theme({
error: 'red',
info: 'blue'
});
debugger.theme({
error: 'magenta',
info: 'cyan'
});
console.log(logger.error('Logger error')); // Red text
console.log(debugger.error('Debugger error')); // Plain text (disabled)
// Dynamic alias creation
const logLevels = {
fatal: colors.bold.red,
error: colors.red,
warn: colors.yellow,
info: colors.blue,
debug: colors.gray,
trace: colors.dim
};
colors.theme(logLevels);
// Usage in logging system
function log(level, message) {
if (colors[level]) {
console.log(colors[level](`[${level.toUpperCase()}] ${message}`));
}
}
log('error', 'Something went wrong');
log('info', 'Process completed');
log('debug', 'Debug information');
// Reassigning aliases
colors.alias('error', colors.magenta); // Change error from red to magenta
console.log(colors.error('Now magenta error'));
// Using functions as aliases
colors.alias('timestamp', (text) => colors.gray(`[${new Date().toISOString()}] ${text}`));
console.log(colors.timestamp('Timestamped message'));