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
Easily set your terminal text color & styles.
- No wonky prototype method-chain API.
- Automatic color support detection.
- Up to 2x faster than alternatives.
NO_COLOR
friendly. ✅
👋 Upgrading from Colorette 1.x
?
Quickstart
Here's the first example to get you started.
import { blue, bold, underline } from "colorette"
console.log(
blue("I'm blue"),
bold(blue("da ba dee")),
underline(bold(blue("da ba daa")))
)
Here's an example using template literals.
console.log(`
There's a ${underline(blue("house"))},
With a ${bold(blue("window"))},
And a ${blue("corvette")}
And everything is blue
`)
Of course, you can nest styles without breaking existing color sequences.
console.log(bold(`I'm ${blue(`da ba ${underline("dee")} da ba`)} daa`))
Need to override automatic color detection? You can do that too.
import { createColors } from "colorette"
const { blue } = createColors({ useColor: false })
console.log(blue("Blue? Nope, nah"))
Installation
npm install colorette
API
blue(text)
See all supported colors.
blue("I'm blue")
isColorSupported
true
if your terminal supports color, false
otherwise. Used internally and handled for you, but exposed for convenience.
createColors({ useColor })
Create a reusable instance of Colorette. Color support is automatically detected, but you can override it by setting the useColor
boolean property.
import { createColors } from "colorette"
const { blue } = createColors({ useColor: false })
Environment
You can override automatic color detection from the CLI too via NO_COLOR=
or FORCE_COLOR=
.
$ FORCE_COLOR= node example.js | ./consumer.js
Supported colors
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 | | | | |
npm --prefix bench start
License
MIT