Skip to content

Commit 608d720

Browse files
BridgeARtargos
authored andcommitted
util: add more predefined color codes to inspect.colors
This adds most commonly used ANSI color codes to `util.inspect.colors`. PR-URL: #30659 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 77ffd54 commit 608d720

File tree

3 files changed

+166
-15
lines changed

3 files changed

+166
-15
lines changed

doc/api/util.md

+63-4
Original file line numberDiff line numberDiff line change
@@ -678,13 +678,72 @@ The default styles and associated colors are:
678678
* `symbol`: `green`
679679
* `undefined`: `grey`
680680

681-
The predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
682-
`green`, `magenta`, `red` and `yellow`. There are also `bold`, `italic`,
683-
`underline` and `inverse` codes.
684-
685681
Color styling uses ANSI control codes that may not be supported on all
686682
terminals. To verify color support use [`tty.hasColors()`][].
687683

684+
Predefined control codes are listed below (grouped as "Modifiers", "Foreground
685+
colors", and "Background colors").
686+
687+
#### Modifiers
688+
689+
Modifier support varies throughout different terminals. They will mostly be
690+
ignored, if not supported.
691+
692+
* `reset` - Resets all (color) modifiers to their defaults
693+
* **bold** - Make text bold
694+
* _italic_ - Make text italic
695+
* <span style="border-bottom: 1px;">underline</span> - Make text underlined
696+
* ~~strikethrough~~ - Puts a horizontal line through the center of the text
697+
(Alias: `strikeThrough`, `crossedout`, `crossedOut`)
698+
* `hidden` - Prints the text, but makes it invisible (Alias: conceal)
699+
* <span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias:
700+
`faint`)
701+
* <span style="border-top: 1px">overlined</span> - Make text overlined
702+
* blink - Hides and shows the text in an interval
703+
* <span style="filter: invert(100%)">inverse</span> - Swap foreground and
704+
background colors (Alias: `swapcolors`, `swapColors`)
705+
* <span style="border-bottom: 1px double;">doubleunderline</span> - Make text
706+
double underlined (Alias: `doubleUnderline`)
707+
* <span style="border: 1px">framed</span> - Draw a frame around the text
708+
709+
#### Foreground colors
710+
711+
* `black`
712+
* `red`
713+
* `green`
714+
* `yellow`
715+
* `blue`
716+
* `magenta`
717+
* `cyan`
718+
* `white`
719+
* `gray` (alias: `grey`, `blackBright`)
720+
* `redBright`
721+
* `greenBright`
722+
* `yellowBright`
723+
* `blueBright`
724+
* `magentaBright`
725+
* `cyanBright`
726+
* `whiteBright`
727+
728+
#### Background colors
729+
730+
* `bgBlack`
731+
* `bgRed`
732+
* `bgGreen`
733+
* `bgYellow`
734+
* `bgBlue`
735+
* `bgMagenta`
736+
* `bgCyan`
737+
* `bgWhite`
738+
* `bgGray` (alias: `bgGrey`, `bgBlackBright`)
739+
* `bgRedBright`
740+
* `bgGreenBright`
741+
* `bgYellowBright`
742+
* `bgBlueBright`
743+
* `bgMagentaBright`
744+
* `bgCyanBright`
745+
* `bgWhiteBright`
746+
688747
### Custom inspection functions on Objects
689748

690749
<!-- type=misc -->

lib/internal/util/inspect.js

+75-11
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,86 @@ ObjectDefineProperty(inspect, 'defaultOptions', {
267267
}
268268
});
269269

