Get colors in your node.js console (evacuated from NSA/Microsoft Github)

anonymous 7b2df7e777 debian update 1 year ago
debian 7b2df7e777 debian update 1 year ago
examples 5aebc674d8 Add ESLint and lint all files; add lint check to CI 2 years ago
lib 2894751d40 Remove another dynamic require and add similar deprecation notice 2 years ago
screenshots 13dd5a84e3 Revert "Reverted to the latest tag" 5 years ago
tests 5aebc674d8 Add ESLint and lint all files; add lint check to CI 2 years ago
themes 5aebc674d8 Add ESLint and lint all files; add lint check to CI 2 years ago
.eslintrc.json 5aebc674d8 Add ESLint and lint all files; add lint check to CI 2 years ago
.gitignore 5aebc674d8 Add ESLint and lint all files; add lint check to CI 2 years ago
.npmignore e368a283b1 Add .npmignore to exclude unnecessary development files from npm package 5 years ago
.travis.yml a8ce90c51c Add edge case handling for undefined styles 2 years ago
LICENSE 5152d16f22 Update LICENSE 4 years ago
README.md e89ff93c09 Resolve merge conflicts 2 years ago
ROADMAP.md 64a2ea2510 update roadmap 2 years ago
index.d.ts b674514f1b Expose enable/disable methods 2 years ago
package-lock.json 2894751d40 Remove another dynamic require and add similar deprecation notice 2 years ago
package.json 2894751d40 Remove another dynamic require and add similar deprecation notice 2 years ago
safe.d.ts dc1ea49d2b Add missing setTheme definition 2 years ago
safe.js 05129e7811 Revert dummy commit 2 years ago

README.md

colors.js

Build Status version dependencies devDependencies

Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback, and check the develop branch for the latest bleeding-edge updates.

get color and style in your node.js console

Demo

Installation

npm install colors

colors and styles!

text colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • gray
  • grey

background colors

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite

styles

  • reset
  • bold
  • dim
  • italic
  • underline
  • inverse
  • hidden
  • strikethrough

extras

  • rainbow
  • zebra
  • america
  • trap
  • random

Usage

By popular demand, colors now ships with two types of usages!

The super nifty way

var colors = require('colors');

console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
console.log('OMG Rainbows!'.rainbow); // rainbow
console.log('Run the trap'.trap); // Drops the bass

or a slightly less nifty way which doesn't extend String.prototype

var colors = require('colors/safe');

console.log(colors.green('hello')); // outputs green text
console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text
console.log(colors.inverse('inverse the color')); // inverses the color
console.log(colors.rainbow('OMG Rainbows!')); // rainbow
console.log(colors.trap('Run the trap')); // Drops the bass

I prefer the first way. Some people seem to be afraid of extending String.prototype and prefer the second way.

If you are writing good code you will never have an issue with the first approach. If you really don't want to touch String.prototype, the second usage will not touch String native object.

Disabling Colors

To disable colors you can pass the following arguments in the command line to your application:

node myapp.js --no-color

Console.log string substitution

var name = 'Marak';
console.log(colors.green('Hello %s'), name);
// outputs -> 'Hello Marak'

Custom themes

Using standard API


var colors = require('colors');

colors.setTheme({
  silly: 'rainbow',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
  info: 'green',
  data: 'grey',
  help: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});

// outputs red text
console.log("this is an error".error);

// outputs yellow text
console.log("this is a warning".warn);

Using string safe API

var colors = require('colors/safe');

// set single property
var error = colors.red;
error('this is red');

// set theme
colors.setTheme({
  silly: 'rainbow',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
  info: 'green',
  data: 'grey',
  help: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});

// outputs red text
console.log(colors.error("this is an error"));

// outputs yellow text
console.log(colors.warn("this is a warning"));

Combining Colors

var colors = require('colors');

colors.setTheme({
  custom: ['red', 'underline']
});

console.log('test'.custom);

Protip: There is a secret undocumented style in colors. If you find the style you can summon him.