Node.js library for colorizing text using ANSI escape sequences
Clone or download

README.md

Colorette

CI Coverage

Colorette is a Node.js library for colorizing text using ANSI escape sequences.

Features

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)

Nest styles without breaking an existing open sequence.

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") //=> \u001b[31mRed Alert\u001b[39m

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 normal and 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

To chalk or not to chalk?

Chalk is another Node.js terminal colorizer. Its core feature is a prototype-based chainable API where ANSI styles can be used both as properties or methods. Colorette doesn't rely on a domain-specific abstraction, allowing you to write purely functional, idiomatic JavaScript if you don't mind a few more sticking parentheses.

License

Colorette is MIT licensed. See the LICENSE for details.