What is colorette?
The colorette npm package is a Node.js library for colorizing text in the terminal. It provides a simple API to add colors and styles to console output, which can be useful for creating more readable and organized logs, command-line tools, and scripts.
What are colorette's main functionalities?
Text coloring
Colorette allows you to color text output in the terminal. You can use predefined color functions to wrap your text and make it appear in the specified color when logged to the console.
const { red, green, blue } = require('colorette');
console.log(red('This is red text'));
console.log(green('This is green text'));
console.log(blue('This is blue text'));
Text styling
In addition to coloring, colorette provides functions to apply various text styles such as bold, underline, and italic.
const { bold, underline, italic } = require('colorette');
console.log(bold('This is bold text'));
console.log(underline('This is underlined text'));
console.log(italic('This is italic text'));
Combining styles
Colorette allows you to combine multiple styles and colors for a single piece of text. This can be useful for highlighting important information or creating visually distinct messages in the terminal.
const { red, bold, bgGreen } = require('colorette');
console.log(bold(red(bgGreen('Red text on green background'))));
Other packages similar to colorette
chalk
Chalk is a popular npm package similar to colorette that allows for styling and coloring terminal text. It offers a chainable API and additional color options. Chalk is known for its ease of use and extensive customization capabilities, but it might be slightly larger in size compared to colorette.
ansi-colors
Ansi-colors is another npm package for coloring terminal text. It focuses on performance and is dependency-free. It provides a similar API to colorette but with a different implementation approach. Ansi-colors may offer better performance in certain scenarios.
kleur
Kleur is a lightweight alternative to colorette with zero dependencies. It offers a similar API for text coloring and formatting. Kleur is designed for performance and has a minimalistic approach, which might make it faster in some cases.
Colorette
Colorette is a Node.js library for colorizing text using ANSI escape sequences. It's small and faster than alternatives. Each available style is a function you can use to wrap your strings in an escape sequence without prototype-chains or learning a domain specific language. Color your terminal using purely functional, idiomatic JavaScript.
Installation
npm i colorette
Usage
Import the style functions you want to use.
const { red, blue, bold } = require("colorette")
Then use them to colorize your output.
console.log(bold(blue("Engage!")))
Mix it with template literals.
console.log(`
Beets are ${red("red")},
Plums are ${blue("blue")},
${bold("Colorette!")}.
`)
Use console.log's string substitution.
console.log(bold("Total: $%f"), 1.99)
You can also nest styles without breaking existing escape sequences.
console.log(red(`Red Shirt ${blue("Blue Shirt")} Red Shirt`))
Feeling adventurous? Try the pipeline operator.
console.log("Make it so!" |> bold |> blue)
API
style(string)
A style function returns its string argument wrapped in the corresponding ANSI escape sequence.
red("Red Alert")
options.enabled
Color support is automatically enabled if your terminal supports it, but you can toggle it on/off as needed.
const { options } = require("colorette")
options.enabled = false
Styles
Colorette supports the standard bright color variations.
Colors | Background Colors | Bright Colors | Bright Background Colors | Modifiers |
---|
black | bgBlack | blackBright | bgBlackBright | dim |
red | bgRed | redBright | bgRedBright | bold |
green | bgGreen | greenBright | bgGreenBright | hidden |
yellow | bgYellow | yellowBright | bgYellowBright | italic |
blue | bgBlue | blueBright | bgBlueBright | underline |
magenta | bgMagenta | magentaBright | bgMagentaBright | strikethrough |
cyan | bgCyan | cyanBright | bgCyanBright | reset |
white | bgWhite | whiteBright | bgWhiteBright | |
gray | | | | |
Benchmark Results
All tests run on a 2.4GHz Intel Core i7 CPU with 16 GB memory.
npm i -C bench && node bench
# Using Styles
chalk × 8,935 ops/sec
kleur × 297,289 ops/sec
colors × 75,594 ops/sec
ansi-colors × 166,105 ops/sec
colorette × 725,926 ops/sec
# Combining Styles
chalk × 28,834 ops/sec
kleur × 764,202 ops/sec
colors × 263,517 ops/sec
ansi-colors × 304,108 ops/sec
colorette × 2,047,564 ops/sec
# Nesting Styles
chalk × 24,584 ops/sec
kleur × 294,766 ops/sec
colors × 143,747 ops/sec
ansi-colors × 208,175 ops/sec
colorette × 388,440 ops/sec
License
MIT