Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 219c8e9

Browse filesBrowse 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 <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
1 parent 1fbd7ac commit 219c8e9
Copy full SHA for 219c8e9

File tree

Expand file treeCollapse file tree

3 files changed

+166
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+166
-15
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+63-4Lines changed: 63 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,72 @@ The default styles and associated colors are:
675675
* `symbol`: `green`
676676
* `undefined`: `grey`
677677

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

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

687746
<!-- type=misc -->
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+75-11Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,86 @@ ObjectDefineProperty(inspect, 'defaultOptions', {
269269
}
270270
});
271271

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

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

‎test/parallel/test-util-inspect.js‎

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

20772105
assert.strictEqual(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.