Node.js library for colorizing text using ANSI escape sequences.
JavaScript TypeScript
Clone or download
Latest commit d03a7b9 Jul 20, 2018

README.md

Turbocolor

Codecov

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

Features

Installation

npm i turbocolor

Usage

const tc = require("turbocolor")

Writing with color.

console.log(tc.red("Bonjour!"))

Chaining styles.

console.log(tc.red.bold("Turbo") + tc.bgRed.white("Color"))

Using template literals.

console.log(`
  ${tc.bold("Score")}: ${100}
  Lives: ${tc.red.inverse(1)}
  Level: ${tc.bgCyan.black.inverse(2)}
`)

Nesting styles.

console.log(`Normal ${tc.bold(`Bold ${tc.red("Bold/Red")} Bold`)} Normal`)

Using string substitution.

console.log(tc.green("Total: $%f"), 1.99)

Styles

Every style function can be chained or nested with one another and will return a string when invoked.

Colors Background Colors Modifiers
black bgBlack dim
red bgRed bold
green bgGreen hidden
yellow bgYellow italic
blue bgBlue underline
magenta bgMagenta inverse
cyan bgCyan strikethrough
white bgWhite reset
gray

Color Support

Color support is automatically enabled if your terminal supports it, but you can toggle it on/off as needed.

const tc = require("turbocolor")

tc.enabled = false

console.log(tc.red("No color!"))

Escape Codes

Turbocolor exports ANSI escape codes for each available style. Each has an open and close property which can be used for manually styling console output.

const { Styles } = require("turbocolor")

console.log(`${Styles.red.open}Red${Styles.red.close}`)

License

Turbocolor is MIT licensed. See LICENSE.