Browse Source

Enable color customization of `util.inspect`

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.
v0.9.1-release
Pavel Lang 13 years ago
committed by isaacs
parent
commit
ff14007573
  1. 23
      doc/api/util.markdown
  2. 10
      lib/util.js
  3. 23
      test/simple/test-util-inspect.js

23
doc/api/util.markdown

@ -81,6 +81,7 @@ 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:
@ -88,6 +89,28 @@ Example of inspecting all properties of the `util` object:
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)

10
lib/util.js

@ -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],
@ -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',
@ -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;
}

23
test/simple/test-util-inspect.js

@ -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/);

Loading…
Cancel
Save