oli-colors
v4.6.2
Published
lets you use terminal colors esser
Readme
Features
Chainable API: Combine multiple styles and colors (e.g.,
colors.bold.red.bgYellow('text')).Foreground Colors: A wide range of standard and bright foreground colors.
Background Colors: A wide range of standard and bright background colors.
Text Effects: Apply effects like bold, dim, italic, underline, strikethrough, and more.
Automatic Reset: Each styled string automatically resets the console style at its end.
Usage
1. Import the Utility
First, you need to import the oli-colors utility into your Node.js script:
const colors = require('oli-colors'); // Adjust path if your file is named differently
2. Basic Usage
To apply a single color or effect, simply call the corresponding method on the colors object and pass your text as an argument:
console.log(colors.red('This text is red.'));
console.log(colors.green('Success message!'));
console.log(colors.bold('This text is bold.'));
console.log(colors.underline('This text is underlined.'));
3. Chainable Usage
The real power of oli-colors comes from its chainable API. You can chain multiple color and effect methods together to create complex styles. The order of chaining generally doesn't matter for colors, but effects might combine in specific ways.
console.log(colors.bold.yellow('Warning: Something important happened!'));
console.log(colors.italic.cyan('This is an italicized cyan message.'));
console.log(colors.bgBlue.white.bold('White text on a blue background, and bold!'));
console.log(colors.red.strikethrough('This text is red and crossed out.'));
console.log(colors.brightMagenta.underline('A bright magenta, underlined string.'));
4. Available Styles and Colors
Here's a comprehensive list of the available methods:
Text Effects
colors.reset('text')colors.bold('text')colors.dim('text')colors.italic('text')colors.underline('text')colors.blink('text')colors.inverse('text')(swaps foreground and background colors)colors.hidden('text')(makes text invisible)colors.strikethrough('text')
Foreground Colors
colors.black('text')colors.red('text')colors.green('text')colors.yellow('text')colors.blue('text')colors.magenta('text')colors.cyan('text')colors.white('text')colors.brightBlack('text')(often appears as gray)colors.brightRed('text')colors.brightGreen('text')colors.brightYellow('text')colors.brightBlue('text')colors.brightMagenta('text')colors.brightCyan('text')colors.brightWhite('text')
Background Colors
colors.bgBlack('text')colors.bgRed('text')colors.bgGreen('text')colors.bgYellow('text')colors.bgBlue('text')colors.bgMagenta('text')colors.bgCyan('text')colors.bgWhite('text')colors.bgBrightBlack('text')colors.bgBrightRed('text')colors.bgBrightGreen('text')colors.bgBrightYellow('text')colors.bgBrightBlue('text')colors.bgBrightMagenta('text')colors.bgBrightCyan('text')colors.bgBrightWhite('text')
Example Usage
Here's a more complete example demonstrating various styles:
const colors = require('oli-colors');
console.log(colors.green('Hello, world!'));
console.log(colors.bold.red('Error: Something went wrong!'));
console.log(colors.underline.cyan('This is a link or important note.'));
console.log(colors.bgYellow.black('Black text on a yellow background.'));
console.log(colors.brightBlue('This is a bright blue message.'));
console.log(colors.dim('This text is less prominent.'));
console.log(colors.italic.brightGreen.bgBlack('A stylish message with multiple effects!'));
console.log(colors.inverse('Inverted colors!'));
console.log(colors.hidden('You cannot see this text.')); // It's there, just hidden!
console.log(colors.strikethrough('This text is cancelled.'));
// You can also combine with regular strings
console.log(`This is a ${colors.red('red')} word and a ${colors.blue('blue')} word.`);
How it Works (Briefly)
The utility uses ANSI escape codes which are special sequences of characters that terminals interpret as commands to control formatting, color, and other output options. Each method (.red, .bold, etc.) appends the corresponding ANSI code before your text and an ANSI reset code at the end, ensuring that subsequent console output is not affected.
The chainable nature is achieved using a JavaScript Proxy object, which allows method calls like colors.bold.red to build up a list of ANSI codes before the final function call (('text')) applies them to the string.
