# Terminal Coloring

Easily add coloring to your text and symbols in the terminal. A faster drop-in replacement for chalk, kleur and turbocolor (without the dependencies and rendering bugs).

image

# Why use this?

Colors plugin is the one of the fastest, if not the fastest Node.js library for terminal styling. A more performant drop-in replacement for chalk, with no dependencies.

# Usage

const text = require("rovel.js").text;

console.log(text.red('This is a red string!'));
console.log(text.green('This is a red string!'));
console.log(text.cyan('This is a cyan string!'));
console.log(text.yellow('This is a yellow string!'));
1
2
3
4
5
6

image

# Chained colors

console.log(text.bold.red('this is a bold red message'));
console.log(text.bold.yellow.italic('this is a bold yellow italicized message'));
console.log(text.green.bold.underline('this is a bold green underlined message'));
1
2
3

image

# Nested colors

console.log(text.yellow(`foo ${text.red.bold('red')} bar ${text.cyan('cyan')} baz`));
1

image

# Nested styling bug

ansi-colors does not have the nested styling bug found in colorette (opens new window), chalk (opens new window), and kleur (opens new window).

const { bold, red } = require('ansi-styles');
console.log(bold(`foo ${red.dim('bar')} baz`));

const colorette = require('colorette');
console.log(colorette.bold(`foo ${colorette.red(colorette.dim('bar'))} baz`));

const kleur = require('kleur');
console.log(kleur.bold(`foo ${kleur.red.dim('bar')} baz`));

const chalk = require('chalk');
console.log(chalk.bold(`foo ${chalk.red.dim('bar')} baz`));
1
2
3
4
5
6
7
8
9
10
11

Results in the following

(sans icons and labels)

image

# Toggle color support

Easily enable/disable colors.

Colors plugin uses supports-colors plugin by default to check whether your terminal supports colors or not.

const {text, supportsColors} = require("rovel.js");

// disable colors manually
text.enabled = false;

// or use our plugin to automatically detect support
text.enabled = supportsColors.hasBasic;

console.log(text.red('I will only be colored red if the terminal supports colors'));
1
2
3
4
5
6
7
8
9

# Strip ANSI codes

Use the .unstyle method to strip ANSI codes from a string.

console.log(text.unstyle(text.blue.bold('foo bar baz')));
//=> 'foo bar baz'
1
2

# Available styles

Note that bright and bright-background colors are not always supported.

Colors Background Colors Bright Colors Bright Background Colors
black bgBlack blackBright bgBlackBright
red bgRed redBright bgRedBright
green bgGreen greenBright bgGreenBright
yellow bgYellow yellowBright bgYellowBright
blue bgBlue blueBright bgBlueBright
magenta bgMagenta magentaBright bgMagentaBright
cyan bgCyan cyanBright bgCyanBright
white bgWhite whiteBright bgWhiteBright
gray
grey

(gray is the U.S. spelling, grey is more commonly used in the Canada and U.K.)

# Style modifiers

  • dim

  • bold

  • hidden

  • italic

  • underline

  • inverse

  • strikethrough

  • reset

# Aliases

Create custom aliases for styles.

const colors = require('rovel.js').text;

colors.alias('primary', colors.yellow);
colors.alias('secondary', colors.bold);

console.log(colors.primary.secondary('Foo'));
1
2
3
4
5
6

# Themes

A theme is an object of custom aliases.

const colors = require('rovel.js').text;

colors.theme({
  danger: colors.red,
  dark: colors.dim.gray,
  disabled: colors.gray,
  em: colors.italic,
  heading: colors.bold.underline,
  info: colors.cyan,
  muted: colors.dim,
  primary: colors.blue,
  strong: colors.bold,
  success: colors.green,
  underline: colors.underline,
  warning: colors.yellow
});

// Now, we can use our custom styles alongside the built-in styles!
console.log(colors.danger.strong.em('Error!'));
console.log(colors.warning('Heads up!'));
console.log(colors.info('Did you know...'));
console.log(colors.success.bold('It worked!'));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22