Terminal colors that respect whether a stream is a tty

Downloads in past


401.1.07 years ago9 years agoMinified + gzip package size for ttycolor in KB


Build Status npm version Coverage Status
Terminal colors for nodenode that respect whether the stdout and stderr streams are a tty.


  • Extends the familiar console functions
  • Always respects whether the stream is a tty
  • Chainable attributes, foreground colors and background colors
  • Asynchronous write to any stream respecting tty.isatty
  • Default styles to keep the code clean
  • Safely stringify circular references (%j)
  • Argument parsing support (always|never|auto)
  • Stringify with a custom tty test using format
  • 100% code coverage


npm install ttycolor


npm test

Example executables are not included in the default test suite as they do not contribute to the code coverage, however you can run tests against them with:
npm run exe


There are various example and test programs in the bin directory. Note that these executables are not included when the package is distributed via npm.


var ttycolor = require('ttycolor')();
console.log('%s', 'value');
console.log('%d', 3.14);
console.log('%j', {message: 'json'});
console.log('a %s of %d with %j', 'value', 3.14, {message: 'json'});

Defaults (recommended)

var ttycolor = require('ttycolor')();
var revert = ttycolor.defaults();
console.log('a %s message', 'log');'an %s message', 'info');
console.warn('a %s message', 'warn');
console.error('an %s message', 'error');


var ansi = require('ttycolor')().ansi;
console.log('%s', ansi('log message');'%s', ansi('info message').cyan);
console.warn('%s', ansi('warn message').magenta);
console.error('%s', ansi('error message');
console.log('pi %d', ansi(3.14).blue.underline);
console.log('%j', ansi({message: 'json'}).red);



This package overrides the default console functions, you may access the original console functions via the console property exposed by the module. Note that the console.dir method is left untouched.
All overriden methods proxy to the original method after handling escape sequences.

console.log(format, ...)

Print a log message with escape sequence support, output is to stdout.
  • format: The format string.
  • ...: The format replacement parameters., ...)

Print an info message with escape sequence support, output is to stdout.
  • format: The format string.
  • ...: The format replacement parameters.

console.warn(format, ...)

Print a warn message with escape sequence support, output is to stderr.
  • format: The format string.
  • ...: The format replacement parameters.

console.error(format, ...)

Print an error message with escape sequence support, output is to stderr.
  • format: The format string.
  • ...: The format replacement parameters.



Wrap value in a chainable color instance, the value will be coerced to a string when passed to a console function.


Map of attribute codes.


Map of background color codes.


Array of color names.


Map referencing the original console methods.


Configure default styles for the console functions.
  • styles: Styles to use, if this option is not specified then the module default styles are used.

This method returns a closure that allows reverting to the previous console functions.
var ttycolor = require('ttycolor'), defaults = ttycolor.defaults, revert;
revert = defaults();
console.log('log: a %s message', 'log');
console.log('log: a %s message', 'log');


Map of foreground color codes.

format(test, format, ...)

Return a formatted string with escape sequences. If the first argument is a function it should return a boolean indicating whether escape sequences should be used.
  • test: A function used to determine the tty boolean.
  • format: The format string.
  • ...: The format replacement parameters.

Note that there are no default styles for this method even if defaults() has been invoked.


Map of highlighting modes.

stringify(value, code, attr, tag)

Low-level method for creating escaped string sequences.
  • value: The value to escape, will be coerced to a string.
  • code: The color code.
  • attr: Optional attribute code.
  • tag: Optional close tag.

write(options, format, ...)

Asynchronously write to an arbitrary writable stream with escape sequence support if the stream is a tty as reported by tty.isatty.
  • options: The write options.
  • format: The format string.
  • ...: The format replacement parameters.

The options object should contain the properties stream and callback. The callback is invoked when the write operation has completed and is passed the value written to the stream.
The stream must be open and have an associated fd or this method will throw an error.
Note this method is only available after the module main method has been initialized so that the stream write method can respect commmand line arguments. For example:
var ttycolor = require('ttycolor')();
// ttycolor.write(...);


