$ npm install ttycolor
Terminal colors for node that respect whether the stdout
and stderr
streams are a tty
.
console
functionstty
tty.isatty
%j
)always|never|auto
)tty
test using format
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'});
var ttycolor = require('ttycolor')();
var revert = ttycolor.defaults();
console.log('a %s message', 'log');
console.info('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').white.bg.black);
console.info('%s', ansi('info message').cyan);
console.warn('%s', ansi('warn message').magenta);
console.error('%s', ansi('error message').bright.red);
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.
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.Print a warn message with escape sequence support, output is to stderr
.
format
: The format string....
: The format replacement parameters.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');
revert();
console.log('log: a %s message', 'log');
Map of foreground color codes.
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.
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.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()
.
console.log('%s', ansi('log message').white);
normal
white
black
blue
cyan
green
yellow
magenta
red
console.log('%s', ansi('log message').bg.black);
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:
ansi('value').bg.red.white.underline;
ansi('value').bg.red.underline.white;
ansi('value').underline.white.bg.red;
ansi('value').white.underline.bg.red;
ansi('value').white.bg.red.underline;
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
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();
console.info('an %s message', 'info');
console.error('an %s message', 'error');
revert();
console.info('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": [
"normal"
],
"parameters": [
"normal",
"bright"
]
},
"info": {
"format": [
"cyan"
],
"parameters": [
"cyan",
"bright"
]
},
"warn": {
"format": [
"magenta"
],
"parameters": [
"magenta",
"bright"
]
},
"error": {
"format": [
"red"
],
"parameters": [
"red",
"bright"
]
}
}
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: [
"red"
],
parameters: [
"red",
"bright"
],
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')})
revert();
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']}
}
defaults(styles);
keys.forEach(function(method) {console[method](method + ': %s', 'message')})
keys.forEach(function(method) {
console[method]('[%s] %s', ansi(method).bright[styles[method].format],
ansi(method).underline.bright[styles[method].format]);
});
keys.forEach(function(method) {
console[method]('[%s] %s', ansi(method).bright[styles[method].format],
ansi(method).bright);
});
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)
return modes.auto;
}
var ttycolor = require('ttycolor')(parser);
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.
© 2010 - cnpmjs.org x YWFE | Home | YWFE