270-
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
270+
// Set Graphics Rendition http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
271+
// Each color consists of an array with the color code as first entry and the
272+
// reset code as second entry.
273+
const defaultFG = 39;
274+
const defaultBG = 49;
271275
inspect.colors = ObjectAssign(ObjectCreate(null), {
276+
reset: [0, 0],
272277
bold: [1, 22],
278+
dim: [2, 22], // Alias: faint
273279
italic: [3, 23],
274280
underline: [4, 24],
275-
inverse: [7, 27],
276-
white: [37, 39],
277-
grey: [90, 39],
278-
black: [30, 39],
279-
blue: [34, 39],
280-
cyan: [36, 39],
281-
green: [32, 39],
282-
magenta: [35, 39],
283-
red: [31, 39],
284-
yellow: [33, 39]
281+
blink: [5, 25],
282+
// Swap forground and background colors
283+
inverse: [7, 27], // Alias: swapcolors, swapColors
284+
hidden: [8, 28], // Alias: conceal
285+
strikethrough: [9, 29], // Alias: strikeThrough, crossedout, crossedOut
286+
doubleunderline: [21, 24], // Alias: doubleUnderline
287+
black: [30, defaultFG],
288+
red: [31, defaultFG],
289+
green: [32, defaultFG],
290+
yellow: [33, defaultFG],
291+
blue: [34, defaultFG],
292+
magenta: [35, defaultFG],
293+
cyan: [36, defaultFG],
294+
white: [37, defaultFG],
295+
bgBlack: [40, defaultBG],
296+
bgRed: [41, defaultBG],
297+
bgGreen: [42, defaultBG],
298+
bgYellow: [43, defaultBG],
299+
bgBlue: [44, defaultBG],
300+
bgMagenta: [45, defaultBG],
301+
bgCyan: [46, defaultBG],
302+
bgWhite: [47, defaultBG],
303+
framed: [51, 54],
304+
overlined: [53, 55],
305+
gray: [90, defaultFG], // Alias: grey, blackBright
306+
redBright: [91, defaultFG],
307+
greenBright: [92, defaultFG],
308+
yellowBright: [93, defaultFG],
309+
blueBright: [94, defaultFG],
310+
magentaBright: [95, defaultFG],
311+
cyanBright: [96, defaultFG],
312+
whiteBright: [97, defaultFG],
313+
bgGray: [100, defaultBG], // Alias: bgGrey, bgBlackBright
314+
bgRedBright: [101, defaultBG],
315+
bgGreenBright: [102, defaultBG],
316+
bgYellowBright: [103, defaultBG],
317+
bgBlueBright: [104, defaultBG],
318+
bgMagentaBright: [105, defaultBG],
319+
bgCyanBright: [106, defaultBG],
320+
bgWhiteBright: [107, defaultBG],
285321
});
286322

323+
function defineColorAlias(target, alias) {
324+
ObjectDefineProperty(inspect.colors, alias, {
325+
get() {
326+
return this[target];
327+
},
328+
set(value) {
329+
this[target] = value;
330+
},
331+
configurable: true,
332+
enumerable: false
333+
});
334+
}
335+
336+
defineColorAlias('gray', 'grey');
337+
defineColorAlias('gray', 'blackBright');
338+
defineColorAlias('bgGray', 'bgGrey');
339+
defineColorAlias('bgGray', 'bgBlackBright');
340+
defineColorAlias('dim', 'faint');
341+
defineColorAlias('strikethrough', 'crossedout');
342+
defineColorAlias('strikethrough', 'strikeThrough');
343+
defineColorAlias('strikethrough', 'crossedOut');
344+
defineColorAlias('hidden', 'conceal');
345+
defineColorAlias('inverse', 'swapColors');
346+
defineColorAlias('inverse', 'swapcolors');
347+
defineColorAlias('doubleunderline', 'doubleUnderline');
348+
349+
// TODO(BridgeAR): Add function style support for more complex styles.
287350
// Don't use 'blue' not visible on cmd.exe
288351
inspect.styles = ObjectAssign(ObjectCreate(null), {
289352
special: 'cyan',
@@ -296,6 +359,7 @@ inspect.styles = ObjectAssign(ObjectCreate(null), {
296359
symbol: 'green',
297360
date: 'magenta',
298361
// "name": intentionally not styling
362+
// TODO(BridgeAR): Highlight regular expressions properly.
299363
regexp: 'red',
300364
module: 'underline'
301365
});

test/parallel/test-util-inspect.js

+28
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,34 @@ assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');
20752075
`\u001b[${string[0]}m'Oh no!'\u001b[${string[1]}m }`
20762076
);
20772077
rejection.catch(() => {});
2078+
2079+
// Verify that aliases do not show up as key while checking `inspect.colors`.
2080+
const colors = Object.keys(inspect.colors);
2081+
const aliases = Object.getOwnPropertyNames(inspect.colors)
2082+
.filter((c) => !colors.includes(c));
2083+
assert(!colors.includes('grey'));
2084+
assert(colors.includes('gray'));
2085+
// Verify that all aliases are correctly mapped.
2086+
for (const alias of aliases) {
2087+
assert(Array.isArray(inspect.colors[alias]));
2088+
}
2089+
// Check consistent naming.
2090+
[
2091+
'black',
2092+
'red',
2093+
'green',
2094+
'yellow',
2095+
'blue',
2096+
'magenta',
2097+
'cyan',
2098+
'white'
2099+
].forEach((color, i) => {
2100+
assert.deepStrictEqual(inspect.colors[color], [30 + i, 39]);
2101+
assert.deepStrictEqual(inspect.colors[`${color}Bright`], [90 + i, 39]);
2102+
const bgColor = `bg${color[0].toUpperCase()}${color.slice(1)}`;
2103+
assert.deepStrictEqual(inspect.colors[bgColor], [40 + i, 49]);
2104+
assert.deepStrictEqual(inspect.colors[`${bgColor}Bright`], [100 + i, 49]);
2105+
});
20782106
}
20792107

20802108
assert.strictEqual(

0 commit comments

Comments
 (0)