Map of styles to use when no arguments are passed to defaults().


Foreground Colors

console.log('%s', ansi('log message').white);

  • normal
  • white
  • black
  • blue
  • cyan
  • green
  • yellow
  • magenta
  • red

Background Colors

console.log('%s', ansi('log message');

Background colors are set by accessing a color property via the bg property. Property names are identical to the foreground color list.
Note that the background color chain is reset after accessing a color property such that order is not important when combining background and foreground colors, the following are all equivalent:


console.log('%s', ansi('log message').bright);

Depending upon the terminal emulator some attributes may not be supported and will have no visual effect. Typically bright, underline and reverse are safe to use.
  • bright
  • dim
  • italic
  • underline
  • blink
  • reverse

Default Styles

To keep your code clean and the program output consistent it is a good idea to use the default styles.
var ttycolor = require('ttycolor')(), defaults = ttycolor.defaults, revert;
revert = defaults();'an %s message', 'info');
console.error('an %s message', 'error');
revert();'an %s message', 'info');
console.error('an %s message', 'error');


To customise the styles when using defaults() create an object with the following structure (see below for some example code):
  "log": {
    "format": [
    "parameters": [
  "info": {
    "format": [
    "parameters": [
  "warn": {
    "format": [
    "parameters": [
  "error": {
    "format": [
    "parameters": [

If you need to format a particular parameter, declare a params object with the given index and assign a function that returns an ansi instance, for example:
  error: {
    format: [
    parameters: [
    params: {
      '0': function(arg) {
        // override the style for a parameter at index 0
        return ansi(arg).magenta; 


An example derived from the defaults executable:
#!/usr/bin/env node
var ttycolor = require('ttycolor')(), defaults = ttycolor.defaults,
  revert, styles, keys;
revert = defaults(); keys = Object.keys(ttycolor.styles); ansi = ttycolor.ansi;
keys.forEach(function(method) {console[method]('%s: %s', method, 'message')})
keys.forEach(function(method) {console[method]('%s: %s', method, 'message')})
styles = {
  log: {format: ['normal'], parameters: ['normal', 'underline']},
  info: {format: ['cyan'], parameters: ['cyan', 'underline']},
  warn: {format: ['magenta'], parameters: ['magenta', 'underline']},
  error: {format: ['red'], parameters: ['red', 'underline']}
keys.forEach(function(method) {console[method](method + ': %s', 'message')})
keys.forEach(function(method) {
  console[method]('[%s] %s', ansi(method).bright[styles[method].format],
keys.forEach(function(method) {
  console[method]('[%s] %s', ansi(method).bright[styles[method].format],


The module supports argument parsing with the modes always, auto and never. Argument parsing is built in to the module to prevent repeating the option parsing logic for multiple command line programs. All you need to do is document the option(s) in your program's help or documentation.
Note that this behaviour is enabled by default so that you may enable highlighting before options are parsed so that whichever module is used for option parsing will respect highlighting provided defaults() is invoked prior to parsing arguments and that the module uses the console methods to print errors.
By default this module will use the auto behaviour.


The default argument parsing supports the following variations:
  • --color: Sets the mode to always.
  • --no-color: Sets the mode to never.
  • --color=always: Sets the mode to always.
  • --color=auto: Sets the mode to auto.
  • --color=never: Sets the mode to never.
  • --color always: Sets the mode to always.
  • --color auto: Sets the mode to auto.
  • --color never: Sets the mode to never.


If you want to implement a custom argument parser you can pass a function when initializing the module.
function parser(modes, option, argv) {
  // do argument parsing and return a mode (always|auto|never)
var ttycolor = require('ttycolor')(parser);
parser(modes, option, argv)
  • modes: A map of the available highlighting modes.
  • option: A map containing option keys.
  • argv: Arguments to parse, value is process.argv.slice(2).

The parser function should return a string representing one of the available modes.


You may wish to disable argument parsing, to do so pass false when initializing the module.
var ttycolor = require('ttycolor')(false);


Everything is MIT. Read the license if you feel inclined.