Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Enable color customization of util.inspect
Browse files Browse the repository at this point in the history
This is rewrite of #3701 and #3603 before.

This patch introduce `util.inspect.styles`
and `util.inspect.colors` objects, which enables customization
of color sequences.
  • Loading branch information
langpavel authored and isaacs committed Jul 17, 2012
1 parent b8d8615 commit ff14007
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
23 changes: 23 additions & 0 deletions doc/api/util.markdown
Expand Up @@ -81,13 +81,36 @@ in `null` for `depth`.

If `colors` is `true`, the output will be styled with ANSI color codes.
Defaults to `false`.
Colors are customizable, see below.

Example of inspecting all properties of the `util` object:

var util = require('util');

console.log(util.inspect(util, true, null));

### Customizing `util.inspect` colors

Color output (if enabled) of `util.inspect` is customizable globally
via `util.inspect.styles` and `util.inspect.colors` objects.

`util.inspect.styles` is a map assigning each style a color
from `util.inspect.colors`.
Highlighted styles and their default values are:
* `number` (yellow)
* `boolean` (yellow)
* `string` (green)
* `date` (magenta)
* `regexp` (red)
* `null` (bold)
* `undefined` (grey)
* `special` - only function at this time (cyan)
* `name` (intentionally no styling)

Predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
`green`, `magenta`, `red` and `yellow`.
There are also `bold`, `italic`, `underline` and `inverse` codes.


## util.isArray(object)

Expand Down
10 changes: 5 additions & 5 deletions lib/util.js
Expand Up @@ -128,7 +128,7 @@ exports.inspect = inspect;


// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
var colors = {
inspect.colors = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
Expand All @@ -145,7 +145,7 @@ var colors = {
};

// Don't use 'blue' not visible on cmd.exe
var styles = {
inspect.styles = {
'special': 'cyan',
'number': 'yellow',
'boolean': 'yellow',
Expand All @@ -159,11 +159,11 @@ var styles = {


function stylizeWithColor(str, styleType) {
var style = styles[styleType];
var style = inspect.styles[styleType];

if (style) {
return '\u001b[' + colors[style][0] + 'm' + str +
'\u001b[' + colors[style][1] + 'm';
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
'\u001b[' + inspect.colors[style][1] + 'm';
} else {
return str;
}
Expand Down
23 changes: 23 additions & 0 deletions test/simple/test-util-inspect.js
Expand Up @@ -107,3 +107,26 @@ assert.doesNotThrow(function() {
// GH-2225
var x = { inspect: util.inspect };
assert.ok(util.inspect(x).indexOf('inspect') != -1);

// util.inspect.styles and util.inspect.colors
function test_color_style(style, input, implicit) {
var color_name = util.inspect.styles[style];
var color = ['', ''];
if(util.inspect.colors[color_name])
color = util.inspect.colors[color_name];

var without_color = util.inspect(input, false, 0, false);
var with_color = util.inspect(input, false, 0, true);
var expect = '\u001b[' + color[0] + 'm' + without_color +
'\u001b[' + color[1] + 'm';
assert.equal(with_color, expect, 'util.inspect color for style '+style);
}

test_color_style('special', function(){});
test_color_style('number', 123.456);
test_color_style('boolean', true);
test_color_style('undefined', undefined);
test_color_style('null', null);
test_color_style('string', 'test string');
test_color_style('date', new Date);
test_color_style('regexp', /regexp/);

0 comments on commit ff14007

Please sign in to comment.